/** * Factory for loading minion tasks * * @param array An array of command line options. It should contain the 'task' key * @throws Minion_Exception_InvalidTask * @return Minion_Task The Minion task */ public static function factory($options) { if (($task = Arr::get($options, 'task')) !== NULL) { unset($options['task']); } else { if (($task = Arr::get($options, 0)) !== NULL) { // The first positional argument (aka 0) may be the task name unset($options[0]); } else { // If we didn't get a valid task, generate the help $task = 'help'; } } $class = Minion_Task::convert_task_to_class_name($task); if (!class_exists($class)) { throw new Minion_Exception_InvalidTask("Task ':task' is not a valid minion task", array(':task' => $class)); } $class = new $class(); if (!$class instanceof Minion_Task) { throw new Minion_Exception_InvalidTask("Task ':task' is not a valid minion task", array(':task' => $class)); } $class->set_options($options); // Show the help page for this task if requested if (array_key_exists('help', $options)) { $class->_method = '_help'; } return $class; }
/** * * @param Model_Job $job * @return void */ public function run(Model_Job $job) { if (Kohana::$profiling === TRUE) { $benchmark = Profiler::start('Rub job', $job->name); } $this->values(array('job_id' => $job->id))->create(); $this->set_status(Model_Job::STATUS_RUNNING); try { $job = $job->job; $minion_task = Minion_Task::convert_task_to_class_name($job); if (is_array($job)) { $passed = call_user_func($job); } elseif (class_exists($minion_task)) { Minion_Task::factory(array($job))->execute(); $passed = TRUE; } elseif (strpos($job, '::') === FALSE) { $function = new ReflectionFunction($job); $passed = $function->invoke(); } else { list($class, $method) = explode('::', $job, 2); $method = new ReflectionMethod($class, $method); $passed = $method->invoke(NULL); } } catch (Exception $e) { $this->failed(); return; } $this->complete(); if (isset($benchmark)) { Profiler::stop($benchmark); } }
/** * Tests that a task can be converted to a class name * * @test * @covers Minion_Task::convert_task_to_class_name * @dataProvider provider_convert_task_to_class_name * @param string Expected class name * @param string Input task name */ public function test_convert_task_to_class_name($expected, $task_name) { $this->assertSame($expected, Minion_Task::convert_task_to_class_name($task_name)); }