9 protected static $logClass =
"application.modules.job.models.Job";
32 public static function model($className=__CLASS__)
42 return array_merge(parent::rules(), array(
43 array(
'crontab',
'validateCrontab')
61 return $this->job_status_id;
75 catch (InvalidArgumentException $e)
77 $this->addError(
'crontab',
'crontab notation parsing failed');
109 if($identifiers && is_array($jobData))
111 foreach ($jobData as $name => $value)
113 if (isset($identifiers[$name]))
115 $column = $identifiers[$name];
116 $this->$column = $value;
122 if (!$this->create_time)
124 $this->create_time = $this->update_time;
126 $this->job_data = json_encode($jobData);
138 if ($this->scenario !=
'search')
140 $this->job_class = get_class($this);
153 throw new Exception(
"Unable to save job in beforeExecute");
181 catch (Exception $ex)
183 Yii::log(
"Exception during job onError (ID {$this->id}): ".$ex->getMessage(), CLogger::LEVEL_ERROR);
195 return $this->job_status_id;
203 $command =
'./yiic job execJob --name=' . $this->job_class .
' --id=' . $this->id;
204 shell_exec(sprintf(
'%s > /dev/null 2>&1 &', $command));
213 Yii::trace(
"execute", self::$logClass);
218 $errors = error_get_last();
219 if (!empty($errors)) {
221 $this->__finishMessage = array(
222 'job_data' => $this->job_data,
238 $this->__finishMessage = array(
239 'job_data' => $this->job_data,
240 'errors' => $e->getMessage(),
241 'trace' => $e->getTrace()
248 if ($this->rollbackOpenTransaction)
250 if ($transaction = Yii::app()->db->getCurrentTransaction())
251 $transaction->rollback();
262 Yii::trace(
"after execute, jobStatus = {$this->job_status_id}", self::$logClass);
273 $job->job_status_id = $this->job_status_id;
275 $job->update_time = $time;
276 $job->finish_time = $time;
286 $this->__finishMessage[
'log'] = $this->
getLog();
294 public function log($message)
296 $this->__log[] = $message;
323 if (!is_array(
$data))
326 foreach (
$data as $attribute => $value)
328 $this->$attribute = $value;
347 throw new Exception(
"Illegal call to _execute. This method should have been overwritten");
357 if ($timestamp === null)
362 return date(
"Y-m-d G:i:s", $timestamp);
370 if ($this->planned_time === null)
373 if ($this->planned_time === null)
386 $this->
setJobData(json_decode($this->job_data,
true));
417 Yii::trace(
"log result", self::$logClass);
421 $log->job_id = $this->id;
424 return Yii::log(
'Saving a job log failed: ' . var_export($log->errors,
true),
'error');
440 $finish = $log->finish_message;
441 if (is_string($finish))
442 $finish = json_decode($finish,
true);
444 if (isset($finish[
'errors']))
445 return $finish[
'errors'];