Cron.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Command;
  4. use App\Models\Setting;
  5. use App\Services\Cron as CronService;
  6. use App\Services\Detect;
  7. use Exception;
  8. use Telegram\Bot\Exceptions\TelegramSDKException;
  9. use function mktime;
  10. use function time;
  11. final class Cron extends Command
  12. {
  13. public string $description = <<<EOL
  14. ├─=: php xcat Cron - 站点定时任务,每五分钟
  15. EOL;
  16. /**
  17. * @throws TelegramSDKException
  18. * @throws Exception
  19. */
  20. public function boot(): void
  21. {
  22. ini_set('memory_limit', '-1');
  23. // Log current hour & minute
  24. $hour = (int) date('H');
  25. $minute = (int) date('i');
  26. $jobs = new CronService();
  27. // Run new shop related jobs
  28. $jobs->processPendingOrder();
  29. $jobs->processTabpOrderActivation();
  30. $jobs->processBandwidthOrderActivation();
  31. $jobs->processTimeOrderActivation();
  32. // Run user related jobs
  33. $jobs->expirePaidUserAccount();
  34. $jobs->sendPaidUserUsageLimitNotification();
  35. // Run node related jobs
  36. $jobs->updateNodeIp();
  37. if ($_ENV['enable_detect_offline']) {
  38. $jobs->detectNodeOffline();
  39. }
  40. // Run traffic log job
  41. if ($minute === 0 && $_ENV['trafficLog']) {
  42. $jobs->addTrafficLog();
  43. }
  44. // Run daily job
  45. if ($hour === Setting::obtain('daily_job_hour') &&
  46. $minute === Setting::obtain('daily_job_minute') &&
  47. time() - Setting::obtain('last_daily_job_time') > 86399
  48. ) {
  49. $jobs->cleanDb();
  50. $jobs->resetNodeBandwidth();
  51. $jobs->resetFreeUserTraffic();
  52. $jobs->sendDailyTrafficReport();
  53. if (Setting::obtain('enable_detect_inactive_user')) {
  54. $jobs->detectInactiveUser();
  55. }
  56. if (Setting::obtain('telegram_diary')) {
  57. $jobs->sendTelegramDiary();
  58. }
  59. $jobs->resetTodayTraffic();
  60. if (Setting::obtain('telegram_daily_job')) {
  61. $jobs->sendTelegramDailyJob();
  62. }
  63. Setting::where('item', 'last_daily_job_time')->update([
  64. 'value' => mktime(
  65. Setting::obtain('daily_job_hour'),
  66. Setting::obtain('daily_job_minute'),
  67. 0,
  68. (int) date('m'),
  69. (int) date('d'),
  70. (int) date('Y')
  71. ),
  72. ]);
  73. }
  74. // Daily finance report
  75. if (Setting::obtain('enable_daily_finance_mail')
  76. && $hour === 0
  77. && $minute === 0
  78. ) {
  79. $jobs->sendDailyFinanceMail();
  80. }
  81. // Weekly finance report
  82. if (Setting::obtain('enable_weekly_finance_mail')
  83. && $hour === 0
  84. && $minute === 0
  85. && date('w') === '1'
  86. ) {
  87. $jobs->sendWeeklyFinanceMail();
  88. }
  89. // Monthly finance report
  90. if (Setting::obtain('enable_monthly_finance_mail')
  91. && $hour === 0
  92. && $minute === 0
  93. && date('d') === '01'
  94. ) {
  95. $jobs->sendMonthlyFinanceMail();
  96. }
  97. // Detect GFW
  98. if (Setting::obtain('enable_detect_gfw') && $minute === 0
  99. ) {
  100. $detect = new Detect();
  101. $detect->gfw();
  102. }
  103. // Detect ban
  104. if (Setting::obtain('enable_detect_ban') && $minute === 0
  105. ) {
  106. $detect = new Detect();
  107. $detect->ban();
  108. }
  109. // Run email queue
  110. $jobs->processEmailQueue();
  111. }
  112. }