This repository has been archived on 2024-04-08. You can view files and clone it, but cannot push or open issues or pull requests.
khosb/modules/task/classes/Model/Task.php

108 lines
2.5 KiB
PHP
Raw Normal View History

2011-08-16 02:27:19 +00:00
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* This class supports Tasks
*
2013-03-19 22:35:19 +00:00
* @package Task
2011-08-16 02:27:19 +00:00
* @category Models
* @author Deon George
2013-03-19 22:35:19 +00:00
* @copyright (c) 2009-2013 Open Source Billing
2011-08-16 02:27:19 +00:00
* @license http://dev.osbill.net/license.html
*/
class Model_Task extends ORM_OSB {
2011-08-16 02:27:19 +00:00
protected $_display_filters = array(
'date_run'=>array(
array('Config::datetime',array(':value')),
),
);
public function run($force=FALSE) {
2011-08-16 02:27:19 +00:00
$r = rand(0,9999);
2012-11-09 23:13:57 +00:00
$tlo = ORM::factory('Task_Log');
2011-08-16 02:27:19 +00:00
$tlo->task_id = $this->id;
if (! $this->loaded())
$tlo->message = sprintf('Unknown Task ID %s',$this->id);
elseif (! $this->status)
$tlo->message = sprintf('Task %s is not active',$this->id);
elseif ($this->running AND ! $force)
2011-08-16 02:27:19 +00:00
$tlo->message = sprintf('Task %s is already running',$this->id);
elseif (! preg_match('/\//',$this->command))
$tlo->message = sprintf('Task %s uses the old configuration, ignoring :command',$this->id,$this->command);
else {
try {
// Get a lock
$this->running = 1;
$this->running_host = $r;
$this->save();
// Check we are the winning host to run this task
// @todo We need to test that the lock is not stale
2011-08-16 02:27:19 +00:00
$this->reload();
if ($this->running_host != $r)
return;
switch ($this->type) {
case 0:
$r = Request::factory($this->command)->execute();
break;
default:
throw new Kohana_Exception('Unknown task type :type',array(':type'=>$this->type));
}
// Clear our lock and update the last run time
$this->date_run = time();
$this->running = 0;
$this->running_host = NULL;
$tlo->result = 0;
$tlo->message = $r->body();
}
catch (Exception $e) {
$tlo->result = $e->getCode();
$tlo->message = Kohana_Exception::text($e);
2011-08-25 23:21:39 +00:00
$this->running = 1;
$this->running_host = 'ERROR';
2011-08-16 02:27:19 +00:00
}
$this->save();
2011-08-16 02:27:19 +00:00
}
if ($this->log)
$tlo->save();
}
/** LIST FUNCTIONS **/
public function list_active() {
$result = array();
2011-08-16 02:27:19 +00:00
2012-08-01 12:43:33 +00:00
foreach ($this->_where_active()->find_all() as $to) {
2011-08-16 02:27:19 +00:00
$ct = sprintf('%s %s %s %s %s',$to->int_min,$to->int_hour,$to->int_month_day,$to->int_month,$to->int_week_day);
$c = new Cron($ct,$to->command);
$result[$to->id]['task'] = $to;
$result[$to->id]['next'] = $c->next($to->date_run);
2011-08-16 02:27:19 +00:00
}
return $result;
2011-08-16 02:27:19 +00:00
}
public function list_next() {
$result = array();
2011-08-16 02:27:19 +00:00
foreach ($this->list_active() as $v)
if ((! $result OR $v['next']<$result['next']) AND ! $v['task']->running)
$result = $v;
2011-08-16 02:27:19 +00:00
return array($result['task']->id=>$result);
2011-08-16 02:27:19 +00:00
}
}
?>