Timer.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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\Lib;
  15. use Workerman\Events\EventInterface;
  16. use Exception;
  17. /**
  18. * Timer.
  19. *
  20. * example:
  21. * Workerman\Lib\Timer::add($time_interval, callback, array($arg1, $arg2..));
  22. */
  23. class Timer
  24. {
  25. /**
  26. * Tasks that based on ALARM signal.
  27. * [
  28. * run_time => [[$func, $args, $persistent, time_interval],[$func, $args, $persistent, time_interval],..]],
  29. * run_time => [[$func, $args, $persistent, time_interval],[$func, $args, $persistent, time_interval],..]],
  30. * ..
  31. * ]
  32. *
  33. * @var array
  34. */
  35. protected static $_tasks = array();
  36. /**
  37. * event
  38. *
  39. * @var \Workerman\Events\EventInterface
  40. */
  41. protected static $_event = null;
  42. /**
  43. * Init.
  44. *
  45. * @param \Workerman\Events\EventInterface $event
  46. * @return void
  47. */
  48. public static function init($event = null)
  49. {
  50. if ($event) {
  51. self::$_event = $event;
  52. } else {
  53. if (function_exists('pcntl_signal')) {
  54. pcntl_signal(SIGALRM, array('\Workerman\Lib\Timer', 'signalHandle'), false);
  55. }
  56. }
  57. }
  58. /**
  59. * ALARM signal handler.
  60. *
  61. * @return void
  62. */
  63. public static function signalHandle()
  64. {
  65. if (!self::$_event) {
  66. pcntl_alarm(1);
  67. self::tick();
  68. }
  69. }
  70. /**
  71. * Add a timer.
  72. *
  73. * @param float $time_interval
  74. * @param callable $func
  75. * @param mixed $args
  76. * @param bool $persistent
  77. * @return int/false
  78. */
  79. public static function add($time_interval, $func, $args = array(), $persistent = true)
  80. {
  81. if ($time_interval <= 0) {
  82. echo new Exception("bad time_interval");
  83. return false;
  84. }
  85. if (self::$_event) {
  86. return self::$_event->add($time_interval,
  87. $persistent ? EventInterface::EV_TIMER : EventInterface::EV_TIMER_ONCE, $func, $args);
  88. }
  89. if (!is_callable($func)) {
  90. echo new Exception("not callable");
  91. return false;
  92. }
  93. if (empty(self::$_tasks)) {
  94. pcntl_alarm(1);
  95. }
  96. $time_now = time();
  97. $run_time = $time_now + $time_interval;
  98. if (!isset(self::$_tasks[$run_time])) {
  99. self::$_tasks[$run_time] = array();
  100. }
  101. self::$_tasks[$run_time][] = array($func, (array)$args, $persistent, $time_interval);
  102. return 1;
  103. }
  104. /**
  105. * Tick.
  106. *
  107. * @return void
  108. */
  109. public static function tick()
  110. {
  111. if (empty(self::$_tasks)) {
  112. pcntl_alarm(0);
  113. return;
  114. }
  115. $time_now = time();
  116. foreach (self::$_tasks as $run_time => $task_data) {
  117. if ($time_now >= $run_time) {
  118. foreach ($task_data as $index => $one_task) {
  119. $task_func = $one_task[0];
  120. $task_args = $one_task[1];
  121. $persistent = $one_task[2];
  122. $time_interval = $one_task[3];
  123. try {
  124. call_user_func_array($task_func, $task_args);
  125. } catch (\Exception $e) {
  126. echo $e;
  127. }
  128. if ($persistent) {
  129. self::add($time_interval, $task_func, $task_args);
  130. }
  131. }
  132. unset(self::$_tasks[$run_time]);
  133. }
  134. }
  135. }
  136. /**
  137. * Remove a timer.
  138. *
  139. * @param mixed $timer_id
  140. * @return bool
  141. */
  142. public static function del($timer_id)
  143. {
  144. if (self::$_event) {
  145. return self::$_event->del($timer_id, EventInterface::EV_TIMER);
  146. }
  147. return false;
  148. }
  149. /**
  150. * Remove all timers.
  151. *
  152. * @return void
  153. */
  154. public static function delAll()
  155. {
  156. self::$_tasks = array();
  157. pcntl_alarm(0);
  158. if (self::$_event) {
  159. self::$_event->clearAllTimer();
  160. }
  161. }
  162. }