/** * 解析crontab的定时格式,linux只支持到分钟/,这个类支持到秒 * @param string $crontab_string : * * 0 1 2 3 4 5 * * * * * * * * - - - - - - * | | | | | | * | | | | | +----- day of week (0 - 6) (Sunday=0) * | | | | +----- month (1 - 12) * | | | +------- day of month (1 - 31) * | | +--------- hour (0 - 23) * | +----------- min (0 - 59) * +------------- sec (0-59) * @param int $start_time timestamp [default=current timestamp] * @return int unix timestamp - 下一分钟内执行是否需要执行任务,如果需要,则把需要在那几秒执行返回 * @throws InvalidArgumentException 错误信息 */ public static function parse($crontab_string, $start_time = null) { if (is_array($crontab_string)) { return self::_parse_array($crontab_string, $start_time); } if (!preg_match('/^((\\*(\\/[0-9]+)?)|[0-9\\-\\,\\/]+)\\s+((\\*(\\/[0-9]+)?)|[0-9\\-\\,\\/]+)\\s+((\\*(\\/[0-9]+)?)|[0-9\\-\\,\\/]+)\\s+((\\*(\\/[0-9]+)?)|[0-9\\-\\,\\/]+)\\s+((\\*(\\/[0-9]+)?)|[0-9\\-\\,\\/]+)\\s+((\\*(\\/[0-9]+)?)|[0-9\\-\\,\\/]+)$/i', trim($crontab_string))) { if (!preg_match('/^((\\*(\\/[0-9]+)?)|[0-9\\-\\,\\/]+)\\s+((\\*(\\/[0-9]+)?)|[0-9\\-\\,\\/]+)\\s+((\\*(\\/[0-9]+)?)|[0-9\\-\\,\\/]+)\\s+((\\*(\\/[0-9]+)?)|[0-9\\-\\,\\/]+)\\s+((\\*(\\/[0-9]+)?)|[0-9\\-\\,\\/]+)$/i', trim($crontab_string))) { self::$error = "Invalid cron string: " . $crontab_string; return false; } } if ($start_time && !is_numeric($start_time)) { self::$error = "\$start_time must be a valid unix timestamp ({$start_time} given)"; return false; } $cron = preg_split("/[\\s]+/i", trim($crontab_string)); $start = empty($start_time) ? time() : $start_time; if (count($cron) == 6) { $date = array('second' => self::_parse_cron_number($cron[0], 0, 59), 'minutes' => self::_parse_cron_number($cron[1], 0, 59), 'hours' => self::_parse_cron_number($cron[2], 0, 23), 'day' => self::_parse_cron_number($cron[3], 1, 31), 'month' => self::_parse_cron_number($cron[4], 1, 12), 'week' => self::_parse_cron_number($cron[5], 0, 6)); } elseif (count($cron) == 5) { $date = array('second' => array(1 => 1), 'minutes' => self::_parse_cron_number($cron[0], 0, 59), 'hours' => self::_parse_cron_number($cron[1], 0, 23), 'day' => self::_parse_cron_number($cron[2], 1, 31), 'month' => self::_parse_cron_number($cron[3], 1, 12), 'week' => self::_parse_cron_number($cron[4], 0, 6)); } if (in_array(intval(date('i', $start)), $date['minutes']) && in_array(intval(date('G', $start)), $date['hours']) && in_array(intval(date('j', $start)), $date['day']) && in_array(intval(date('w', $start)), $date['week']) && in_array(intval(date('n', $start)), $date['month'])) { return $date['second']; } return null; }
static function load_config() { $time = time(); $config = (include ROOT_PATH . DS . 'config.php'); foreach ($config as $id => $task) { // 先支持秒(, *) 这两种 $ret = ParseCrontab::parse($task["time"], $time); // 设置每秒的任务 foreach ($ret as $sec) { self::$turntable[$sec][$id] = $task; } } }
/** * 检查时间是否运行 * * @param array $time * @param int */ protected function getRunTime(array $conf) { $timestamp = time(); if (isset($conf['interval'])) { return ParseInterval::parse($conf, $timestamp); } elseif (isset($conf['crontab'])) { //var_dump($conf['crontab']); //while (true) { //$t = microtime(true); $time = ParseCrontab::parse($conf, $timestamp); //echo "nowIs :" . date('Y-m-d H:i:s', $timestamp) . "\t" . $time . " : " . date('Y-m-d H:i:s', $time) . "\tuseTime:" . // (microtime(true) - $t) . "\n"; return $time; //$timestamp += 1; //usleep(50000); //} } return 9999999999.0; }
/** * 解析星期 * * @param string $set * @param string $nowd * @param string $noww * @return array */ protected static function fieldWeek($set, $nowd, $noww) { if (self::$isCeil) { $noww++; $nowd++; } $noww %= 7; self::$isCeil = false; if (!is_numeric($set)) { $parse = self::parseCronNumbers($set, 0, 6); $set = $parse[0]; foreach ($parse as $value) { if ($noww <= $value) { $set = $value; break; } } } $set = intval($set); if ($noww > $set) { self::reset(false); $offset += 7 - ($noww - $set); } elseif ($noww < $set) { self::reset(false); $offset += $set - $noww; } else { $offset = $noww - $set; } $retval = array($nowd + $offset); return $retval; }
/** * 根据配置载入需要执行的任务 */ public static function load_config() { $time = time(); $config = self::$tasksHandle->getTasks(self::$taskParams); foreach ($config as $id => $task) { $ret = ParseCrontab::parse($task["rule"], $time); if ($ret === false) { Main::log_write(ParseCrontab::$error); } elseif (!empty($ret)) { TickTable::set_task($ret, array_merge($task, array("id" => $id))); } } }
/** * 根据配置载入需要执行的任务 */ public static function load_config() { $time = time(); $config = LoadConfig::get_config(); foreach ($config as $id => $task) { $ret = ParseCrontab::parse($task["time"], $time); if ($ret === false) { Main::log_write(ParseCrontab::$error); } elseif (!empty($ret)) { TurnTable::set_task($ret, array_merge($task, array("id" => $id))); } } TurnTable::turn(); }