Cron.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace crmeb\utils;
  12. use think\swoole\Manager;
  13. use Swoole\Timer;
  14. use think\facade\Log;
  15. /**
  16. * Cron定时执行
  17. * Class Cron
  18. * @package crmeb\utils
  19. */
  20. class Cron
  21. {
  22. /**
  23. * @var Manager
  24. */
  25. protected $manager;
  26. /**
  27. * @var int
  28. */
  29. protected $workerId = 0;
  30. /**
  31. * @var
  32. */
  33. protected $timer;
  34. /**
  35. * @var bool
  36. */
  37. protected $debug = false;
  38. /**
  39. * Cron constructor.
  40. * @param Manager $manager
  41. */
  42. public function __construct(Manager $manager)
  43. {
  44. $this->manager = $manager;
  45. $this->debug = env('APP_DEBUG', false);
  46. }
  47. /**
  48. * @param int $workerId
  49. * @return Cron
  50. */
  51. public function setWorkerId(int $workerId)
  52. {
  53. $this->workerId = $workerId;
  54. return $this;
  55. }
  56. /**
  57. * 沙盒运行
  58. * @param callable $callable
  59. */
  60. protected function runInSandbox(callable $callable)
  61. {
  62. $this->manager->runWithBarrier([$this->manager, 'runInSandbox'], function () use ($callable) {
  63. try {
  64. $callable();
  65. } catch (\Throwable $e) {
  66. $this->debug && Log::error($e->getMessage());
  67. }
  68. });
  69. }
  70. /**
  71. * 添加启动定时任务
  72. * @param int $ms
  73. * @param callable $callable
  74. * @return mixed|null
  75. */
  76. public function tick(int $ms, callable $callable)
  77. {
  78. if ($this->workerId === $this->manager->getWorkerId()) {
  79. return Timer::tick($ms, function () use ($callable) {
  80. $this->runInSandbox($callable);
  81. });
  82. } else {
  83. return null;
  84. }
  85. }
  86. /**
  87. * 每天的某时某分运行一次
  88. * minuteTick('12:15',fun()) 例如: 12:15 会在当天的中午12点15分钟运行一次
  89. * @param string $time
  90. * @param callable $callable
  91. * @return mixed|null
  92. */
  93. public function minuteTick(string $time, callable $callable)
  94. {
  95. return $this->tick(1000 * 60, function () use ($callable, $time) {
  96. $nowTime = date('H:i');
  97. if ($nowTime === $time) {
  98. $callable();
  99. }
  100. });
  101. }
  102. /**
  103. * 一次执行
  104. * @param int $ms
  105. * @param callable $callable
  106. */
  107. public function after(int $ms, callable $callable)
  108. {
  109. // if ($this->workerId === $this->manager->getWorkerId()) {
  110. Timer::after($ms, function () use ($callable) {
  111. $this->runInSandbox($callable);
  112. });
  113. // }
  114. }
  115. /**
  116. * 清除定时任务
  117. * @param int $timer
  118. */
  119. public function clear(int $timer)
  120. {
  121. Timer::clear($timer);
  122. }
  123. /**
  124. * 清除所有定时任务
  125. */
  126. public function clearAll()
  127. {
  128. Timer::clearAll();
  129. }
  130. }