00001 <?php
00002
00003
00004
00005 class RescheduleSchedulerBaseCommand extends CommandChain {
00006
00007
00008
00009 protected function do_execute() {
00010 $ret = new Status();
00011
00012
00013 $task = $this->get_instance();
00014
00015 $err = $this->get_params();
00016
00017 if ($err->is_ok()) {
00018 $ret->merge($this->reschedule_success($task, $err));
00019 }
00020 else {
00021 $ret->merge($this->reschedule_error($task, $err));
00022 }
00023
00024 return $ret;
00025 }
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 protected function reschedule_success(DAOScheduler $task, Status $err) {
00036 $ret = new Status();
00037
00038 $policy = $task->reschedule_success;
00039 $params = $this->success_get_params($task, $err);
00040
00041 if ($this->do_reschedule($task, $policy, $params, Scheduler::STATUS_ACTIVE, $err)) {
00042 $ret->merge($this->success_on_rescheduled($task, $params, $err));
00043 }
00044 else {
00045 $ret->merge($this->success_on_finished($task, $params, $err));
00046 }
00047
00048 return $ret;
00049 }
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059 protected function success_get_params(DAOScheduler $task, Status $err) {
00060 return array(
00061 'runs_success' => $task->runs_success + 1,
00062 'runs_error' => 0,
00063 'error_message' => '',
00064 );
00065 }
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076 protected function success_on_rescheduled(DAOScheduler $task, $params, Status $err) {
00077 return new Status();
00078 }
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089 protected function success_on_finished(DAOScheduler $task, $params, Status $err) {
00090 $this->append(CommandsFactory::create_command($task, 'delete', false));
00091 return new Status();
00092 }
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102 protected function reschedule_error(DAOScheduler $task, Status $err) {
00103 $ret = new Status();
00104
00105 $policy = $task->reschedule_error;
00106 $params = $this->error_get_params($task, $err);
00107
00108 if ($this->do_reschedule($task, $policy, $params, Scheduler::STATUS_RESCHEDULED, $err)) {
00109 $ret->merge($this->error_on_rescheduled($task, $params, $err));
00110 }
00111 else {
00112 $ret->merge($this->error_on_give_up($task, $params, $err));
00113 }
00114
00115 return $ret;
00116 }
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126 protected function error_get_params(DAOScheduler $task, Status $err) {
00127 return array(
00128 'runs_success' => $task->runs_success,
00129 'runs_error' => $task->runs_error + 1,
00130 'error_message' => $err->to_string(Status::OUTPUT_PLAIN),
00131 );
00132 }
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143 protected function error_on_rescheduled(DAOScheduler $task, $params, Status $err) {
00144 return new Status();
00145 }
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156 protected function error_on_give_up(DAOScheduler $task, $params, Status $err) {
00157 $this->append(CommandsFactory::create_command($task, 'update', $params));
00158 $this->append(CommandsFactory::create_command($task, 'status', Scheduler::STATUS_ERROR));
00159 return new Status();
00160 }
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173 protected function do_reschedule(DAOScheduler $task, $policy, $params, $new_status, Status $err) {
00174 $ret = false;
00175
00176 $policy = strtolower($policy);
00177 Load::classes_in_directory('behaviour/scheduler/', $policy, 'rescheduler');
00178 $cls = 'Rescheduler' . ucfirst($policy);
00179 $rescheduler = new $cls();
00180
00181 $newdate = $rescheduler->reschedule($task, $err);
00182 if ($newdate !== false) {
00183
00184 $params['scheduledate'] = $newdate;
00185 $this->append(CommandsFactory::create_command($task, 'update', $params));
00186 $this->append(CommandsFactory::create_command($task, 'status', $new_status));
00187
00188 $ret = true;
00189 }
00190
00191 return $ret;
00192 }
00193
00194
00195
00196
00197 public function get_name() {
00198 return 'reschedule';
00199 }
00200
00201
00202
00203
00204 public function get_description() {
00205 return tr('Reschedule', 'scheduler');
00206 }
00207
00208 }