Installing and configuring

Ingo Schommer edited this page May 19, 2015 · 9 revisions
  • Extract the package to the 'queuedjobs' directory
  • Install the cronjob needed to manage all the jobs within the system. It is best to have this execute as the same user as your webserver - this prevents any problems with file permissions. Remember, you can add > /dev/null 2>&1 to this line to have any output silenced (otherwise you may get mailbombed by any SS output during job execution)

*/1 * * * * php /path/to/silverstripe/framework/cli-script.php dev/tasks/ProcessJobQueueTask queue=2

  • If your code is to make use of the 'long' jobs, ie that could take minutes (days!? hah) to process, also install another task that processes this queue. Its time of execution can be left a little longer.

*/15 * * * * php /path/to/silverstripe/framework/cli-script.php dev/tasks/ProcessJobQueueTask queue=3

  • If you're on windows, you can use the system scheduler.

  • To test things are working, from your browser run the action; this will create a dummy job on the 'Jobs' tab of the CMS

  • To execute the job manually, navigate to (or dev/tasks/ProcessJobQueueTask?queue=3 if there was more than 50 items in the job). The cronjob effective executes this exact same action when it triggers.

  • From your code, add a new job for execution.

    $publish = new PublishItemsJob(21); singleton('QueuedJobService')->queueJob($publish);

  • To schedule a job to be executed at some point in the future, pass a date through with the call to queueJob

The following will run the publish job in 1 day's time from now.

$publish = new PublishItemsJob(21);
singleton('QueuedJobService')->queueJob($publish, date('Y-m-d H:i:s', time() + 86400));

Configuring the lsyncd watcher

By default the system will execute 'immediate' jobs when the current web request ends, which to an extent removes any of the benefits of using a background processor. However, to make sure these immediate jobs execute, well, immediately, there needs to be a mechanism for detecting their creation, and launching the background processing script. To aid this, whenever a job is created an empty file will be created in $TEMP_DIR/queuedjobs (typically project-name/silverstripe-cache/queuedjobs or /tmp/silverstripe-instance/queuedjobs). Using lsyncd (or something similar for windows), you can then trigger the processor script to actually execute the job.

The following is an example config /etc/lsyncd.conf

-- Queue Processor configuration, typically placed in /etc/lsyncd.conf 
-- Remember to set QueuedJobService::$use_shutdown_function = false; in local.conf.php

settings = {
	logfile    = "/var/log/lsyncd/lsyncd.log",
	statusFile = "/var/run/lsyncd.status",
	nodaemon   = true,

-- Define the command and path for the each system being monitored here, where webuser is the user your webserver
-- runs as
runcmd = "/sbin/runuser webuser -c \"/usr/bin/php /var/www/sitepath/framework/cli-script.php dev/tasks/ProcessJobQueueTask job=\"$1\" /var/www/sitepath/framework/silverstripe-cache/queuedjobs\""

site_processor = {
	onCreate = function(event)
	log("Normal", "got an onCreate Event")
	   spawnShell(event, runcmd, event.basename)

sync{site_processor, source="/var/www/sitepath/silverstripe-cache/queuedjobs"}
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.