Cron.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CatchAdmin [Just Like ~ ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2020 http://catchadmin.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
  8. // +----------------------------------------------------------------------
  9. // | Author: JaguarJack [ njphper@gmail.com ]
  10. // +----------------------------------------------------------------------
  11. namespace catcher\library\crontab;
  12. use Cron\CronExpression;
  13. use think\facade\Console;
  14. /**
  15. * Class Cron
  16. * @package catcher\library\crontab
  17. *
  18. * // cron 表达式
  19. * * * * *
  20. * - - - - -
  21. * | | | | |
  22. * | | | | |
  23. * | | | | +----- day of week (0 - 6) (Sunday=0)
  24. * | | | +---------- month (1 - 12)
  25. * | | +--------------- day of month (1 - 31)
  26. * | +-------------------- hour (0 - 23)
  27. * +------------------------- min (0 - 59)
  28. *
  29. *
  30. *
  31. */
  32. class Cron
  33. {
  34. use Frequencies;
  35. /**
  36. * crontab 表达式
  37. *
  38. * @var string
  39. */
  40. protected $expression = '* * * * *';
  41. /**
  42. * task 任务
  43. *
  44. * @var string
  45. */
  46. protected $task;
  47. /**
  48. * console 命令
  49. *
  50. * @var string
  51. */
  52. protected $console;
  53. /**
  54. * console 参数
  55. *
  56. * @var array
  57. */
  58. protected $arguments;
  59. /**
  60. * 秒级支持
  61. *
  62. * @var int
  63. */
  64. protected $second;
  65. public function __construct($name, $arguments = [])
  66. {
  67. if (is_string($name)) {
  68. $this->console = $name;
  69. }
  70. if (is_object($name)) {
  71. $this->task = $name;
  72. }
  73. $this->arguments = $arguments;
  74. }
  75. /**
  76. * 运行 cron 任务
  77. *
  78. * @time 2020年07月04日
  79. * @return void
  80. */
  81. public function run()
  82. {
  83. if ($this->console) {
  84. Console::call($this->console, $this->arguments);
  85. }
  86. if ($this->task && method_exists($this->task, 'run')) {
  87. $this->task->run();
  88. }
  89. }
  90. /**
  91. * 是否可运行
  92. *
  93. * @time 2020年07月04日
  94. * @return bool
  95. */
  96. public function can()
  97. {
  98. if ($this->second) {
  99. $now = date('s', time());
  100. return ($now % $this->second) == 0;
  101. }
  102. if ($this->expression) {
  103. $cron = CronExpression::factory($this->expression);
  104. return $cron->getNextRunDate(date('Y-m-d H:i:s'), 0 , true)->getTimestamp() == time();
  105. }
  106. return false;
  107. }
  108. }