public function executeJob(Job $job) { $this->out(sprintf(' - <info>%s</info>', $job->getWorker()), 1, Shell::VERBOSE); $this->out(' - Executing job', 1, Shell::VERBOSE); $this->djLog(__('Executing: {0}', $job->getId())); $start = microtime(true); $response = JobManager::instance()->execute($job, $this->param('force')); if ($response instanceof \Throwable) { $this->_failedJob($job, $response); } else { $this->djLog(__('Done with: {0}', $job->getId())); $this->out(sprintf('<success> - Execution successful</success> :: <info>%s</info>', $response), 1, Shell::VERBOSE); } $end = microtime(true); $this->out(sprintf(' - Took: %.2f seconds', $end - $start), 1, Shell::VERBOSE); }
/** * Checks if there already is a job with the same worker waiting * * @param \DelayedJobs\DelayedJob\Job $job Job to check * @return bool */ public function isSimilarJob(Job $job) { $quoting = $this->connection()->driver()->autoQuoting(); $this->connection()->driver()->autoQuoting(true); $conditions = ['worker' => $job->getWorker(), 'status IN' => [Job::STATUS_BUSY, Job::STATUS_NEW, Job::STATUS_FAILED, Job::STATUS_UNKNOWN]]; if (!empty($job->getId())) { $conditions['id !='] = $job->getId(); } $exists = $this->exists($conditions); $this->connection()->driver()->autoQuoting($quoting); return $exists; }
public function execute(Job $job, $force = false) { $className = App::className($job->getWorker(), 'Worker', 'Worker'); if (!class_exists($className)) { throw new JobExecuteException("Worker does not exist (" . $className . ")"); } $jobWorker = new $className(); if (!$jobWorker instanceof JobWorkerInterface) { throw new JobExecuteException("Worker class '{$className}' does not follow the required 'JobWorkerInterface"); } $this->djLog(__('Received job {0}.', $job->getId())); $event = $this->dispatchEvent('DelayedJob.beforeJobExecute', [$job]); if ($event->isStopped()) { //@TODO: Requeue job if queueable job return $event->result; } if ($force === false && ($job->getStatus() === Job::STATUS_SUCCESS || $job->getStatus() === Job::STATUS_BURRIED)) { $this->djLog(__('Job {0} has already been processed', $job->getId())); $this->getMessageBroker()->ack($job); return true; } if ($force === false && $job->getStatus() === Job::STATUS_BUSY) { $this->djLog(__('Job {0} has already being processed', $job->getId())); $this->getMessageBroker()->ack($job); return true; } $this->lock($job); $event = null; $result = false; $start = microtime(true); try { $result = $jobWorker($job); $duration = round((microtime(true) - $start) * 1000); $this->completed($job, $result, $duration); } catch (\Error $error) { //## Job Failed badly $result = $error; $this->failed($job, $error, true); Log::emergency(sprintf("Delayed job %d failed due to a fatal PHP error.\n%s\n%s", $job->getId(), $error->getMessage(), $error->getTraceAsString())); } catch (\Exception $exc) { //## Job Failed $result = $exc; $this->failed($job, $exc, $exc instanceof NonRetryableException); } finally { $this->getMessageBroker()->ack($job); $duration = $duration ?? round((microtime(true) - $start) * 1000); $this->dispatchEvent('DelayedJob.afterJobExecute', [$job, $result, $duration]); } return $result; }
public function beforeExecute(Event $event, Job $job) { if ($this->_worker && ($this->_worker->status === WorkersTable::STATUS_SHUTDOWN || $this->_worker->status === WorkersTable::STATUS_TO_KILL)) { $event->stopPropagation(); return false; } cli_set_process_title(sprintf('DJ Worker :: %s :: Working %s', $this->_workerId, $job->getId())); $this->out(__('<success>Starting job:</success> {0} :: ', $job->getId()), 1, Shell::VERBOSE); $this->out(sprintf(' - <info>%s</info>', $job->getWorker()), 1, Shell::VERBOSE); $this->out(' - Executing job', 1, Shell::VERBOSE); $job->setHostName($this->_hostName); pcntl_signal_dispatch(); $this->_timeLastJob = microtime(true); return true; }