CronKeeper.php 924 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <?php
  2. namespace app\process;
  3. use app\framework\Log\SimpleLog;
  4. use Illuminate\Support\Facades\DB;
  5. use Illuminate\Support\Facades\Redis;
  6. use Liebig\Cron\Cron;
  7. use function foo\func;
  8. class CronKeeper
  9. {
  10. use Fork;
  11. public function main()
  12. {
  13. // app()->share(function ($app) {
  14. // return new Cron;
  15. // });
  16. app()->singleton('cron',function () {
  17. return new Cron;
  18. });
  19. }
  20. public function run()
  21. {
  22. //改为redis锁,防止集群极端并发
  23. if (!Redis::setnx('CronRunning', gethostname() . '[' . getmypid() . ']')) {
  24. // 60秒内运行过了
  25. // 验证CronRunning的过期时间
  26. $ttl = Redis::TTL('CronRunning');
  27. if ($ttl == -1 || $ttl > 59) {
  28. Redis::del('CronRunning');
  29. }
  30. return true;
  31. }
  32. Redis::expire('CronRunning', 59);
  33. $this->cProcess(function (){
  34. \Artisan::call("cron:run");
  35. },'cron');
  36. }
  37. }