您所在的位置:首页 / 知识分享

教你在tp6中实现毫秒级定时任务功能

2023.03.01

226

小平胸

导入 workerman

  1. composer require workerman/workerman
复制代码
创建 Timer 命令
  1. php think make:command Timer
复制代码
实现 Timer
  1. class Timer extends Command
  2. {
  3.     /**
  4.      * @var int
  5.      */
  6.     protected $timer;
  7.     /**
  8.      * @var int|float
  9.      */
  10.     protected $interval = 2;
  11.     protected function configure()
  12.     {
  13.         // 指令配置
  14.         $this->setName('timer')
  15.             ->addArgument('status', Argument::REQUIRED, 'start/stop/reload/status/connections')
  16.             ->addOption('d', null, Option::VALUE_NONE, 'daemon(守护进程)方式启动')
  17.             ->addOption('i', null, Option::VALUE_OPTIONAL, '多长时间执行一次')
  18.             ->setDescription('开启/关闭/重启 定时任务');
  19.     }
  20.     protected function init(Input $input, Output $output)
  21.     {
  22.         global $argv;
  23.         if ($input->hasOption('i'))
  24.             $this->interval = floatval($input->getOption('i'));
  25.         $argv[1] = $input->getArgument('status') ?: 'start';
  26.         if ($input->hasOption('d')) {
  27.             $argv[2] = '-d';
  28.         } else {
  29.             unset($argv[2]);
  30.         }
  31.     }
  32.     protected function execute(Input $input, Output $output)
  33.     {
  34.         $this->init($input, $output);
  35.         //创建定时器任务
  36.         $task = new Worker();
  37.         $task->count = 1;
  38.         $task->onWorkerStart = [$this, 'start'];
  39.         $task->runAll();
  40.     }
  41.     public function stop()
  42.     {
  43.         //手动暂停定时器
  44.         \Workerman\Lib\Timer::del($this->timer);
  45.     }
  46.     public function start()
  47.     {
  48.         $last = time();
  49.         $task = [6 => $last, 10 => $last, 30 => $last, 60 => $last, 180 => $last, 300 => $last];
  50.         
  51.         $this->timer = \Workerman\Lib\Timer::add($this->interval, function () use (&$task) {
  52.             //每隔2秒执行一次
  53.             try {
  54.                 $now = time();
  55.                 foreach ($task as $sec => $time) {
  56.                     if ($now - $time >= $sec) {
  57.                         //每隔$sec秒执行一次
  58.                         $task[$sec] = $now;
  59.                     }
  60.                 }
  61.             } catch (\Throwable $e) {
  62.             }
  63.         });
  64.     }
  65. }
复制代码
Timer 参数说明
  1. Usage:
  2.   timer [options] [--] <status>
  3. Arguments:
  4.   status                start/stop/reload/status/connections
  5. Options:
  6.       --d               daemon(守护进程)方式启动
  7.       --i[=I]           多长时间执行一次,可以精确到0.001
复制代码
注册 Timer 命令
修改 console.php 文件
  1. 'commands' => [
  2.         //定时任务命令
  3.         'timer'=>\app\command\Timer::class,
  4.     ],
复制代码
启动定时器
  1. php think timer start
复制代码
关闭定时器
  1. php think timer stop

相关新闻

上传图片的时候,ios手机的图片会旋转90°

2019.01.17

3359

在html5中利用canvas对上传图片压缩的时候,ios手机竖着拍照时,图片会旋转90°,其他情况正常。

Ubuntu 16与18 使用 rc.local执行开机启动脚本

2021.02.17

936

在 Ubuntu 16以前,有个很简单的 rc.local ,在这个文件内写上你要执行的命令,在开机后,系统就会以管理员权限去执行这些命令。

PHP 正则表达式记录

2023.03.09

240

PHP 正则表达式记录