THINKPHP的cron任务实现

THINKPHP的cron任务实现

THINKPHP的cron计划任务的实现,利用THINKPHP自带的cli,加上数据库执行记录(记录任务的报错,成功)。

在服务器cron定时任务在网站目录(不是网站根目录)执行php cron.php,网站根目录为Public。

1.jpg

写一个cli的入口文件

立即学习“PHP免费学习笔记(深入)”;

cli.php

<?phpdefine('MODE_NAME', 'cli');// 检测PHP环境if(version_compare(PHP_VERSION,'5.3.0',' 5.3.0 !');define('APP_DEBUG', true);// 定义应用目录define('APP_PATH', __DIR__ . '/Application/');// 引入ThinkPHP入口文件require __DIR__ . '/ThinkPHP/ThinkPHP.php';

写一个执行文件

cron.php

define('AUTO_CRON', true);include __DIR__ . '/cli.php';

数据库设计

DROP TABLE IF EXISTS `cron`;CREATE TABLE IF NOT EXISTS `cron` (  `cron_id` int(10) unsigned NOT NULL AUTO_INCREMENT,  `name` varchar(255) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',  `expression` varchar(255) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',  `class` varchar(255) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',  `method` varchar(255) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',  `type` varchar(30) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',  `status` varchar(30) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',  `created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',  `updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',  `run_at` timestamp NULL DEFAULT NULL,  `ms` int(10) unsigned NOT NULL DEFAULT '0',  `error` text COLLATE utf8_unicode_ci NOT NULL,  PRIMARY KEY (`cron_id`),  KEY `name` (`name`,`created_at`),  KEY `cron_status_index` (`status`)) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;

配置文件

 '1.0.0',    'beastalkd' => array(        'process_untreated_queue' => array(            'expression' => '* * * * *',            'class' => 'StatisticsModelPheanstalkModel',            'method' => 'processUntreatedQueue'        )    ));

执行文件 init.php  

/写个hook程序执行init.php

query("SHOW TABLES LIKE 'cron'")?false:true;if(defined("AUTO_CRON") && $Has){    class CronCommand    {        protected $_initializedJobs;        protected $_jobs;        protected $_now;        public function __construct()        {            $this->_now = strtotime(date('Y-n-j H:i'));            import("Cron.Common.Cron.tdcron_entry",'','.php');            import("Cron.Common.Cron.tdcron",'','.php');        }        /**         * 这里是放要执行的代码         */        public function fire()        {            restore_error_handler();            restore_exception_handler();            $this->_initializedJobs = array();            $jobs = M('cron')->where("status = 'initialized'")->select();            /**             * @var $cron Cron             * 已存在 cron             */            if($jobs) {                $cron = new Cron();                foreach ($jobs as $data) {                    $cron->setData($data)->isNew(false);                    $this->_initializedJobs[$data['name']] = $cron;                }            }            /**             * 新 cron             */            foreach ($this->getCronJobs() as $name => $cronJob) {                if (isset($cronJob['expression'])) {                    $expression = $cronJob['expression'];                } else {                    Log::write('Cron expression is required for cron job "' . $name . '"',Log::WARN);                    continue;                }                if ($this->_now != tdCron::getNextOccurrence($expression, $this->_now)) continue;                $cronJob['name'] = $name;                $cron = isset($this->_initializedJobs[$name]) ? $this->_initializedJobs[$name] : $this->_initializedJobs[$name] = new Cron();                $cron->initialize($cronJob);            }            /* @var $cron Cron 处理*/            foreach ($this->_initializedJobs as $cron) {                $cron->run();            }        }        /**         * Get All Defined Cron Jobs         * 获取配置         * @return array         */        public function getCronJobs()        {            if ($this->_jobs === null) {                $this->_jobs = C('beastalkd');            }            return $this->_jobs;        }    }    $command = new CronCommand();    $command->fire();}

cron 模型

 $v) {            $this->setData($k, $v);        }        $now = date('Y-m-d H:i:s');        $this->setData('status',self::STATUS_INITIALIZED)->setData('created_at',$now)->setData('updated_at',$now)->save();        return $this;    }    /**     * @return $this  run 命令     */    public function run()    {        $this->setData('run_at',date('Y-m-d H:i:s'))->setData('status',self::STATUS_RUNNING)->save();        Timer::start();        try {            $class = $this->getData('class');            $method = $this->getData('method');            if (!class_exists($class)) throw new Exception(sprintf('Class "%s" not found!', $class));            if (!method_exists($class, $method)) throw new Exception(sprintf('Method "%s::%s()" not found!', $class, $method));            $callback = array($this->getSingleton($class), $method);            //new CLASS 使用操作方法            // 执行配置里的 StatisticsModelPheanstalkModel类 的 processUntreatedQueue 操作             call_user_func($callback);            Timer::stop();            $this->setData('ms',round(Timer::diff() * 1000))->setData('status',self::STATUS_COMPLETED)->save();        } catch (Exception $e) {            Timer::stop();            $this->setData('ms',round(Timer::diff() * 1000))                ->setData('status',self::STATUS_FAILED)                ->setData('error',$e->getMessage() . "nParams:n" . var_export($this->getDbFields(), true))->save();            Log::write($e->getMessage() . "n" . $e->getTraceAsString(),Log::ERR);        }        return $this;    }}

CommonModel 模型

_jsonFields as $field) {         is_string($_data = fnGet($result, $field)) and $result[$field] = json_decode($_data, true);      }      $this->_originalData = $result;      $this->_isNew = !$result;      parent::_after_find($result, $options);   }   protected function _after_save($result) {   }   protected function _before_find() {      $this->_originalData = array();   }   protected function _facade($data) {      foreach ($this->_jsonFields as $field) {         is_array($_data = fnGet($data, $field)) and $data[$field] = json_encode($_data);      }      return parent::_facade($data);   }   public function find($options = array()) {      $this->_before_find();      return parent::find($options);   }   public function getData($key = null) {      return $key === null ? $this->data : $this->__get($key);   }   public function getOptions() {      return $this->options;   }   public function getOriginalData($key = null) {      return $key === null ? $this->_originalData : fnGet($this->_originalData, $key);   }   /**    * Get or set isNew flag    *    * @param bool $flag    *    * @return bool    */   public function isNew($flag = null) {      if ($flag !== null) $this->_isNew = (bool)$flag;      return $this->_isNew;   }   public function save($data = '', $options = array()) {      if ($this->_isNew) {         $oldData = $this->data;         $result = $this->add($data, $options);         $this->data = $oldData;         if ($result && $this->pk && is_string($this->pk)) {            $this->setData($this->pk, $result);         }         $this->_isNew = false;      } else {         $oldData = $this->data;         $result = parent::save($data, $options);         $this->data = $oldData;      }      $this->_after_save($result);      return $result;   }   public function setData($key, $value = null) {      is_array($key) ?         $this->data = $key :         $this->data[$key] = $value;      return $this;   }}

Timer.class.php

<?phpnamespace CronModel;class Timer{    protected static $_start = array(0, 0);    protected static $_stop = array(0, 0);    public static function diff($start = null, $stop = null)    {        $start and self::start($start);        $stop and self::stop($stop);        return (self::$_stop[0] - self::$_start[0]) + (self::$_stop[1] - self::$_start[1]);    }    public static function start($microtime = null)    {        $microtime or $microtime = microtime();        self::$_start = explode(' ', $microtime);    }    public static function stop($microtime = null)    {        $microtime or $microtime = microtime();        self::$_stop = explode(' ', $microtime);    }}

tdcron.php

<?phpdefine('IDX_MINUTE', 0);define('IDX_HOUR', 1);define('IDX_DAY', 2);define('IDX_MONTH', 3);define('IDX_WEEKDAY', 4);define('IDX_YEAR', 5);/* * tdCron v0.0.1 beta - CRON-Parser for PHP * * Copyright (c) 2010 Christian Land / tagdocs.de * * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and * associated documentation files (the "Software"), to deal in the Software without restriction, * including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, * subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all copies or substantial * portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT * LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN * NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * * @author Christian Land  * @package    tdCron * @copyright  Copyright (c) 2010, Christian Land / tagdocs.de * @version    v0.0.1 beta */class tdCron{   /**    * Parsed cron-expressions cache.    * @var mixed    */   static private $pcron = array();   /**    * getNextOccurrence() uses a cron-expression to calculate the time and date at which a cronjob    * should be executed the next time. If a reference-time is passed, the next time and date    * after that time is calculated.    *    * @access    public    * @param     string $expression cron-expression to use    * @param     int $timestamp optional reference-time    * @return    int    * @throws    Exception    */   static public function getNextOccurrence($expression, $timestamp = null)   {      try {         // Convert timestamp to array         $next = self::getTimestamp($timestamp);         // Calculate date/time         $next_time = self::calculateDateTime($expression, $next);      } catch (Exception $e) {         throw $e;      }      // return calculated time      return $next_time;   }   /**    * getLastOccurrence() does pretty much the same as getNextOccurrence(). The only difference    * is, that it doesn't calculate the next but the last time a cronjob should have been executed.    *    * @access    public    * @param     string $expression cron-expression to use    * @param     int $timestamp optional reference-time    * @return    int    * @throws    Exception    */   static public function getLastOccurrence($expression, $timestamp = null)   {      try {         // Convert timestamp to array         $last = self::getTimestamp($timestamp);         // Calculate date/time         $last_time = self::calculateDateTime($expression, $last, false);      } catch (Exception $e) {         throw $e;      }      // return calculated time      return $last_time;   }   /**    * calculateDateTime() is the function where all the magic happens :-)    *    * It calculates the time and date at which the next/last call of a cronjob is/was due.    *    * @access    private    * @param     mixed $expression cron-expression    * @param     mixed $rtime reference-time    * @param     bool $next true = nextOccurence, false = lastOccurence    * @return    int    * @throws    Exception    */   static private function calculateDateTime($expression, $rtime, $next = true)   {      // Initialize vars      $calc_date = true;      // Parse cron-expression (if neccessary)      $cron = self::getExpression($expression, !$next);      // OK, lets see if the day/month/weekday of the reference-date exist in our      // $cron-array.      if (!in_array($rtime[IDX_DAY], $cron[IDX_DAY]) || !in_array($rtime[IDX_MONTH], $cron[IDX_MONTH]) || !in_array($rtime[IDX_WEEKDAY], $cron[IDX_WEEKDAY])) {         // OK, things are easy. The day/month/weekday of the reference time         // can't be found in the $cron-array. This means that no matter what         // happens, we WILL end up at at a different date than that of our         // reference-time. And in this case, the lastOccurrence will ALWAYS         // happen at the latest possible time of the day and the nextOccurrence         // at the earliest possible time.         //         // In both cases, the time can be found in the first elements of the         // hour/minute cron-arrays.         $rtime[IDX_HOUR] = reset($cron[IDX_HOUR]);         $rtime[IDX_MINUTE] = reset($cron[IDX_MINUTE]);      } else {         // OK, things are getting a little bit more complicated...         $nhour = self::findValue($rtime[IDX_HOUR], $cron[IDX_HOUR], $next);         // Meh. Such a cruel world. Something has gone awry. Lets see HOW awry it went.         if ($nhour === false) {            // Ah, the hour-part went wrong. Thats easy. Wrong hour means that no            // matter what we do we'll end up at a different date. Thus we can use            // some simple operations to make things look pretty ;-)            //            // As alreasy mentioned before -> different date means earliest/latest            // time:            $rtime[IDX_HOUR] = reset($cron[IDX_HOUR]);            $rtime[IDX_MINUTE] = reset($cron[IDX_MINUTE]);            // Now all we have to do is add/subtract a day to get a new reference time            // to use later to find the right date. The following line probably looks            // a little odd but thats the easiest way of adding/substracting a day without            // screwing up the date. Just trust me on that one ;-)            $rtime = explode(',', strftime('%M,%H,%d,%m,%w,%Y', mktime($rtime[IDX_HOUR], $rtime[IDX_MINUTE], 0, $rtime[IDX_MONTH], $rtime[IDX_DAY], $rtime[IDX_YEAR]) + ((($next) ? 1 : -1) * 86400)));         } else {            // OK, there is a higher/lower hour available. Check the minutes-part.            $nminute = self::findValue($rtime[IDX_MINUTE], $cron[IDX_MINUTE], $next);            if ($nminute === false) {               // No matching minute-value found... lets see what happens if we substract/add an hour               $nhour = self::findValue($rtime[IDX_HOUR] + (($next) ? 1 : -1), $cron[IDX_HOUR], $next);               if ($nhour === false) {                  // No more hours available... add/substract a day... you know what happens ;-)                  $nminute = reset($cron[IDX_MINUTE]);                  $nhour = reset($cron[IDX_HOUR]);                  $rtime = explode(',', strftime('%M,%H,%d,%m,%w,%Y', mktime($nhour, $nminute, 0, $rtime[IDX_MONTH], $rtime[IDX_DAY], $rtime[IDX_YEAR]) + ((($next) ? 1 : -1) * 86400)));               } else {                  // OK, there was another hour. Set the right minutes-value                  $rtime[IDX_HOUR] = $nhour;                  $rtime[IDX_MINUTE] = (($next) ? reset($cron[IDX_MINUTE]) : end($cron[IDX_MINUTE]));                  $calc_date = false;               }            } else {               // OK, there is a matching minute... reset minutes if hour has changed               if ($nhour  $rtime[IDX_HOUR]) {                  $nminute = reset($cron[IDX_MINUTE]);               }               // Set time               $rtime[IDX_HOUR] = $nhour;               $rtime[IDX_MINUTE] = $nminute;               $calc_date = false;            }         }      }      // If we have to calculate the date... we'll do so      if ($calc_date) {         if (in_array($rtime[IDX_DAY], $cron[IDX_DAY]) && in_array($rtime[IDX_MONTH], $cron[IDX_MONTH]) && in_array($rtime[IDX_WEEKDAY], $cron[IDX_WEEKDAY])) {            return mktime($rtime[1], $rtime[0], 0, $rtime[3], $rtime[2], $rtime[5]);         } else {            // OK, some searching necessary...            $cdate = mktime(0, 0, 0, $rtime[IDX_MONTH], $rtime[IDX_DAY], $rtime[IDX_YEAR]);            // OK, these three nested loops are responsible for finding the date...            //            // The class has 2 limitations/bugs right now:            //            // -> it doesn't work for dates in 2036 or later!            // -> it will most likely fail if you search for a Feburary, 29th with a given weekday            //    (this does happen because the class only searches in the next/last 10 years! And            //    while it usually takes less than 10 years for a "normal" date to iterate through            //    all weekdays, it can take 20+ years for Feb, 29th to iterate through all weekdays!            for ($nyear = $rtime[IDX_YEAR]; (($next) ? ($nyear = $rtime[IDX_YEAR] - 10)); $nyear = $nyear + (($next) ? 1 : -1)) {               foreach ($cron[IDX_MONTH] as $nmonth) {                  foreach ($cron[IDX_DAY] as $nday) {                     if (checkdate($nmonth, $nday, $nyear)) {                        $ndate = mktime(0, 0, 1, $nmonth, $nday, $nyear);                        if (($next) ? ($ndate >= $cdate) : ($ndate  minute    *    [1]    -> hour    *    [2]    -> day    *    [3]    -> month    *    [4]    -> weekday    *    [5]    -> year    *    * The array is used by various functions.    *    * @access    private    * @param    int $timestamp If none is given, the current time is used    * @return    mixed    */   static private function getTimestamp($timestamp = null)   {      if (is_null($timestamp)) {         $arr = explode(',', strftime('%M,%H,%d,%m,%w,%Y', time()));      } else {         $arr = explode(',', strftime('%M,%H,%d,%m,%w,%Y', $timestamp));      }      // Remove leading zeros (or we'll get in trouble ;-)      foreach ($arr as $key => $value) {         $arr[$key] = (int)ltrim($value, '0');      }      return $arr;   }   /**    * findValue() checks if the given value exists in an array. If it does not exist, the next    * higher/lower value is returned (depending on $next). If no higher/lower value exists,    * false is returned.    *    * @access    public    * @param    int $value    * @param    mixed $data    * @param    bool $next    * @return    mixed    */   static private function findValue($value, $data, $next = true)   {      if (in_array($value, $data)) {         return (int)$value;      } else {         if (($next) ? ($value = end($data))) {            foreach ($data as $curval) {               if (($next) ? ($value <= (int)$curval) : ($curval  $value) {         $cron[$key] = array_reverse($value);      }      return $cron;   }}

tdcron_entry.php

<?php/** * tinyCronEntry is part of tdCron. Its a class to parse Cron-Expressions like "1-45 1,2,3 1-30/5 January,February Mon,Tue" * and convert it to an easily useable format. * * The parser is quite powerful and understands pretty much everything you will ever find in a Cron-Expression. * * A Cron-Expression consists of 5 segments: * * 
 *  .---------------- minute (0 - 59) *  |   .------------- hour (0 - 23) *  |   |   .---------- day of month (1 - 31) *  |   |   |   .------- month (1 - 12) *  |   |   |   |  .----- day of week (0 - 6) *  |   |   |   |  | *  *   *   *   *  * * 

* * Each segment can contain values, ranges and intervals. A range is always written as "value1-value2" and * intervals as "value1/value2". * * Of course each segment can contain multiple values seperated by commas. * * Some valid examples: * *

 * 1,2,3,4,5 * 1-5 * 10-20/* * Jan,Feb,Oct * Monday-Friday * 1-10,15,20,40-50/2 * 

* * The current version of the parser understands all weekdays and month names in german and english! * * Usually you won't need to call this class directly. * * Copyright (c) 2010 Christian Land / tagdocs.de * * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and * associated documentation files (the "Software"), to deal in the Software without restriction, * including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, * subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all copies or substantial * portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT * LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN * NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * * @author Christian Land * @package tinyCron * @subpackage tinyCronEntry * @copyright Copyright (c) 2010, Christian Land / tagdocs.de * @version v0.0.1 beta */class tdCronEntry{ /** * The parsed cron-expression. * @var mixed */ static private $cron = array(); /** * Ranges. * @var mixed */ static private $ranges = array( IDX_MINUTE => array('min' => 0, 'max' => 59), // Minutes IDX_HOUR => array('min' => 0, 'max' => 23), // Hours IDX_DAY => array('min' => 1, 'max' => 31), // Days IDX_MONTH => array('min' => 1, 'max' => 12), // Months IDX_WEEKDAY => array('min' => 0, 'max' => 7) // Weekdays ); /** * Named intervals. * @var mixed */ static private $intervals = array( '@yearly' => '0 0 1 1 *', '@annually' => '0 0 1 1 *', '@monthly' => '0 0 1 * *', '@weekly' => '0 0 * * 0', '@midnight' => '0 0 * * *', '@daily' => '0 0 * * *', '@hourly' => '0 * * * *' ); /** * Possible keywords for months/weekdays. * @var mixed */ static private $keywords = array( IDX_MONTH => array( '/(january|januar|jan)/i' => 1, '/(february|februar|feb)/i' => 2, '/(march|maerz|m?rz|mar|mae|m?r)/i' => 3, '/(april|apr)/i' => 4, '/(may|mai)/i' => 5, '/(june|juni|jun)/i' => 6, '/(july|juli|jul)/i' => 7, '/(august|aug)/i' => 8, '/(september|sep)/i' => 9, '/(october|oktober|okt|oct)/i' => 10, '/(november|nov)/i' => 11, '/(december|dezember|dec|dez)/i' => 12 ), IDX_WEEKDAY => array( '/(sunday|sonntag|sun|son|su|so)/i' => 0, '/(monday|montag|mon|mo)/i' => 1, '/(tuesday|dienstag|die|tue|tu|di)/i' => 2, '/(wednesdays|mittwoch|mit|wed|we|mi)/i' => 3, '/(thursday|donnerstag|don|thu|th|do)/i' => 4, '/(friday|freitag|fre|fri|fr)/i' => 5, '/(saturday|samstag|sam|sat|sa)/i' => 6 ) ); /** * parseExpression() analyses crontab-expressions like "* * 1,2,3 * mon,tue" and returns an array * containing all values. If it can't be parsed, an exception is thrown. * * @access public * @param string $expression The cron-expression to parse. * @return mixed * @throws Exception */ static public function parse($expression) { $dummy = array(); // Convert named expressions if neccessary if (substr($expression, 0, 1) == '@') { $expression = strtr($expression, self::$intervals); if (substr($expression, 0, 1) == '@') { // Oops... unknown named interval!?!! throw new Exception('Unknown named interval [' . $expression . ']', 10000); } } // Next basic check... do we have 5 segments? $cron = explode(' ', $expression); if (count($cron) 5) { // No... we haven't... throw new Exception('Wrong number of segments in expression. Expected: 5, Found: ' . count($cron), 10001); } else { // Yup, 5 segments... lets see if we can work with them foreach ($cron as $idx => $segment) { try { $dummy[$idx] = self::expandSegment($idx, $segment); } catch (Exception $e) { throw $e; } } } return $dummy; } /** * expandSegment() analyses a single segment * * @access public * @param $idx * @param $segment * @return array * @throws Exception */ static private function expandSegment($idx, $segment) { // Store original segment for later use $osegment = $segment; // Replace months/weekdays like "January", "February", etc. with numbers if (isset(self::$keywords[$idx])) { $segment = preg_replace(array_keys(self::$keywords[$idx]), array_values(self::$keywords[$idx]), $segment); } // Replace wildcards if (substr($segment, 0, 1) == '*') { $segment = preg_replace('/^*(/d+)?$/i', self::$ranges[$idx]['min'] . '-' . self::$ranges[$idx]['max'] . '$1', $segment); } // Make sure that nothing unparsed is left :) $dummy = preg_replace('/[0-9-/,]/', '', $segment); if (!empty($dummy)) { // Ohoh.... thats not good :-) throw new Exception('Failed to parse segment: ' . $osegment, 10002); } // At this point our string should be OK - lets convert it to an array $result = array(); $atoms = explode(',', $segment); foreach ($atoms as $curatom) { $result = array_merge($result, self::parseAtom($curatom)); } // Get rid of duplicates and sort the array $result = array_unique($result); sort($result); // Check for invalid values if ($idx == IDX_WEEKDAY) { if (end($result) == 7) { if (reset($result) 0) { array_unshift($result, 0); } array_pop($result); } } foreach ($result as $key => $value) { if (($value self::$ranges[$idx]['max'])) { throw new Exception('Failed to parse segment, invalid value [' . $value . ']: ' . $osegment, 10003); } } return $result; } /** * parseAtom() analyses a single segment * * @access public * @param string $atom The segment to parse * @return array */ static private function parseAtom($atom) { $expanded = array(); if (preg_match('/^(d+)-(d+)(/(d+))?/i', $atom, $matches)) { $low = $matches[1]; $high = $matches[2]; if ($low > $high) { list($low, $high) = array($high, $low); } $step = isset($matches[4]) ? $matches[4] : 1; for ($i = $low; $i <= $high; $i += $step) { $expanded[] = (int)$i; } } else { $expanded[] = (int)$atom; } $expanded2 = array_unique($expanded); return $expanded; }}

推荐教程:《TP5》

以上就是THINKPHP的cron任务实现的详细内容,更多请关注创想鸟其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/89754.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2025年11月18日 06:33:21
下一篇 2025年11月18日 07:14:08

相关推荐

  • Uniapp 中如何不拉伸不裁剪地展示图片?

    灵活展示图片:如何不拉伸不裁剪 在界面设计中,常常需要以原尺寸展示用户上传的图片。本文将介绍一种在 uniapp 框架中实现该功能的简单方法。 对于不同尺寸的图片,可以采用以下处理方式: 极端宽高比:撑满屏幕宽度或高度,再等比缩放居中。非极端宽高比:居中显示,若能撑满则撑满。 然而,如果需要不拉伸不…

    2025年12月24日
    400
  • 如何让小说网站控制台显示乱码,同时网页内容正常显示?

    如何在不影响用户界面的情况下实现控制台乱码? 当在小说网站上下载小说时,大家可能会遇到一个问题:网站上的文本在网页内正常显示,但是在控制台中却是乱码。如何实现此类操作,从而在不影响用户界面(UI)的情况下保持控制台乱码呢? 答案在于使用自定义字体。网站可以通过在服务器端配置自定义字体,并通过在客户端…

    2025年12月24日
    800
  • 如何在地图上轻松创建气泡信息框?

    地图上气泡信息框的巧妙生成 地图上气泡信息框是一种常用的交互功能,它简便易用,能够为用户提供额外信息。本文将探讨如何借助地图库的功能轻松创建这一功能。 利用地图库的原生功能 大多数地图库,如高德地图,都提供了现成的信息窗体和右键菜单功能。这些功能可以通过以下途径实现: 高德地图 JS API 参考文…

    2025年12月24日
    400
  • 如何使用 scroll-behavior 属性实现元素scrollLeft变化时的平滑动画?

    如何实现元素scrollleft变化时的平滑动画效果? 在许多网页应用中,滚动容器的水平滚动条(scrollleft)需要频繁使用。为了让滚动动作更加自然,你希望给scrollleft的变化添加动画效果。 解决方案:scroll-behavior 属性 要实现scrollleft变化时的平滑动画效果…

    2025年12月24日
    000
  • 如何为滚动元素添加平滑过渡,使滚动条滑动时更自然流畅?

    给滚动元素平滑过渡 如何在滚动条属性(scrollleft)发生改变时为元素添加平滑的过渡效果? 解决方案:scroll-behavior 属性 为滚动容器设置 scroll-behavior 属性可以实现平滑滚动。 html 代码: click the button to slide right!…

    2025年12月24日
    500
  • 如何选择元素个数不固定的指定类名子元素?

    灵活选择元素个数不固定的指定类名子元素 在网页布局中,有时需要选择特定类名的子元素,但这些元素的数量并不固定。例如,下面这段 html 代码中,activebar 和 item 元素的数量均不固定: *n *n 如果需要选择第一个 item元素,可以使用 css 选择器 :nth-child()。该…

    2025年12月24日
    200
  • 使用 SVG 如何实现自定义宽度、间距和半径的虚线边框?

    使用 svg 实现自定义虚线边框 如何实现一个具有自定义宽度、间距和半径的虚线边框是一个常见的前端开发问题。传统的解决方案通常涉及使用 border-image 引入切片图片,但是这种方法存在引入外部资源、性能低下的缺点。 为了避免上述问题,可以使用 svg(可缩放矢量图形)来创建纯代码实现。一种方…

    2025年12月24日
    100
  • 如何让“元素跟随文本高度,而不是撑高父容器?

    如何让 元素跟随文本高度,而不是撑高父容器 在页面布局中,经常遇到父容器高度被子元素撑开的问题。在图例所示的案例中,父容器被较高的图片撑开,而文本的高度没有被考虑。本问答将提供纯css解决方案,让图片跟随文本高度,确保父容器的高度不会被图片影响。 解决方法 为了解决这个问题,需要将图片从文档流中脱离…

    2025年12月24日
    000
  • 为什么 CSS mask 属性未请求指定图片?

    解决 css mask 属性未请求图片的问题 在使用 css mask 属性时,指定了图片地址,但网络面板显示未请求获取该图片,这可能是由于浏览器兼容性问题造成的。 问题 如下代码所示: 立即学习“前端免费学习笔记(深入)”; icon [data-icon=”cloud”] { –icon-cl…

    2025年12月24日
    200
  • 如何利用 CSS 选中激活标签并影响相邻元素的样式?

    如何利用 css 选中激活标签并影响相邻元素? 为了实现激活标签影响相邻元素的样式需求,可以通过 :has 选择器来实现。以下是如何具体操作: 对于激活标签相邻后的元素,可以在 css 中使用以下代码进行设置: li:has(+li.active) { border-radius: 0 0 10px…

    2025年12月24日
    100
  • 如何模拟Windows 10 设置界面中的鼠标悬浮放大效果?

    win10设置界面的鼠标移动显示周边的样式(探照灯效果)的实现方式 在windows设置界面的鼠标悬浮效果中,光标周围会显示一个放大区域。在前端开发中,可以通过多种方式实现类似的效果。 使用css 使用css的transform和box-shadow属性。通过将transform: scale(1.…

    2025年12月24日
    200
  • 为什么我的 Safari 自定义样式表在百度页面上失效了?

    为什么在 Safari 中自定义样式表未能正常工作? 在 Safari 的偏好设置中设置自定义样式表后,您对其进行测试却发现效果不同。在您自己的网页中,样式有效,而在百度页面中却失效。 造成这种情况的原因是,第一个访问的项目使用了文件协议,可以访问本地目录中的图片文件。而第二个访问的百度使用了 ht…

    2025年12月24日
    000
  • 如何用前端实现 Windows 10 设置界面的鼠标移动探照灯效果?

    如何在前端实现 Windows 10 设置界面中的鼠标移动探照灯效果 想要在前端开发中实现 Windows 10 设置界面中类似的鼠标移动探照灯效果,可以通过以下途径: CSS 解决方案 DEMO 1: Windows 10 网格悬停效果:https://codepen.io/tr4553r7/pe…

    2025年12月24日
    000
  • 使用CSS mask属性指定图片URL时,为什么浏览器无法加载图片?

    css mask属性未能加载图片的解决方法 使用css mask属性指定图片url时,如示例中所示: mask: url(“https://api.iconify.design/mdi:apple-icloud.svg”) center / contain no-repeat; 但是,在网络面板中却…

    2025年12月24日
    000
  • 如何用CSS Paint API为网页元素添加时尚的斑马线边框?

    为元素添加时尚的斑马线边框 在网页设计中,有时我们需要添加时尚的边框来提升元素的视觉效果。其中,斑马线边框是一种既醒目又别致的设计元素。 实现斜向斑马线边框 要实现斜向斑马线间隔圆环,我们可以使用css paint api。该api提供了强大的功能,可以让我们在元素上绘制复杂的图形。 立即学习“前端…

    2025年12月24日
    000
  • 图片如何不撑高父容器?

    如何让图片不撑高父容器? 当父容器包含不同高度的子元素时,父容器的高度通常会被最高元素撑开。如果你希望父容器的高度由文本内容撑开,避免图片对其产生影响,可以通过以下 css 解决方法: 绝对定位元素: .child-image { position: absolute; top: 0; left: …

    2025年12月24日
    000
  • CSS 帮助

    我正在尝试将文本附加到棕色框的左侧。我不能。我不知道代码有什么问题。请帮助我。 css .hero { position: relative; bottom: 80px; display: flex; justify-content: left; align-items: start; color:…

    2025年12月24日 好文分享
    200
  • 前端代码辅助工具:如何选择最可靠的AI工具?

    前端代码辅助工具:可靠性探讨 对于前端工程师来说,在HTML、CSS和JavaScript开发中借助AI工具是司空见惯的事情。然而,并非所有工具都能提供同等的可靠性。 个性化需求 关于哪个AI工具最可靠,这个问题没有一刀切的答案。每个人的使用习惯和项目需求各不相同。以下是一些影响选择的重要因素: 立…

    2025年12月24日
    000
  • 如何用 CSS Paint API 实现倾斜的斑马线间隔圆环?

    实现斑马线边框样式:探究 css paint api 本文将探究如何使用 css paint api 实现倾斜的斑马线间隔圆环。 问题: 给定一个有多个圆圈组成的斑马线图案,如何使用 css 实现倾斜的斑马线间隔圆环? 答案: 立即学习“前端免费学习笔记(深入)”; 使用 css paint api…

    2025年12月24日
    000
  • 如何使用CSS Paint API实现倾斜斑马线间隔圆环边框?

    css实现斑马线边框样式 想定制一个带有倾斜斑马线间隔圆环的边框?现在使用css paint api,定制任何样式都轻而易举。 css paint api 这是一个新的css特性,允许开发人员创建自定义形状和图案,其中包括斑马线样式。 立即学习“前端免费学习笔记(深入)”; 实现倾斜斑马线间隔圆环 …

    2025年12月24日
    100

发表回复

登录后才能评论
关注微信