00001 <?php
00002
00003
00004
00005
00006 class Scheduler {
00007 const STATUS_ACTIVE = 'ACTIVE';
00008 const STATUS_PROCESSING = 'PROCESSING';
00009 const STATUS_DISABLED = 'DISABLED';
00010 const STATUS_ERROR = 'ERROR';
00011 const STATUS_RESCHEDULED = 'RESCHEDULED';
00012
00013 const RESCHEDULE_TERMINATOR_1 = 'TERMINATOR1';
00014 const RESCHEDULE_TERMINATOR_2 = 'TERMINATOR2';
00015 const RESCHEDULE_TERMINATOR_3 = 'TERMINATOR3';
00016 const RESCHEDULE_DIE_HARD_1 = 'DIEHARD1';
00017 const RESCHEDULE_DIE_HARD_2 = 'DIEHARD2';
00018 const RESCHEDULE_DIE_HARD_3 = 'DIEHARD3';
00019 const RESCHEDULE_24_HOURS = '24HOURS';
00020 const RESCHEDULE_RUSHHOUR1 = 'RUSHHOUR1';
00021 const RESCHEDULE_RUSHHOUR2 = 'RUSHHOUR2';
00022 const RESCHEDULE_RUSHHOUR3 = 'RUSHHOUR3';
00023
00024 const REMOVE_LIKE = 'LIKE';
00025 const REMOVE_EXACT = '=';
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 public static function create_task($params, &$result, $exclusive = false) {
00037 $params['exclusive'] = $exclusive;
00038 $params['runs_error'] = 0;
00039 $params['runs_success'] = 0;
00040 $cmd = CommandsFactory::create_command('scheduler', 'create', $params);
00041 $ret = $cmd->execute();
00042 $result = $cmd->get_result();
00043 return $ret;
00044 }
00045
00046
00047
00048
00049
00050
00051 public static function get_statuses() {
00052 return array(
00053 self::STATUS_ACTIVE => tr(self::STATUS_ACTIVE, 'scheduler'),
00054 self::STATUS_PROCESSING => tr(self::STATUS_PROCESSING, 'scheduler'),
00055 self::STATUS_DISABLED => tr(self::STATUS_DISABLED, 'scheduler'),
00056 self::STATUS_ERROR => tr(self::STATUS_ERROR, 'scheduler'),
00057 self::STATUS_RESCHEDULED => tr(self::STATUS_RESCHEDULED, 'scheduler'),
00058 );
00059 }
00060
00061
00062
00063
00064
00065
00066 public static function get_reschedule_policies() {
00067 return array(
00068 self::RESCHEDULE_TERMINATOR_1 => tr(self::RESCHEDULE_TERMINATOR_1, 'scheduler'),
00069 self::RESCHEDULE_TERMINATOR_2 => tr(self::RESCHEDULE_TERMINATOR_2, 'scheduler'),
00070 self::RESCHEDULE_TERMINATOR_3 => tr(self::RESCHEDULE_TERMINATOR_3, 'scheduler'),
00071 self::RESCHEDULE_DIE_HARD_1 => tr(self::RESCHEDULE_DIE_HARD_1, 'scheduler'),
00072 self::RESCHEDULE_DIE_HARD_2 => tr(self::RESCHEDULE_DIE_HARD_2, 'scheduler'),
00073 self::RESCHEDULE_DIE_HARD_3 => tr(self::RESCHEDULE_DIE_HARD_3, 'scheduler'),
00074 self::RESCHEDULE_24_HOURS => tr(self::RESCHEDULE_24_HOURS, 'scheduler'),
00075 self::RESCHEDULE_RUSHHOUR1 => tr(self::RESCHEDULE_RUSHHOUR1, 'scheduler'),
00076 self::RESCHEDULE_RUSHHOUR2 => tr(self::RESCHEDULE_RUSHHOUR2, 'scheduler'),
00077 self::RESCHEDULE_RUSHHOUR3 => tr(self::RESCHEDULE_RUSHHOUR3, 'scheduler'),
00078 );
00079 }
00080
00081
00082
00083
00084
00085
00086 public static function create_adapter() {
00087 $dao = new DAOScheduler();
00088 $dao->sort('scheduledate', ISearchAdapter::ASC);
00089 return $dao;
00090 }
00091
00092
00093
00094
00095
00096
00097 public static function get_all() {
00098 $dao = self::create_adapter();
00099 return $dao->find_array();
00100 }
00101
00102
00103
00104
00105
00106
00107 public static function get_next_task() {
00108 DB::start_trans();
00109
00110 $dao = self::create_adapter();
00111 $dao->add_where('status', DBWhere::OP_IN, array(self::STATUS_ACTIVE, self::STATUS_RESCHEDULED));
00112 $dao->add_where('scheduledate', '<=', time());
00113 $dao->limit(0, 1);
00114 $query = $dao->create_select_query();
00115 $query->set_policy(DBQuerySelect::FOR_UPDATE);
00116
00117 $ret = false;
00118 if ($dao->query($query->get_sql(), DAOScheduler::AUTOFETCH)) {
00119 $dao->status = self::STATUS_PROCESSING;
00120 $dao->update();
00121 $ret = clone($dao);
00122 }
00123
00124 DB::commit();
00125 return $ret;
00126 }
00127
00128
00129
00130
00131
00132
00133
00134 public static function reschedule(DAOScheduler $task) {
00135 $cmd = CommandsFactory::create_command($task, 'reschedule', false);
00136 return $cmd->execute();
00137 }
00138
00139
00140
00141
00142
00143
00144
00145
00146 public static function remove_tasks($action, $op = self::REMOVE_EXACT) {
00147 $ret = new Status();
00148
00149 Load::commands('generics/massdelete');
00150 $db_op = '=';
00151 switch ($op) {
00152 case self::REMOVE_LIKE:
00153 $action .= '%';
00154 $db_op = DBWhere::OP_LIKE;
00155 break;
00156 default:
00157 break;
00158 }
00159 $where = new DBWhere(new DAOScheduler(), 'action', $db_op, $action);
00160 $cmd = new MassDeleteCommand('scheduler', array($where));
00161 $ret->merge($cmd->execute());
00162
00163 return $ret;
00164 }
00165 }
00166