首頁 php框架 ThinkPHP THINKPHP的cron任務實現

THINKPHP的cron任務實現

May 18, 2020 am 09:11 AM
thinkphp

THINKPHP的cron任務實現

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

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

THINKPHP的cron任務實現

写一个cli的入口文件

cli.php

<?php
define(&#39;MODE_NAME&#39;, &#39;cli&#39;);
// 检测PHP环境
if(version_compare(PHP_VERSION,&#39;5.3.0&#39;,&#39;<&#39;))  die(&#39;require PHP > 5.3.0 !&#39;);

define(&#39;APP_DEBUG&#39;, true);

// 定义应用目录
define(&#39;APP_PATH&#39;, __DIR__ . &#39;/Application/&#39;);

// 引入ThinkPHP入口文件
require __DIR__ . &#39;/ThinkPHP/ThinkPHP.php&#39;;
登入後複製

写一个执行文件

cron.php

define(&#39;AUTO_CRON&#39;, true);
include __DIR__ . &#39;/cli.php&#39;;
登入後複製

数据库设计

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 &#39;&#39;,
  `expression` varchar(255) COLLATE utf8_unicode_ci NOT NULL DEFAULT &#39;&#39;,
  `class` varchar(255) COLLATE utf8_unicode_ci NOT NULL DEFAULT &#39;&#39;,
  `method` varchar(255) COLLATE utf8_unicode_ci NOT NULL DEFAULT &#39;&#39;,
  `type` varchar(30) COLLATE utf8_unicode_ci NOT NULL DEFAULT &#39;&#39;,
  `status` varchar(30) COLLATE utf8_unicode_ci NOT NULL DEFAULT &#39;&#39;,
  `created_at` timestamp NOT NULL DEFAULT &#39;0000-00-00 00:00:00&#39;,
  `updated_at` timestamp NOT NULL DEFAULT &#39;0000-00-00 00:00:00&#39;,
  `run_at` timestamp NULL DEFAULT NULL,
  `ms` int(10) unsigned NOT NULL DEFAULT &#39;0&#39;,
  `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 ;
登入後複製

配置文件

<?php
return array(
    &#39;version&#39; => &#39;1.0.0&#39;,
    &#39;beastalkd&#39; => array(
        &#39;process_untreated_queue&#39; => array(
            &#39;expression&#39; => &#39;* * * * *&#39;,
            &#39;class&#39; => &#39;Statistics\Model\PheanstalkModel&#39;,
            &#39;method&#39; => &#39;processUntreatedQueue&#39;
        )
    )
);
登入後複製

执行文件 init.php

/写个hook程序执行init.php

<?php
use Think\Log, Think\Db, Cron\Model\Cron;
$Model = new \Think\Model();
$Has = !$Model->query("SHOW TABLES LIKE &#39;cron&#39;")?false:true;

if(defined("AUTO_CRON") && $Has){
    class CronCommand
    {

        protected $_initializedJobs;
        protected $_jobs;
        protected $_now;

        public function __construct()
        {
            $this->_now = strtotime(date(&#39;Y-n-j H:i&#39;));
            import("Cron.Common.Cron.tdcron_entry",&#39;&#39;,&#39;.php&#39;);
            import("Cron.Common.Cron.tdcron",&#39;&#39;,&#39;.php&#39;);
        }

        /**
         * 这里是放要执行的代码
         */
        public function fire()
        {
            restore_error_handler();
            restore_exception_handler();
            $this->_initializedJobs = array();
            $jobs = M(&#39;cron&#39;)->where("status = &#39;initialized&#39;")->select();
            /**
             * @var $cron Cron
             * 已存在 cron
             */
            if($jobs) {
                $cron = new Cron();
                foreach ($jobs as $data) {
                    $cron->setData($data)->isNew(false);
                    $this->_initializedJobs[$data[&#39;name&#39;]] = $cron;
                }
            }

            /**
             * 新 cron
             */
            foreach ($this->getCronJobs() as $name => $cronJob) {
                if (isset($cronJob[&#39;expression&#39;])) {
                    $expression = $cronJob[&#39;expression&#39;];
                } else {
                    Log::write(&#39;Cron expression is required for cron job "&#39; . $name . &#39;"&#39;,Log::WARN);
                    continue;
                }
                if ($this->_now != tdCron::getNextOccurrence($expression, $this->_now)) continue;
                $cronJob[&#39;name&#39;] = $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(&#39;beastalkd&#39;);
            }
            return $this->_jobs;
        }

    }
    $command = new CronCommand();
    $command->fire();
}
登入後複製

cron 模型

<?php
namespace Cron\Model;
use Common\Model;
use Think\Log;

/**
 * Class Cron
 * @method string getClass()
 * @method string getMethod()
 * @method string getName()
 * @method string getType()
 * @package Cron\Model
 */
class Cron extends Model{

    const STATUS_COMPLETED = &#39;completed&#39;;
    const STATUS_FAILED = &#39;failed&#39;;
    const STATUS_INITIALIZED = &#39;initialized&#39;;
    const STATUS_RUNNING = &#39;running&#39;;

    protected $name = &#39;cron&#39;;
    protected $tableName = &#39;cron&#39;;
    protected $pk = &#39;cron_id&#39;;

    protected $_originalData = array();
    /**
     *  保存配置信息CLASS
     */
    protected static $_cron_classes = array();


    /**
     * @param $class
     * @return mixed  获取配置的 CLASS
     */
    public function getSingleton($class)
    {
        isset(static::$_cron_classes[$class]) or static::$_cron_classes[$class] = new $class;
        return static::$_cron_classes[$class];
    }


    /**
     * @param $cronJob
     * @return $this
     * 初始化 任务状态
     */
    public function initialize($cronJob)
    {
        foreach ($cronJob as $k => $v) {
            $this->setData($k, $v);
        }
        $now = date(&#39;Y-m-d H:i:s&#39;);
        $this->setData(&#39;status&#39;,self::STATUS_INITIALIZED)->setData(&#39;created_at&#39;,$now)->setData(&#39;updated_at&#39;,$now)->save();
        return $this;
    }

    /**
     * @return $this  run 命令
     */
    public function run()
    {
        $this->setData(&#39;run_at&#39;,date(&#39;Y-m-d H:i:s&#39;))->setData(&#39;status&#39;,self::STATUS_RUNNING)->save();
        Timer::start();
        try {
            $class = $this->getData(&#39;class&#39;);
            $method = $this->getData(&#39;method&#39;);
            if (!class_exists($class)) throw new \Exception(sprintf(&#39;Class "%s" not found!&#39;, $class));
            if (!method_exists($class, $method)) throw new \Exception(sprintf(&#39;Method "%s::%s()" not found!&#39;, $class, $method));
            $callback = array($this->getSingleton($class), $method);

            //new CLASS 使用操作方法
            // 执行配置里的 Statistics\Model\PheanstalkModel类 的 processUntreatedQueue 操作 
            call_user_func($callback);
            Timer::stop();
            $this->setData(&#39;ms&#39;,round(Timer::diff() * 1000))->setData(&#39;status&#39;,self::STATUS_COMPLETED)->save();

        } catch (\Exception $e) {
            Timer::stop();
            $this->setData(&#39;ms&#39;,round(Timer::diff() * 1000))
                ->setData(&#39;status&#39;,self::STATUS_FAILED)
                ->setData(&#39;error&#39;,$e->getMessage() . "\nParams:\n" . var_export($this->getDbFields(), true))->save();
            Log::write($e->getMessage() . "\n" . $e->getTraceAsString(),Log::ERR);
        }
        return $this;
    }

}
登入後複製

Common\Model 模型

<?php

namespace Common;

use Think\Model as ThinkModel;

/**
 * Class Model
 * @package Common
 *
 * @property \Think\Db\Driver\Mysql $db DB instance
 */
abstract class Model extends ThinkModel {
   protected $_isNew = true;
   protected $_jsonFields = array();
   protected $_originalData = array();

   protected function _after_find(&$result, $options) {
      foreach ($this->_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 = &#39;&#39;, $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

<?php
namespace Cron\Model;
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(&#39; &#39;, $microtime);
    }

    public static function stop($microtime = null)
    {
        $microtime or $microtime = microtime();
        self::$_stop = explode(&#39; &#39;, $microtime);
    }
}
登入後複製

tdcron.php

<?php

define(&#39;IDX_MINUTE&#39;, 0);
define(&#39;IDX_HOUR&#39;, 1);
define(&#39;IDX_DAY&#39;, 2);
define(&#39;IDX_MONTH&#39;, 3);
define(&#39;IDX_WEEKDAY&#39;, 4);
define(&#39;IDX_YEAR&#39;, 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 <devel@tagdocs.de>
 * @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&#39;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&#39;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&#39;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(&#39;,&#39;, strftime(&#39;%M,%H,%d,%m,%w,%Y&#39;, 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(&#39;,&#39;, strftime(&#39;%M,%H,%d,%m,%w,%Y&#39;, 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&#39;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&#39;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 >= $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 <= $cdate)) {
                           $dow = date(&#39;w&#39;, $ndate);

                           // The date is "OK" - lets see if the weekday matches, too...
                           if (in_array($dow, $cron[IDX_WEEKDAY])) {
                              // WIN! :-) We found a valid date...
                              $rtime = explode(&#39;,&#39;, strftime(&#39;%M,%H,%d,%m,%w,%Y&#39;, mktime($rtime[IDX_HOUR], $rtime[IDX_MINUTE], 0, $nmonth, $nday, $nyear)));
                              return mktime($rtime[1], $rtime[0], 0, $rtime[3], $rtime[2], $rtime[5]);
                           }
                        }
                     }
                  }
               }
            }
         }

         throw new Exception(&#39;Failed to find date, No matching date found in a 10 years range!&#39;, 10004);
      }

      return mktime($rtime[1], $rtime[0], 0, $rtime[3], $rtime[2], $rtime[5]);

   }

   /**
    * getTimestamp() converts an unix-timestamp to an array. The returned array contains the following values:
    *
    *    [0]    -> 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(&#39;,&#39;, strftime(&#39;%M,%H,%d,%m,%w,%Y&#39;, time()));
      } else {
         $arr = explode(&#39;,&#39;, strftime(&#39;%M,%H,%d,%m,%w,%Y&#39;, $timestamp));
      }

      // Remove leading zeros (or we&#39;ll get in trouble ;-)
      foreach ($arr as $key => $value) {
         $arr[$key] = (int)ltrim($value, &#39;0&#39;);
      }
      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)) : ($value >= end($data))) {
            foreach ($data as $curval) {
               if (($next) ? ($value <= (int)$curval) : ($curval <= $value)) {
                  return (int)$curval;
               }
            }
         }
      }
      return false;
   }

   /**
    * getExpression() returns a parsed cron-expression. Parsed cron-expressions are cached to reduce
    * unneccessary calls of the parser.
    *
    * @access    public
    * @param     string $expression
    * @param     bool $reverse
    * @return    mixed
    * @throws    Exception
    */
   static private function getExpression($expression, $reverse = false)
   {
      // First of all we cleanup the expression and remove all duplicate tabs/spaces/etc.
      // For example "*              * *    * *" would be converted to "* * * * *", etc.
      $expression = preg_replace(&#39;/(\s+)/&#39;, &#39; &#39;, strtolower(trim($expression)));

      // Lets see if we&#39;ve already parsed that expression
      if (!isset(self::$pcron[$expression])) {
         // Nope - parse it!
         try {
            self::$pcron[$expression] = tdCronEntry::parse($expression);
            self::$pcron[&#39;reverse&#39;][$expression] = self::arrayReverse(self::$pcron[$expression]);
         } catch (Exception $e) {
            throw $e;
         }
      }
      return ($reverse ? self::$pcron[&#39;reverse&#39;][$expression] : self::$pcron[$expression]);
   }

   /**
    * arrayReverse() reverses all sub-arrays of our cron array. The reversed values are used for calculations
    * that are run when getLastOccurence() is called.
    *
    * @access    public
    * @param    mixed $cron
    * @return    mixed
    */
   static private function arrayReverse($cron)
   {
      foreach ($cron as $key => $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:
 *
 * <pre class="brush:php;toolbar:false">
 *  .---------------- 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]['min']) || ($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任務實現的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

thinkphp專案怎麼運行 thinkphp專案怎麼運行 Apr 09, 2024 pm 05:33 PM

執行 ThinkPHP 專案需要:安裝 Composer;使用 Composer 建立專案;進入專案目錄,執行 php bin/console serve;造訪 http://localhost:8000 查看歡迎頁面。

thinkphp有幾個版本 thinkphp有幾個版本 Apr 09, 2024 pm 06:09 PM

ThinkPHP 擁有多個版本,針對不同 PHP 版本而設計。主要版本包括 3.2、5.0、5.1 和 6.0,而次要版本用於修復 bug 和提供新功能。目前最新穩定版本為 ThinkPHP 6.0.16。在選擇版本時,需考慮 PHP 版本、功能需求和社群支援。建議使用最新穩定版本以獲得最佳性能和支援。

thinkphp怎麼運行 thinkphp怎麼運行 Apr 09, 2024 pm 05:39 PM

ThinkPHP Framework 的本機運作步驟:下載並解壓縮 ThinkPHP Framework 到本機目錄。建立虛擬主機(可選),指向 ThinkPHP 根目錄。配置資料庫連線參數。啟動 Web 伺服器。初始化 ThinkPHP 應用程式。存取 ThinkPHP 應用程式 URL 運行。

laravel和thinkphp哪個好 laravel和thinkphp哪個好 Apr 09, 2024 pm 03:18 PM

Laravel 和 ThinkPHP 框架的效能比較:ThinkPHP 效能通常優於 Laravel,專注於最佳化和快取。 Laravel 性能良好,但對於複雜應用程序,ThinkPHP 可能更適合。

開發建議:如何利用ThinkPHP框架實現非同步任務 開發建議:如何利用ThinkPHP框架實現非同步任務 Nov 22, 2023 pm 12:01 PM

《開發建議:如何利用ThinkPHP框架實現非同步任務》隨著網路技術的快速發展,Web應用程式對於處理大量並發請求和複雜業務邏輯的需求也越來越高。為了提高系統的效能和使用者體驗,開發人員常常會考慮利用非同步任務來執行一些耗時操作,例如發送郵件、處理文件上傳、產生報表等。在PHP領域,ThinkPHP框架作為一個流行的開發框架,提供了一些便捷的方式來實現非同步任務。

thinkphp怎麼安裝 thinkphp怎麼安裝 Apr 09, 2024 pm 05:42 PM

ThinkPHP 安裝步驟:準備 PHP、Composer、MySQL 環境。使用 Composer 建立專案。安裝 ThinkPHP 框架及相依性。配置資料庫連線。產生應用程式碼。啟動應用程式並造訪 http://localhost:8000。

thinkphp效能怎麼樣 thinkphp效能怎麼樣 Apr 09, 2024 pm 05:24 PM

ThinkPHP 是一款高效能的 PHP 框架,具備快取機制、程式碼最佳化、平行處理和資料庫最佳化等優勢。官方性能測試顯示,它每秒可處理超過 10,000 個請求,實際應用中被廣泛用於京東商城、攜程網等大型網站和企業系統。

開發建議:如何利用ThinkPHP框架進行API開發 開發建議:如何利用ThinkPHP框架進行API開發 Nov 22, 2023 pm 05:18 PM

開發建議:如何利用ThinkPHP框架進行API開發隨著網際網路的不斷發展,API(ApplicationProgrammingInterface)的重要性也日益凸顯。 API是不同應用程式之間進行通訊的橋樑,它可以實現資料共享、功能呼叫等操作,為開發者提供了相對簡單且快速的開發方式。而ThinkPHP框架作為一款優秀的PHP開發框架,具有高效能、可擴展且易用

See all articles