Timer.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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 app\services\system\crontab\SystemCrontabServices;
  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 Workerman\Worker;
  19. class Timer extends Command
  20. {
  21. /**
  22. * @var int
  23. */
  24. protected $timer;
  25. /**
  26. * @var int|float
  27. */
  28. protected $interval = 1;
  29. protected function configure()
  30. {
  31. // 指令配置
  32. $this->setName('timer')
  33. ->addArgument('status', Argument::REQUIRED, 'start/stop/reload/status/connections')
  34. ->addOption('d', null, Option::VALUE_NONE, 'daemon(守护进程)方式启动')
  35. ->addOption('i', null, Option::VALUE_OPTIONAL, '多长时间执行一次,可以精确到0.001')
  36. ->setDescription('start/stop/restart 定时任务');
  37. }
  38. protected function init(Input $input, Output $output)
  39. {
  40. global $argv;
  41. if ($input->hasOption('i'))
  42. $this->interval = floatval($input->getOption('i'));
  43. $argv[1] = $input->getArgument('status') ?: 'start';
  44. if ($input->hasOption('d')) {
  45. $argv[2] = '-d';
  46. } else {
  47. unset($argv[2]);
  48. }
  49. }
  50. protected function execute(Input $input, Output $output)
  51. {
  52. $this->init($input, $output);
  53. Worker::$pidFile = app()->getRootPath().'runtime/timer.pid';
  54. $task = new Worker();
  55. date_default_timezone_set('PRC');
  56. $task->count = 1;
  57. $task->onWorkerStart = function () {
  58. app()->make(SystemCrontabServices::class)->crontabCommandRun();
  59. };
  60. $task->runAll();
  61. }
  62. }