Timer.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <?php
  2. /**
  3. * This file is part of workerman.
  4. *
  5. * Licensed under The MIT License
  6. * For full copyright and license information, please see the MIT-LICENSE.txt
  7. * Redistributions of files must retain the above copyright notice.
  8. *
  9. * @author walkor<walkor@workerman.net>
  10. * @copyright walkor<walkor@workerman.net>
  11. * @link http://www.workerman.net/
  12. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  13. */
  14. namespace Workerman;
  15. use Workerman\Events\EventInterface;
  16. use Workerman\Events\Select;
  17. use Workerman\Worker;
  18. use \Exception;
  19. /**
  20. * Timer.
  21. */
  22. class Timer
  23. {
  24. /**
  25. * Tasks that based on ALARM signal.
  26. * [
  27. * run_time => [[$func, $args, $persistent, time_interval],[$func, $args, $persistent, time_interval],..]],
  28. * run_time => [[$func, $args, $persistent, time_interval],[$func, $args, $persistent, time_interval],..]],
  29. * ..
  30. * ]
  31. *
  32. * @var array
  33. */
  34. protected static $_tasks = [];
  35. /**
  36. * event
  37. *
  38. * @var Select
  39. */
  40. protected static $_event = null;
  41. /**
  42. * timer id
  43. *
  44. * @var int
  45. */
  46. protected static $_timerId = 0;
  47. /**
  48. * timer status
  49. * [
  50. * timer_id1 => bool,
  51. * timer_id2 => bool,
  52. * ....................,
  53. * ]
  54. *
  55. * @var array
  56. */
  57. protected static $_status = [];
  58. /**
  59. * Init.
  60. *
  61. * @param EventInterface $event
  62. * @return void
  63. */
  64. public static function init($event = null)
  65. {
  66. if ($event) {
  67. self::$_event = $event;
  68. return;
  69. }
  70. if (\function_exists('pcntl_signal')) {
  71. \pcntl_signal(\SIGALRM, ['\Workerman\Timer', 'signalHandle'], false);
  72. }
  73. }
  74. /**
  75. * ALARM signal handler.
  76. *
  77. * @return void
  78. */
  79. public static function signalHandle()
  80. {
  81. if (!self::$_event) {
  82. \pcntl_alarm(1);
  83. self::tick();
  84. }
  85. }
  86. /**
  87. * Add a timer.
  88. *
  89. * @param float $time_interval
  90. * @param callable $func
  91. * @param mixed $args
  92. * @param bool $persistent
  93. * @return int|bool
  94. */
  95. public static function add(float $time_interval, $func, $args = [], $persistent = true)
  96. {
  97. if ($time_interval < 0) {
  98. Worker::safeEcho(new Exception("bad time_interval"));
  99. return false;
  100. }
  101. if ($args === null) {
  102. $args = [];
  103. }
  104. if (self::$_event) {
  105. return $persistent ? self::$_event->repeat($time_interval, $func, $args) : self::$_event->delay($time_interval, $func, $args);
  106. }
  107. if (!\is_callable($func)) {
  108. Worker::safeEcho(new Exception("not callable"));
  109. return false;
  110. }
  111. if (empty(self::$_tasks)) {
  112. \pcntl_alarm(1);
  113. }
  114. $run_time = \time() + $time_interval;
  115. if (!isset(self::$_tasks[$run_time])) {
  116. self::$_tasks[$run_time] = [];
  117. }
  118. self::$_timerId = self::$_timerId == \PHP_INT_MAX ? 1 : ++self::$_timerId;
  119. self::$_status[self::$_timerId] = true;
  120. self::$_tasks[$run_time][self::$_timerId] = [$func, (array)$args, $persistent, $time_interval];
  121. return self::$_timerId;
  122. }
  123. /**
  124. * @param float $delay
  125. * @param $func
  126. * @param array $args
  127. * @return bool|int
  128. */
  129. public function delay(float $delay, $func, $args = [])
  130. {
  131. return $this->add($delay, $func, $args);
  132. }
  133. /**
  134. * Tick.
  135. *
  136. * @return void
  137. */
  138. public static function tick()
  139. {
  140. if (empty(self::$_tasks)) {
  141. \pcntl_alarm(0);
  142. return;
  143. }
  144. $time_now = \time();
  145. foreach (self::$_tasks as $run_time => $task_data) {
  146. if ($time_now >= $run_time) {
  147. foreach ($task_data as $index => $one_task) {
  148. $task_func = $one_task[0];
  149. $task_args = $one_task[1];
  150. $persistent = $one_task[2];
  151. $time_interval = $one_task[3];
  152. try {
  153. $task_func(...$task_args);
  154. } catch (\Throwable $e) {
  155. Worker::safeEcho($e);
  156. }
  157. if($persistent && !empty(self::$_status[$index])) {
  158. $new_run_time = \time() + $time_interval;
  159. if(!isset(self::$_tasks[$new_run_time])) self::$_tasks[$new_run_time] = [];
  160. self::$_tasks[$new_run_time][$index] = [$task_func, (array)$task_args, $persistent, $time_interval];
  161. }
  162. }
  163. unset(self::$_tasks[$run_time]);
  164. }
  165. }
  166. }
  167. /**
  168. * Remove a timer.
  169. *
  170. * @param mixed $timer_id
  171. * @return bool
  172. */
  173. public static function del($timer_id)
  174. {
  175. if (self::$_event) {
  176. return self::$_event->deleteTimer($timer_id);
  177. }
  178. foreach(self::$_tasks as $run_time => $task_data)
  179. {
  180. if(array_key_exists($timer_id, $task_data)) unset(self::$_tasks[$run_time][$timer_id]);
  181. }
  182. if(array_key_exists($timer_id, self::$_status)) unset(self::$_status[$timer_id]);
  183. return true;
  184. }
  185. /**
  186. * Remove all timers.
  187. *
  188. * @return void
  189. */
  190. public static function delAll()
  191. {
  192. self::$_tasks = self::$_status = [];
  193. \pcntl_alarm(0);
  194. if (self::$_event) {
  195. self::$_event->deleteAllTimer();
  196. }
  197. }
  198. }