Crontab.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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\Crontab;
  15. use Workerman\Timer;
  16. /**
  17. * Class Crontab
  18. * @package Workerman\Crontab
  19. */
  20. class Crontab
  21. {
  22. /**
  23. * @var string
  24. */
  25. protected $_rule;
  26. /**
  27. * @var callable
  28. */
  29. protected $_callback;
  30. /**
  31. * @var string
  32. */
  33. protected $_name;
  34. /**
  35. * @var int
  36. */
  37. protected $_id;
  38. /**
  39. * @var array
  40. */
  41. protected static $_instances = [];
  42. /**
  43. * Crontab constructor.
  44. * @param string $rule
  45. * @param callable $callback
  46. * @param string $name
  47. */
  48. public function __construct($rule, $callback, $name = '')
  49. {
  50. $this->_rule = $rule;
  51. $this->_callback = $callback;
  52. $this->_name = $name;
  53. $this->_id = static::createId();
  54. static::$_instances[$this->_id] = $this;
  55. static::tryInit();
  56. }
  57. /**
  58. * @return string
  59. */
  60. public function getRule()
  61. {
  62. return $this->_rule;
  63. }
  64. /**
  65. * @return callable
  66. */
  67. public function getCallback()
  68. {
  69. return $this->_callback;
  70. }
  71. /**
  72. * @return string
  73. */
  74. public function getName()
  75. {
  76. return $this->_name;
  77. }
  78. /**
  79. * @return int
  80. */
  81. public function getId()
  82. {
  83. return $this->_id;
  84. }
  85. /**
  86. * @return bool
  87. */
  88. public function destroy()
  89. {
  90. return static::remove($this->_id);
  91. }
  92. /**
  93. * @return array
  94. */
  95. public static function getAll()
  96. {
  97. return static::$_instances;
  98. }
  99. /**
  100. * @param $id
  101. * @return bool
  102. */
  103. public static function remove($id)
  104. {
  105. if ($id instanceof Crontab) {
  106. $id = $id->getId();
  107. }
  108. if (!isset(static::$_instances[$id])) {
  109. return false;
  110. }
  111. unset(static::$_instances[$id]);
  112. return true;
  113. }
  114. /**
  115. * @return int
  116. */
  117. protected static function createId()
  118. {
  119. static $id = 0;
  120. return ++$id;
  121. }
  122. /**
  123. * tryInit
  124. */
  125. protected static function tryInit()
  126. {
  127. static $inited = false;
  128. if ($inited) {
  129. return;
  130. }
  131. $inited = true;
  132. $parser = new Parser();
  133. $callback = function () use ($parser, &$callback) {
  134. foreach (static::$_instances as $crontab) {
  135. $rule = $crontab->getRule();
  136. $cb = $crontab->getCallback();
  137. if (!$cb || !$rule) {
  138. continue;
  139. }
  140. $times = $parser->parse($rule);
  141. $now = time();
  142. foreach ($times as $time) {
  143. $t = $time-$now;
  144. if ($t <= 0) {
  145. $t = 0.000001;
  146. }
  147. Timer::add($t, $cb, null, false);
  148. }
  149. }
  150. Timer::add(60 - time()%60, $callback, null, false);
  151. };
  152. $next_time = time()%60;
  153. if ($next_time == 0) {
  154. $next_time = 0.00001;
  155. } else {
  156. $next_time = 60 - $next_time;
  157. }
  158. Timer::add($next_time, $callback, null, false);
  159. }
  160. }