diff --git a/bin/roadrunner/cron-worker.php b/bin/roadrunner/cron.php similarity index 100% rename from bin/roadrunner/cron-worker.php rename to bin/roadrunner/cron.php diff --git a/module/cron/control.php b/module/cron/control.php index 2aa3da6d0c..085bc408c6 100644 --- a/module/cron/control.php +++ b/module/cron/control.php @@ -120,6 +120,55 @@ class cron extends control return print(js::reload('parent')); } + /** + * Ajax run cron job. + * + * @access public + * @return void + */ + public function ajaxSchedule() + { + if(empty($this->config->global->cron)) return; + + /* Zand queue. */ + $zand = $this->app->loadClass('zand'); + $queue = $zand->connectQueue('crons'); + + /* Schedule loop. */ + $cronTimes = array(); + while(true) + { + /* Get and parse crons. */ + $crons = $this->cron->getCrons('nostop'); + $parsedCrons = $this->cron->parseCron($crons); + + $now = date(DT_DATETIME1); + foreach($parsedCrons as $id => $cron) + { + $cronInfo = $crons[$id]; + + /* Skip empty and stop cron.*/ + if(empty($cronInfo) or $cronInfo->status == 'stop') continue; + + if(!$cron['command'] || !isset($crons[$id])) continue; + + /* Check time. */ + $cronTime = $cron['time']->format(DT_DATETIME1); + if(!isset($cronTimes[$id])) $cronTimes[$id] = $cronTime; + + if($now < $cronTimes[$id]) continue; + + /* Push message into queue. */ + $message = array('id' => $id, 'type' => $crons[$id]->type, 'command' => $cron['command']); + $queue->push($message); + + $cronTimes[$id] = $cron['cron']->getNextRunDate()->format(DT_DATETIME1); + } + + sleep(10); + } + } + /** * Ajax exec cron. * @@ -129,12 +178,13 @@ class cron extends control */ public function ajaxExec($restart = false) { - if ('cli' !== PHP_SAPI) + if('cli' !== PHP_SAPI) { ignore_user_abort(true); set_time_limit(0); session_write_close(); } + /* Check cron turnon. */ if(empty($this->config->global->cron)) return; @@ -176,10 +226,13 @@ class cron extends control foreach($parsedCrons as $id => $cron) { $cronInfo = $this->cron->getById($id); + /* Skip empty and stop cron.*/ if(empty($cronInfo) or $cronInfo->status == 'stop') continue; + /* Skip cron that status is running and run time is less than max. */ if($cronInfo->status == 'running' and (time() - strtotime($cronInfo->lastTime)) < $this->config->cron->maxRunTime) continue; + /* Skip cron that last time is more than this cron time. */ if('cli' === PHP_SAPI) { diff --git a/module/cron/model.php b/module/cron/model.php index ae28602815..adc26282f3 100644 --- a/module/cron/model.php +++ b/module/cron/model.php @@ -76,7 +76,7 @@ class cronModel extends model } } } - $this->dao->update(TABLE_CRON)->set('lastTime')->eq(date(DT_DATETIME1))->where('lastTime')->eq('0000-00-00 00:00:00')->andWhere('status')->ne('stop')->exec(); + $this->dao->update(TABLE_CRON)->set('lastTime')->eq(date(DT_DATETIME1))->where('`lastTime` IS NULL')->andWhere('status')->ne('stop')->exec(); return $parsedCrons; } @@ -171,7 +171,7 @@ class cronModel extends model */ public function checkChange() { - $updatedCron = $this->dao->select('*')->from(TABLE_CRON)->where('lastTime')->eq('0000-00-00 00:00:00')->andWhere('status')->ne('stop')->fetch(); + $updatedCron = $this->dao->select('*')->from(TABLE_CRON)->where('`lastTime` IS NULL')->andWhere('status')->ne('stop')->fetch(); return $updatedCron ? true : false; } @@ -185,7 +185,6 @@ class cronModel extends model { $cron = fixer::input('post') ->add('status', 'normal') - ->add('lastTime', '0000-00-00 00:00:00') ->skipSpecial('m,h,dom,mon,dow,command') ->get(); @@ -217,7 +216,7 @@ class cronModel extends model public function update($cronID) { $cron = fixer::input('post') - ->add('lastTime', '0000-00-00 00:00:00') + ->add('lastTime', NULL) ->skipSpecial('m,h,dom,mon,dow,command') ->get(); diff --git a/module/cron/ui/index.html.php b/module/cron/ui/index.html.php index 81a1ce05d8..69664669a5 100644 --- a/module/cron/ui/index.html.php +++ b/module/cron/ui/index.html.php @@ -20,8 +20,8 @@ foreach($crons as $cron) if(!empty($cron->command) and common::hasPriv('cron', 'edit')) $actionsHtml .= html::a(inlink('edit', "id=$cron->id"), $lang->edit, '', "class='primary-500'"); if($cron->buildin == 0 and common::hasPriv('cron', 'delete')) $actionsHtml .= html::a('###', $lang->delete, '', "class='primary-500 ajaxDelete' data-id={$cron->id}"); - $cron->actions = $actionsHtml; - $cron->lastTime = substr($cron->lastTime, 2, 17); + $cron->actions = $actionsHtml; + if($cron->lastTime) $cron->lastTime = substr($cron->lastTime, 2, 17); } panel