function parseCron($path) { chdir($path); $crons = array(); $files = glob('*'); foreach ($files as $file) { $rows = file($file); foreach ($rows as $row) { $row = preg_replace("/[ \t]+/", ' ', trim($row, " \t\n")); $row = preg_replace("/#.*/", '', $row); if ($row) { preg_match_all('/(\\S+\\s+){5}|.*/', $row, $matchs); if ($matchs[0]) { $cron = array(); $cron['schema'] = trim($matchs[0][0]); $cron['command'] = trim($matchs[0][1]); $cron['cron'] = CronExpression::factory($cron['schema']); $cron['time'] = $cron['cron']->getNextRunDate(); $crons[] = $cron; } } } } return $crons; }
/** * @param string $hook The hook to trigger when this action runs * @param array $args Args to pass when the hook is triggered * @param int $first Unix timestamp for the first run * @param int $schedule A cron definition string * @param string $group A group to put the action in * * @return string The ID of the stored action */ public function cron($hook, $args = array(), $first = NULL, $schedule = NULL, $group = '') { if (empty($schedule)) { return $this->single($hook, $args, $first, $group); } $date = as_get_datetime_object($first); $cron = CronExpression::factory($schedule); $schedule = new ActionScheduler_CronSchedule($date, $cron); $action = new ActionScheduler_Action($hook, $args, $schedule, $group); return $this->store($action); }
public function test_cron_format() { $time = new DateTime('2014-01-01'); $cron = CronExpression::factory('0 0 10 10 *'); $schedule = new ActionScheduler_CronSchedule($time, $cron); $this->assertEquals(new DateTime('2014-10-10'), $schedule->next()); $cron = CronExpression::factory('0 0 L 1/2 *'); $schedule = new ActionScheduler_CronSchedule($time, $cron); $this->assertEquals(new DateTime('2014-01-31'), $schedule->next()); $this->assertEquals(new DateTime('2014-07-31'), $schedule->next(new DateTime('2014-06-01'))); $this->assertEquals(new DateTime('2028-11-30'), $schedule->next(new DateTime('2028-11-01'))); $cron = CronExpression::factory('30 14 * * MON#3 *'); $schedule = new ActionScheduler_CronSchedule($time, $cron); $this->assertEquals(new DateTime('2014-01-20 14:30:00'), $schedule->next()); $this->assertEquals(new DateTime('2014-05-19 14:30:00'), $schedule->next(new DateTime('2014-05-01'))); }
/** * Parse crons. * * @param array $crons * @access public * @return array */ public function parseCron($crons) { $this->app->loadClass('crontab', true); $parsedCron = array(); foreach ($crons as $cron) { $row = "{$cron->m} {$cron->h} {$cron->dom} {$cron->mon} {$cron->dow} {$cron->command}"; preg_match_all('/(\\S+\\s+){5}|.*/', $row, $matchs); if ($matchs[0]) { try { $parsedCron = array(); $parsedCron['schema'] = trim($matchs[0][0]); $parsedCron['command'] = trim($matchs[0][1]); $parsedCron['cron'] = CronExpression::factory($parsedCron['schema']); $parsedCron['time'] = $parsedCron['cron']->getNextRunDate(); $parsedCrons[$cron->id] = $parsedCron; } catch (InvalidArgumentException $e) { $this->dao->update(TABLE_CRON)->set('status')->eq('stop')->where('id')->eq($cron->id)->exec(); continue; } } } $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(); return $parsedCrons; }
private function validateSaveTasktemplate4Modify($request, array &$validateInfo) { /*{{{*/ $taskTemplate = DAL::get()->find('TaskTemplate', $request->tasktemplateid); if ($taskTemplate->queueTemplate->isAnyTime()) { /*{{{*/ if (trim($request->title) != $taskTemplate->getTrueTitle()) { $validateInfo['error'][] = '随意队列, 不能修改title'; } if (trim($request->crontimestr) != $taskTemplate->queueTemplate->cronTimeStr) { $validateInfo['error'][] = '随意队列, 不能修改时间配置'; } } /*}}}*/ if (CronExpression::isRight(trim($request->crontimestr)) && trim($request->crontimestr) != $taskTemplate->queueTemplate->cronTimeStr) { $nextRunDateStr = CronExpression::factory(trim($request->crontimestr))->getNextRunDate()->format('Y-m-d H:i:s'); $validateInfo['warning'][] = "此脚本下一次运行时间为{$nextRunDateStr}"; } if (trim($request->scriptparams) != '') { $paramArr = preg_split('#,\\s*#', $request->scriptparams); $cnt = count($paramArr); $paramStr = implode('、', $paramArr); if ($paramArr != $taskTemplate->scriptParams) { $validateInfo['warning'][] = "你输入的{$cnt}个参数分别{$paramStr}"; } } if (false == CronExpression::isRight(trim($request->crontimestr))) { $validateInfo['error'][] = '时间配置输入有误'; } $this->validateSaveTasktemplate4Common($request, $validateInfo); }
/** * Tests for known formatting problems * * @return null */ public function testIssue29() { $cron = CronExpression::factory('@weekly'); $this->assertEquals('2013-03-10 00:00:00', $cron->getPreviousRunDate('2013-03-17 00:00:00')->format('Y-m-d H:i:s')); }