XF 2.0 What triggers cron jobs?

djbaxter

in memoriam 1947-2022
With vBulletin, there was a pixel in the footer that a visitor triggered which in turn triggered cron jobs to run.

How does this work in Xenforo?
 
deferred.php was the XF 1.5 mechanism.

In XF 2.x it uses job.php in the forum root directory - triggered by an Ajax call on certain page loads when the system detects jobs waiting to be run.

In XF 2.x, "jobs" are (typically) restartable processes which run in the background doing tasks which can sometimes take a long time to complete - it has built in mechanisms to process small chunks of work before stopping and then restarting again on the next trigger. Typically they are used for things like rebuilding data or caches, sending large batches of email, processing email bounces - all those background tasks which are not user-facing and aren't time-critical.

The cron system is actually one of these jobs!

The cron "job" looks through the list of cron entries and calculates which are due to run, then loops through and runs those cron tasks until the allowed execution time is reached - by default, that's 8 seconds and this is configurable via a config.php option. Note that it doesn't forcefully stop a task from running if it takes longer than 8 seconds, it just won't start any new tasks if it works out that it has already been running the various cron tasks for more than 8 seconds. It will stop and then wait to be triggered by the job manager again.

Normally a job will do it's work and then if it runs out of time, will update the system with counters for where it is up to and wait to be triggered again (waits for a "resume"). Alternatively, if it has finished its task (rebuilding data, or sending mail, or whatever), it will mark itself as "complete" and stop execution. Execution will start again (from the beginning) once explicitly triggered by some process or user/admin action.

The trick with the Cron job is that it never completes. The last step it takes, is to mark itself as unfinished and calculate the time the next cron task is due to run. It then sets that as the trigger time for job manager to run it again. The job entry for the cron system remains in the database - and will be run by the job system again once the trigger time passes.

If you look in the xf_job table on XF2.x, you should always see an entry for XF\Job\Cron - the trigger date will be the time it has calculated the next cron task is due to run. This is recalculated if you add or update a cron entry in the admin ui.

If you want a greater understanding of how it all hangs together - you can disable the browser based ajax trigger and trigger the job manager manually using my addon https://xenforo.com/community/resources/cli-job-runner.6478/ and watch what happens in the database as it does its thing.

That ajax call which triggers job.php will only appear on a page load if the system detects that there are jobs to be run.

If you look at the HTML tag at the top of the page - you'll see data-run-jobs="" appear whenever the system detects that there are jobs to be run:

HTML:
<!DOCTYPE html>
<html id="XF" lang="en-US" dir="LTR"
    data-app="public"
    data-template="forum_list"
    data-container-key=""
    data-content-key=""
    data-logged-in="true"
    data-cookie-prefix="xf_"
    class="has-no-js template-forum_list"
     data-run-jobs=""> <!-- <== this is the magic bit! -->

This is picked up by the XF JavaScript which posts an Ajax call to job.php - search for XF.JobRunner in js/xf/core.js to see details.
 
Top Bottom