31 protected static $logClass =
"application.modules.job.components.JobManager";
40 public function addJob(
Job $job, $checkEnqueuedDuplicate =
true)
42 if ($checkEnqueuedDuplicate) {
60 Yii::trace(
"Exec job directly, class = ".get_class($job));
68 throw new Exception(
"Job execution was not successful: ".print_r($job->
getFinishMessage(),
true));
82 throw new Exception(
'Job needs to define a class');
95 $model->job_class =
$class;
97 $model->setJobData($jobData);
112 throw new Exception(
'Job needs to define a class');
116 throw new Exception(
'Job needs to define a crontab value');
121 $models =
Job::model()->findAll(
't.job_class=:job_class AND t.crontab IS NOT NULL', array(
':job_class' =>
$attributes[
'class']));
123 foreach ($models as $compareModel) {
124 if ($model->isDuplicateOf($compareModel)) {
125 $model = $compareModel;
136 Yii::trace(
"Setting new time");
137 $model->calculateNextPlannedTime();
140 $this->
addJob($model,
true);
164 $tx = Yii::app()->db->beginTransaction();
171 $sql =
"SELECT * FROM job WHERE planned_time <= '{$now}' AND job_status_id = {$jobStatus}";
173 $sql .=
" LIMIT 1 FOR UPDATE";
179 $job->beforeExecute();
183 }
catch (Exception $ex) {
185 Yii::log(
"Error in finding job: ".$ex->getMessage());
189 if ($this->executeAsync) {
190 $job->executeAsync();
206 $this->raiseEvent(
'onBeforeExecute', $event);
214 $this->raiseEvent(
'onAfterExecute', $event);
227 Yii::log(
'Saving a job failed: '.print_r($job->errors,
true),
'error');
244 if ($timestamp === null) {
248 return date(
"Y-m-d G:i:s", $timestamp);
260 $criteria =
new CDbCriteria(array(
261 'condition' =>
'job_class=:job_class AND job_status_id=:job_status_id',
262 'params' => array(
':job_class' => $job->job_class,
':job_status_id' =>
JobStatus::ENQUEUED),
278 Yii::trace(
"Clear component: $id");
279 Yii::app()->setComponent($id, null);
304 if (is_array($filterClass)) {
305 $filterClass = $filterClass[0];
308 $model = $class::model();
311 $filterModel = $filterClass::model();
313 $identifierAttributes = array();
315 if (!$model->hasAttribute($name)) {
316 $identifiers = array_flip($filterModel->identifiers());
317 if (isset($identifiers[$name])) {
319 $column = $identifiers[$name];
320 $identifierAttributes[$column] = $value;
322 throw new Exception(
"Attribute {$name} not defined on ($filterClass} or mapped as identifier, defined identifiers: ".print_r($identifiers,
true));
327 return array_merge(
$attributes, $identifierAttributes);
342 $model = $class::model();
344 return $model->findAllByAttributes(
$attributes, $condition);
357 throw new Exception(
'Job needs to define a class');
361 exec(
'ps ax | grep "name="'.escapeshellarg(
$attributes[
'class']), $processes);
362 $cntProcesses = count($processes) - 1;
364 if ($cntProcesses > 0 &&
368 Yii::trace(
'Limit of executed processes: '.
$attributes[
'limit']);