class QueuedJobService (View source)

A service that can be used for starting, stopping and listing queued jobs.

When a job is first added, it is initialised, its job type determined, then persisted to the database

When the queues are scanned, a job is reloaded and processed. Ignoring the persistence and reloading, it looks something like

job->getJobType(); job->getJobData(); data->write(); job->setup(); while !job->isComplete job->process(); job->getJobData(); data->write();

Properties

protected int $startedAt

Timestamp (in seconds) when the queue was started

public DefaultQueueHandler $queueHandler
public TaskRunnerEngine $queueRunner
public array $defaultJobs

Config controlled list of default/required jobs

Methods

public
__construct()

Register our shutdown handler

public
int
queueJob(QueuedJob $job, $startAfter = null, int $userId = null, $queueName = null)

Adds a job to the queue to be started

public
startJob(JobDescriptor $jobDescriptor, date $startAfter = null)

Start a job (or however the queue handler determines it should be started)

protected
copyJobToDescriptor(QueuedJob $job, JobDescriptor $jobDescriptor)

Copies data from a job into a descriptor for persisting

protected
copyDescriptorToJob(QueuedJobDescriptor $jobDescriptor, QueuedJob $job)

No description

public
getNextPendingJob(string $type = null)

Check the current job queues and see if any of the jobs currently in there should be started. If so, return the next job that should be executed

public
checkJobHealth(int $queue = null)

Runs an explicit check on all currently running jobs to make sure their "processed" count is incrementing between each run. If it's not, then we need to flag it as paused due to an error.

public
checkdefaultJobs($queue = null)

Checks through all the scheduled jobs that are expected to exist

protected
bool
restartStalledJob(QueuedJobDescriptor $stalledJob)

Attempt to restart a stalled job

protected
QueuedJob|bool
initialiseJob(QueuedJobDescriptor $jobDescriptor)

Prepares the given jobDescriptor for execution. Returns the job that will actually be run in a state ready for executing.

protected
bool
grabMutex(QueuedJobDescriptor $jobDescriptor)

Given a QueuedJobDescriptor mark the job as initialised. Works sort of like a mutex.

public
bool
runJob(int $jobId)

Start the actual execution of a job.

protected
markStarted()

Start timer

protected
bool
hasPassedTimeLimit()

Is execution time too long?

protected
bool
isMemoryTooHigh()

Is memory usage too high?

protected
float
getMemoryUsage()

Get peak memory usage of this application

protected
float
getMemoryLimit()

Determines the memory limit (in bytes) for this application Limits to the smaller of memory_limit configured via php.ini or silverstripe config

protected
float
getPHPMemoryLimit()

Calculate the current memory limit of the server

protected
float
parseMemory(string $memString)

Convert memory limit string to bytes.

protected
humanReadable($size)

No description

public
getJobList(string $type = null, int $includeUpUntil = 0)

Gets a list of all the current jobs (or jobs that have recently finished)

public
string
getJobListFilter(string $type = null, int $includeUpUntil = 0)

Return the SQL filter used to get the job list - this is used by the UI for displaying the job list.

public
runQueue(string $queue)

Process the job queue with the current queue runner

public
processJobQueue(string $name)

Process all jobs from a given queue

public
onShutdown()

When PHP shuts down, we want to process all of the immediate queue items

Details

__construct()

Register our shutdown handler

int queueJob(QueuedJob $job, $startAfter = null, int $userId = null, $queueName = null)

Adds a job to the queue to be started

Relevant data about the job will be persisted using a QueuedJobDescriptor

Parameters

QueuedJob $job

The job to start.

$startAfter

The date (in Y-m-d H:i:s format) to start execution after

int $userId

The ID of a user to execute the job as. Defaults to the current user

$queueName

Return Value

int

startJob(JobDescriptor $jobDescriptor, date $startAfter = null)

Start a job (or however the queue handler determines it should be started)

Parameters

JobDescriptor $jobDescriptor
date $startAfter

protected copyJobToDescriptor(QueuedJob $job, JobDescriptor $jobDescriptor)

Copies data from a job into a descriptor for persisting

Parameters

QueuedJob $job
JobDescriptor $jobDescriptor

protected copyDescriptorToJob(QueuedJobDescriptor $jobDescriptor, QueuedJob $job)

No description

Parameters

QueuedJobDescriptor $jobDescriptor
QueuedJob $job

QueuedJobDescriptor getNextPendingJob(string $type = null)

Check the current job queues and see if any of the jobs currently in there should be started. If so, return the next job that should be executed

Parameters

string $type

Job type

Return Value

QueuedJobDescriptor

checkJobHealth(int $queue = null)

Runs an explicit check on all currently running jobs to make sure their "processed" count is incrementing between each run. If it's not, then we need to flag it as paused due to an error.

This typically happens when a PHP fatal error is thrown, which can't be picked up by the error handler or exception checker; in this case, we detect these stalled jobs later and fix (try) to fix them

Parameters

int $queue

The queue to check against

checkdefaultJobs($queue = null)

Checks through all the scheduled jobs that are expected to exist

Parameters

$queue

protected bool restartStalledJob(QueuedJobDescriptor $stalledJob)

Attempt to restart a stalled job

Parameters

QueuedJobDescriptor $stalledJob

Return Value

bool

True if the job was successfully restarted

protected QueuedJob|bool initialiseJob(QueuedJobDescriptor $jobDescriptor)

Prepares the given jobDescriptor for execution. Returns the job that will actually be run in a state ready for executing.

Note that this is called each time a job is picked up to be executed from the cron job - meaning that jobs that are paused and restarted will have 'setup()' called on them again, so your job MUST detect that and act accordingly.

Parameters

QueuedJobDescriptor $jobDescriptor

The Job descriptor of a job to prepare for execution

Return Value

QueuedJob|bool

protected bool grabMutex(QueuedJobDescriptor $jobDescriptor)

Given a QueuedJobDescriptor mark the job as initialised. Works sort of like a mutex.

Currently a database lock isn't entirely achievable, due to database adapters not supporting locks. This may still have a race condition, but this should minimise the possibility. Side effect is the job status will be changed to "Initialised".

Assumption is the job has a status of "Queued" or "Wait".

Parameters

QueuedJobDescriptor $jobDescriptor

Return Value

bool

bool runJob(int $jobId)

Start the actual execution of a job.

The assumption is the jobID refers to a QueuedJobDescriptor that is status set as "Queued".

This method will continue executing until the job says it's completed

Parameters

int $jobId

The ID of the job to start executing

Return Value

bool

protected markStarted()

Start timer

protected bool hasPassedTimeLimit()

Is execution time too long?

Return Value

bool

True if the script has passed the configured time_limit

protected bool isMemoryTooHigh()

Is memory usage too high?

Return Value

bool

protected float getMemoryUsage()

Get peak memory usage of this application

Return Value

float

protected float getMemoryLimit()

Determines the memory limit (in bytes) for this application Limits to the smaller of memory_limit configured via php.ini or silverstripe config

Return Value

float

Memory limit in bytes

protected float getPHPMemoryLimit()

Calculate the current memory limit of the server

Return Value

float

protected float parseMemory(string $memString)

Convert memory limit string to bytes.

Based on implementation in install.php5

Parameters

string $memString

Return Value

float

protected humanReadable($size)

No description

Parameters

$size

getJobList(string $type = null, int $includeUpUntil = 0)

Gets a list of all the current jobs (or jobs that have recently finished)

Parameters

string $type

if we're after a particular job list

int $includeUpUntil

The number of seconds to include jobs that have just finished, allowing a job list to be built that includes recently finished jobs

string getJobListFilter(string $type = null, int $includeUpUntil = 0)

Return the SQL filter used to get the job list - this is used by the UI for displaying the job list.

..

Parameters

string $type

if we're after a particular job list

int $includeUpUntil

The number of seconds to include jobs that have just finished, allowing a job list to be built that includes recently finished jobs

Return Value

string

runQueue(string $queue)

Process the job queue with the current queue runner

Parameters

string $queue

processJobQueue(string $name)

Process all jobs from a given queue

Parameters

string $name

The job queue to completely process

onShutdown()

When PHP shuts down, we want to process all of the immediate queue items

We use the 'getNextPendingJob' method, instead of just iterating the queue, to ensure we ignore paused or stalled jobs.