Timer.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2024 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\timer\command;
  13. use think\console\Command;
  14. use think\console\Input;
  15. use think\console\input\Argument;
  16. use think\console\input\Option;
  17. use think\console\Output;
  18. use think\facade\Event;
  19. use Workerman\Worker;
  20. /**
  21. * 定时器 (Workerman)
  22. * 用于执行系统里的定时任务, 例如自动确认收货
  23. * 使用方法: 打开命令行 - 执行命令: php think timer start
  24. * Class Timer
  25. * @package app\common\command
  26. */
  27. class Timer extends Command
  28. {
  29. // 定时器句柄/ID
  30. protected $timer;
  31. // 时间间隔 (单位: 秒, 默认5秒)
  32. protected $interval = 1;
  33. protected function configure()
  34. {
  35. // 指令配置
  36. $this->setName('timer')
  37. ->addArgument('status', Argument::REQUIRED, 'start/stop/reload/status/connections')
  38. ->addOption('d', null, Option::VALUE_NONE, 'daemon(守护进程)方式启动')
  39. ->addOption('i', null, Option::VALUE_OPTIONAL, '多长时间执行一次')
  40. ->setDescription('start/stop/restart 定时任务');
  41. }
  42. protected function init(Input $input, Output $output)
  43. {
  44. global $argv;
  45. if ($input->hasOption('i'))
  46. $this->interval = floatval($input->getOption('i'));
  47. $argv[1] = $input->getArgument('status') ?: 'start';
  48. if ($input->hasOption('d')) {
  49. $argv[2] = '-d';
  50. } else {
  51. unset($argv[2]);
  52. }
  53. }
  54. /**
  55. * 创建定时器
  56. * @param Input $input
  57. * @param Output $output
  58. * @return int|void|null
  59. */
  60. protected function execute(Input $input, Output $output)
  61. {
  62. $this->init($input, $output);
  63. // 创建定时器任务
  64. $worker = new Worker();
  65. $worker->onWorkerStart = [$this, 'start'];
  66. $worker->runAll();
  67. }
  68. /**
  69. * 定时器执行的内容
  70. * @return false|int
  71. */
  72. public function start()
  73. {
  74. // 每隔n秒执行一次
  75. return $this->timer = \Workerman\Lib\Timer::add($this->interval, function () {
  76. try {
  77. // 这里执行系统预设的定时任务事件
  78. echo 'timer...' . PHP_EOL;
  79. Event::trigger('StoreTask');
  80. } catch (\Throwable $e) {
  81. echo 'ERROR: ' . $e->getMessage() . PHP_EOL;
  82. $this->stop();
  83. }
  84. });
  85. }
  86. /**
  87. * 停止/删除定时器
  88. * @return bool
  89. */
  90. public function stop()
  91. {
  92. return \Workerman\Lib\Timer::del($this->timer);
  93. }
  94. }