public function create(Job $job) { $class = $job->getClass(); if (!class_exists($class)) { throw new ClassNotFoundException('Job class [ ' . $class . ' ] does not exist'); } $reflect = new \ReflectionClass($class); if (!$reflect->implementsInterface(self::JOB_INTERFACE_NAME)) { throw new ClassNotFoundException('Job class [ ' . $class . ' ] does not implement ' . self::JOB_INTERFACE_NAME); } $instance = $reflect->newInstanceArgs($this->constructorArguments); $instance->setJob($job); return $instance; }
public function perform() { // Make sure we catch and process fatal errors from within the job register_shutdown_function(function () { if (!($e = error_get_last()) || !in_array($e['type'], array(E_ERROR, E_CORE_ERROR, E_COMPILE_ERROR, E_PARSE))) { return; } $this->logger->error('Worker (fork): Job [ ' . $this->job->getId() . ' ] failed fatally with message: [ ' . $e['message'] . ' ]'); $this->host->failJob($this->job, new \ErrorException($e['message'], $e['type'], 0, $e['file'], $e['line'])); }); try { $this->jobCreator->create($this->job)->perform(); } catch (\Exception $e) { $this->logger->error('Worker (fork): Job [ ' . $this->job->getId() . ' ] failed with message: [ ' . $e->getMessage() . ' ]'); $this->host->failJob($this->job, $e); throw $e; } $this->host->completeJob($this->job); $this->logger->debug('Worker (fork): Job [ ' . $this->job->getId() . ' ] completed successfully'); return 0; }
/** * @returns Job */ public function getJob() { if ($this->isSleeping()) { return; } $hostId = $this->host->getId(); foreach ($this->queues as $queue) { if ($job = $queue->nextJob($hostId)) { return Job::fromArray($job); } } $this->sleepFor(self::JOB_MISS_SLEEP_SECONDS); }
/** * @param Job $job * @param Exception $error * @return $this */ public function failJob(Job $job, Exception $error) { $this->complete($job->getId(), $error); return $this; }