Task.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. /**
  3. * Niushop商城系统 - 团队十年电商经验汇集巨献!
  4. * =========================================================
  5. * Copy right 2019-2029 山西牛酷信息科技有限公司, 保留所有权利。
  6. * ----------------------------------------------
  7. * 官方网址: https://www.niushop.com.cn
  8. * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和使用。
  9. * 任何企业和个人不允许对程序代码以任何形式任何目的再发布。
  10. * =========================================================
  11. */
  12. namespace app\cron\controller;
  13. use app\Controller;
  14. use think\facade\Log;
  15. use think\facade\Cache;
  16. /**
  17. * 计划任务
  18. * @author Administrator
  19. */
  20. class Task extends Controller
  21. {
  22. /**
  23. * 执行计划任务(单独计划任务)
  24. */
  25. public function execute()
  26. {
  27. //设置计划任务标识
  28. $last_time = Cache::get("cron_last_load_time");
  29. if ($last_time == false) {
  30. $last_time = 0;
  31. }
  32. $cron_model = new \app\model\system\Cron();
  33. do {
  34. ignore_user_abort(true);
  35. set_time_limit(0);
  36. $last_time = Cache::get("cron_last_load_time");
  37. if (empty($last_time)) {
  38. $last_time = 0;
  39. }
  40. $time = time();
  41. if (($time - $last_time) < 30) {
  42. Log::write("跳出多余循环事件,保证当前只存在一个循环");
  43. exit();//跳出循环
  44. }
  45. $cron_model->execute();
  46. Log::write("检测事件执行");
  47. Cache::set("cron_last_load_time", time());
  48. sleep(60);
  49. } while (TRUE);
  50. }
  51. /**
  52. * php自动执行事件
  53. */
  54. public function phpCron()
  55. {
  56. $url = url('cron/task/execute');
  57. http($url, 1);
  58. }
  59. }