/** * getInstance * * @return null|object * * @throws Resque_Exception */ public function getInstance() { if (!is_null($this->instance)) { return $this->instance; } if (class_exists('Resque_Job_Creator')) { $this->instance = \Resque_Job_Creator::createJob($this->payload['class'], $this->getArguments()); } else { try { $this->instance = $this->getServiceLocator()->get($this->payload['class']); $this->instance->job = $this; $this->instance->args = $this->getArguments(); $this->instance->queue = $this->queue; if (!method_exists($this->instance, 'perform')) { throw new Resque_Exception('Job class ' . $this->payload['class'] . ' does not contain a perform method.'); } } catch (Exception $e) { throw new Resque_Exception($e->getMessage()); } } return $this->instance; }
/** * Create and return a job instance * * @param string $className className of the job to instanciate * @return object $args a job instance * @throws Resque_Exception when the class is not found, or does not follow the job file convention */ public static function createJob($className, $args) { list($plugin, $model) = pluginSplit($className); if (self::$rootFolder === null) { self::$rootFolder = dirname(dirname(dirname(__DIR__))) . DS; } $classpath = self::$rootFolder . (empty($plugin) ? '' : 'Plugin' . DS . $plugin . DS) . 'Console' . DS . 'Command' . DS . $model . '.php'; if (file_exists($classpath)) { require_once $classpath; } else { throw new Resque_Exception('Could not find job class ' . $className . '.'); } if (!class_exists($model)) { throw new Resque_Exception('Could not find job class ' . $className . '.'); } if (!method_exists($model, 'perform')) { throw new Resque_Exception('Job class ' . $className . ' does not contain a perform method.'); } if (!isset($args[0]) || !method_exists($model, $args[0])) { throw new Resque_Exception('Job class ' . $className . ' does not contain ' . $args[0] . ' method.'); } return new $model(); }
/** * Get the instantiated object for this job that will be performing work. * * @return object Instance of the object that this job belongs to. */ public function getInstance() { if (!is_null($this->instance)) { return $this->instance; } if (class_exists('Resque_Job_Creator')) { $this->instance = Resque_Job_Creator::createJob($this->payload['class'], $this->getArguments()); } else { if (!class_exists($this->payload['class'])) { throw new \Resque_Exception('Could not find job class ' . $this->payload['class'] . '.'); } if (!method_exists($this->payload['class'], 'perform')) { throw new \Resque_Exception('Job class ' . $this->payload['class'] . ' does not contain a perform method.'); } $this->instance = new $this->payload['class'](); } $this->instance->job = $this; $this->instance->args = $this->getArguments(); $this->instance->queue = $this->queue; return $this->instance; }
/** * Test job creation from a valid shell class, but without the expected method * * @expectedException Resque_Exception */ public function testJobWithErrorOnInexistingFunction() { Resque_Job_Creator::createJob('JobClassOneShell', ['funcThree']); }