Enhancements to Task

This commit is contained in:
Deon George
2013-05-02 19:52:19 +10:00
parent 04ebda2aaa
commit e73ff93a70
8 changed files with 162 additions and 122 deletions

View File

@@ -11,25 +11,20 @@
*/
class Task_Task_Listactive extends Task {
protected function _execute(array $params) {
$output = '';
$header = "%2s %30s %21s %21s %40s\n";
$output .= sprintf('%2s %30s %21s %21s %40s',
'ID','Command','Last Run','Next Run','Description');
$output .= "\n";
$output = sprintf($header,'ID','Command','Last Run','Next Run','Description');
foreach (ORM::factory('Task')->list_active() as $t) {
$output .= sprintf('%2s %30s %21s %21s %40s',
$t['task']->id,
$t['task']->command,
$t['task']->display('date_run'),
Config::datetime($t['next']),
$t['task']->display('description')
foreach (ORM::factory('Task')->list_active() as $to)
$output .= sprintf($header,
$to->id,
$to->command,
$to->display('date_run'),
$to->next_run(TRUE),
$to->display('description')
);
$output .= "\n";
};
echo $output;
return $output;
}
}
?>

View File

@@ -18,29 +18,85 @@ class Task_Task_Run extends Task {
if (! $to->status)
throw new Minion_Exception_InvalidTask('Task :task (:name) NOT active',array(':task'=>$params['id'],':name'=>$to->name));
if (! Kohana::$config->load('debug')->task_sim)
$to->run($params['force']);
else
printf('Would Run task: (%s) %s',$params['id'],$to->name);
echo "\n";
Kohana::$config->load('debug')->task_sim ? printf("Would Run task: (%s) %s\n",$to->id,$to->name) : $this->_run($to,$params['force']);
} else
throw new Minion_Exception_InvalidTask('Unknown task :task',array(':task'=>$params['id']));
} else {
$tlo = ORM::factory('Task');
$t = time();
foreach ($tlo->list_active() as $to)
if ($to['next'] < $t) {
if (! Kohana::$config->load('debug')->task_sim)
$to['task']->run();
else
printf('Would Run task: (%s) %s',$to['task']->id,$to['task']->name);
echo "\n";
}
foreach (ORM::factory('Task')->list_active() as $to)
if ($to->next_run() < $t)
Kohana::$config->load('debug')->task_sim ? printf("Would Run task: (%s) %s\n",$to->id,$to->name) : $this->_run($to);
}
}
private function _run(Model_Task $to,$force=FALSE) {
$r = rand(0,9999);
$tlo = ORM::factory('Task_Log');
$tlo->task_id = $to->id;
if ($to->running AND ! $force)
$tlo->message = sprintf('Task %s is already running',$to->id);
else {
try {
// Get a lock
$to->running = 1;
$to->running_host = $r;
$to->save();
// Check we are the winning host to run this task
// @todo We need to test that the lock is not stale
$to->reload();
if ($to->running_host != $r)
return;
switch ($to->type) {
case 1:
$r = Minion_Task::factory(array('site'=>Minion_CLI::options('site'),'task'=>$to->command))->execute();
$to->running = 0;
$to->running_host = NULL;
$tlo->message = $r;
$tlo->result = 0;
break;
case 0:
$r = Request::factory($to->command)->execute();
if ($r->status() == 200) {
$to->running = 0;
$to->running_host = NULL;
}
$tlo->message = $r->body();
$tlo->result = $r->status();
break;
default:
throw new Kohana_Exception('Unknown task type :type',array(':type'=>$to->type));
}
// Clear our lock and update the last run time
$to->date_run = time();
} catch (Exception $e) {
$tlo->result = $e->getCode();
$tlo->message = Kohana_Exception::text($e);
$to->running = 1;
$to->running_host = 'ERROR';
}
$to->save();
}
if ($to->log)
$tlo->save();
}
}
?>