Timer.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace crmeb\command;
  12. use think\console\Command;
  13. use think\console\Input;
  14. use think\console\input\Argument;
  15. use think\console\input\Option;
  16. use think\console\Output;
  17. use Workerman\Worker;
  18. class Timer extends Command
  19. {
  20. /**
  21. * @var int
  22. */
  23. protected $timer;
  24. /**
  25. * @var int|float
  26. */
  27. protected $interval = 1;
  28. protected function configure()
  29. {
  30. // 指令配置
  31. $this->setName('timer')
  32. ->addArgument('status', Argument::REQUIRED, 'start/stop/reload/status/connections')
  33. ->addOption('d', null, Option::VALUE_NONE, 'daemon(守护进程)方式启动')
  34. ->addOption('i', null, Option::VALUE_OPTIONAL, '多长时间执行一次,可以精确到0.001')
  35. ->setDescription('start/stop/restart 定时任务');
  36. }
  37. protected function init(Input $input, Output $output)
  38. {
  39. global $argv;
  40. if ($input->hasOption('i'))
  41. $this->interval = floatval($input->getOption('i'));
  42. $argv[1] = $input->getArgument('status') ?: 'start';
  43. if ($input->hasOption('d')) {
  44. $argv[2] = '-d';
  45. } else {
  46. unset($argv[2]);
  47. }
  48. }
  49. protected function execute(Input $input, Output $output)
  50. {
  51. $this->init($input, $output);
  52. Worker::$pidFile = app()->getRootPath().'runtime/timer.pid';
  53. $task = new Worker();
  54. date_default_timezone_set('PRC');
  55. $task->count = 1;
  56. $task->onWorkerStart = function () {
  57. event('CrontabListener');
  58. };
  59. $task->runAll();
  60. }
  61. }