/** * Adds a job to the queue to be started * * Relevant data about the job will be persisted using a QueuedJobDescriptor * * @param QueuedJob $job * The job to start. * @param $startAfter * The date (in Y-m-d H:i:s format) to start execution after * @param int $userId * The ID of a user to execute the job as. Defaults to the current user */ public function queueJob(QueuedJob $job, $startAfter = null, $userId = null) { $signature = $job->getSignature(); // see if we already have this job in a queue $filter = array('Signature =' => $signature, 'JobStatus =' => QueuedJob::STATUS_NEW); $existing = DataObject::get('QueuedJobDescriptor', singleton('QJUtils')->quote($filter)); if ($existing && $existing->Count()) { return $existing->First()->ID; } $jobDescriptor = new QueuedJobDescriptor(); $jobDescriptor->JobTitle = $job->getTitle(); $jobDescriptor->JobType = $job->getJobType(); $jobDescriptor->Signature = $signature; $jobDescriptor->Implementation = get_class($job); $jobDescriptor->StartAfter = $startAfter; $jobDescriptor->RunAsID = $userId ? $userId : Member::currentUserID(); // copy data $this->copyJobToDescriptor($job, $jobDescriptor); $jobDescriptor->write(); return $jobDescriptor->ID; }
/** * Adds a job to the queue to be started * * Relevant data about the job will be persisted using a QueuedJobDescriptor * * @param QueuedJob $job * The job to start. * @param $startAfter * The date (in Y-m-d H:i:s format) to start execution after * @param int $userId * The ID of a user to execute the job as. Defaults to the current user * @return int */ public function queueJob(QueuedJob $job, $startAfter = null, $userId = null, $queueName = null) { $signature = $job->getSignature(); // see if we already have this job in a queue $filter = array('Signature' => $signature, 'JobStatus' => array(QueuedJob::STATUS_NEW, QueuedJob::STATUS_INIT)); $existing = DataList::create('QueuedJobDescriptor')->filter($filter)->first(); if ($existing && $existing->ID) { return $existing->ID; } $jobDescriptor = new QueuedJobDescriptor(); $jobDescriptor->JobTitle = $job->getTitle(); $jobDescriptor->JobType = $queueName ? $queueName : $job->getJobType(); $jobDescriptor->Signature = $signature; $jobDescriptor->Implementation = get_class($job); $jobDescriptor->StartAfter = $startAfter; $jobDescriptor->RunAsID = $userId ? $userId : Member::currentUserID(); // copy data $this->copyJobToDescriptor($job, $jobDescriptor); $jobDescriptor->write(); $this->startJob($jobDescriptor, $startAfter); return $jobDescriptor->ID; }
/** * Adds a job to the queue to be started * * Relevant data about the job will be persisted using a QueuedJobDescriptor * * @param QueuedJob $job * The job to start. * @param $startAfter * The date (in Y-m-d H:i:s format) to start execution after * @param int $userId * The ID of a user to execute the job as. Defaults to the current user */ public function queueJob(QueuedJob $job, $startAfter = null, $userId = null, $queueName = null) { $signature = $job->getSignature(); // see if we already have this job in a queue $filter = array('Signature' => $signature, 'JobStatus' => QueuedJob::STATUS_NEW); $existing = DataList::create('QueuedJobDescriptor')->filter($filter)->first(); if ($existing && $existing->ID) { return $existing->ID; } $jobDescriptor = new QueuedJobDescriptor(); $jobDescriptor->JobTitle = $job->getTitle(); $jobDescriptor->JobType = $queueName ? $queueName : $job->getJobType(); $jobDescriptor->Signature = $signature; $jobDescriptor->Implementation = get_class($job); $jobDescriptor->StartAfter = $startAfter; $jobDescriptor->RunAsID = $userId ? $userId : Member::currentUserID(); // copy data $this->copyJobToDescriptor($job, $jobDescriptor); $jobDescriptor->write(); if ($startAfter && strtotime($startAfter) > time()) { } else { // immediately start it on the queue, however that works $this->queueHandler->startJobOnQueue($jobDescriptor); } return $jobDescriptor->ID; }