+ Add cron schedule for roadrunner.

This commit is contained in:
朱金勇
2023-06-28 07:17:33 +00:00
parent 182cae7fe0
commit 53b2deddde
4 changed files with 59 additions and 7 deletions
+54 -1
View File
@@ -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)
{
+3 -4
View File
@@ -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();
+2 -2
View File
@@ -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