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 = [
            'Signature' => $signature,
            'JobStatus' => [
                QueuedJob::STATUS_NEW,
                QueuedJob::STATUS_INIT,
            ],
        ];

        $existing = QueuedJobDescriptor::get()
            ->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;

        $runAsID = 0;
        if ($userId) {
            $runAsID = $userId;
        } elseif (Security::getCurrentUser() && Security::getCurrentUser()->exists()) {
            $runAsID = Security::getCurrentUser()->ID;
        }
        $jobDescriptor->RunAsID = $runAsID;

        // copy data
        $this->copyJobToDescriptor($job, $jobDescriptor);

        $jobDescriptor->write();

        $this->startJob($jobDescriptor, $startAfter);

        return $jobDescriptor->ID;
    }
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 = [
            'Signature' => $signature,
            'JobStatus' => [
                QueuedJob::STATUS_NEW,
                QueuedJob::STATUS_INIT,
            ],
        ];

        $existing = QueuedJobDescriptor::get()
            ->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;

        $runAsID = 0;
        if ($userId) {
            $runAsID = $userId;
        } elseif (Security::getCurrentUser() && Security::getCurrentUser()->exists()) {
            $runAsID = Security::getCurrentUser()->ID;
        }
        $jobDescriptor->RunAsID = $runAsID;

        // copy data
        $this->copyJobToDescriptor($job, $jobDescriptor);

        $jobDescriptor->write();

        $this->startJob($jobDescriptor, $startAfter);

        return $jobDescriptor->ID;
    }
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 = [ 'Signature' => $ signature , 'JobStatus' => [ QueuedJob :: STATUS_NEW , QueuedJob :: STATUS_INIT , ] , ] ; $ existing = QueuedJobDescriptor :: get ( ) -> 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 ; $ runAsID = 0 ; if ( $ userId ) { $ runAsID = $ userId ; } elseif ( Security :: getCurrentUser ( ) && Security :: getCurrentUser ( ) -> exists ( ) ) { $ runAsID = Security :: getCurrentUser ( ) -> ID ; } $ jobDescriptor -> RunAsID = $ runAsID ; // 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

@return int
Adds a job to the queue to be started