fuc/src/Command/Cron.php
2025-06-29 18:13:58 +08:00

153 lines
4.5 KiB
PHP
Executable File

<?php
declare(strict_types=1);
namespace App\Command;
use App\Models\Config;
use App\Services\Cron as CronService;
use App\Services\Detect;
use Exception;
use Telegram\Bot\Exceptions\TelegramSDKException;
use function mktime;
use function time;
final class Cron extends Command
{
public string $description = <<<EOL
├─=: php xcat Cron - 站点定时任务,每五分钟
EOL;
/**
* @throws TelegramSDKException
* @throws Exception
*/
public function boot(): void
{
ini_set('memory_limit', '-1');
// Log current hour & minute
$hour = (int) date('H');
$minute = (int) date('i');
$jobs = new CronService();
// Run new shop related jobs
$jobs->processPendingOrder(); // 标记订单 - 待激活,超时
$jobs->processTabpOrderActivation(); // 激活订单,过期订单(不重置用户流量)
$jobs->processBandwidthOrderActivation(); // 流量包处理,没用
$jobs->processTimeOrderActivation(); // 时间包处理,没用
// Run user related jobs
$jobs->expireFreeUserAccount(); // 重置免费用户流量
$jobs->expirePaidUserAccount(); // 重置套餐用户流量
$jobs->sendPaidUserUsageLimitNotification(); // 流量预警
// Run node related jobs
$jobs->updateNodeIp();
if ($_ENV['enable_detect_offline']) {
$jobs->detectNodeOffline();
}
// Run daily job
if ($hour === Config::obtain('daily_job_hour') &&
$minute === Config::obtain('daily_job_minute') &&
time() - Config::obtain('last_daily_job_time') > 86399
) {
$jobs->cleanDb();
// $jobs->resetNodeBandwidth();
$jobs->resetDuckBandwidth(); // 重置每日的duck流量
$jobs->reportBandWidth(); // 报告流量给tg
$jobs->resetFreeUserBandwidth(); // 重置免费用户的流量
$jobs->sendDailyTrafficReport(); // 流量报告?不重要
$jobs->clearTodayWidth(); // 重置每日的用户使用流量
// 每日清除前一天残留TgStep
$jobs->clearTgStep();
// 每日统计报表 - 访客
// $jobs->visitorCount();
// 每日统计报表 - 用户
$jobs->userCount();
if (Config::obtain('enable_detect_inactive_user')) {
$jobs->detectInactiveUser();
}
if (Config::obtain('remove_inactive_user_link_and_invite')) {
$jobs->removeInactiveUserLinkAndInvite();
}
if (Config::obtain('telegram_diary')) {
$jobs->sendTelegramDiary();
}
$jobs->resetTodayBandwidth();
if (Config::obtain('telegram_daily_job')) {
$jobs->sendTelegramDailyJob();
}
(new Config())->where('item', 'last_daily_job_time')->update([
'value' => mktime(
Config::obtain('daily_job_hour'),
Config::obtain('daily_job_minute'),
0,
(int) date('m'),
(int) date('d'),
(int) date('Y')
),
]);
}
// Daily finance report
if (Config::obtain('enable_daily_finance_mail')
&& $hour === 0
&& $minute === 0
) {
$jobs->sendDailyFinanceMail();
}
// Weekly finance report
if (Config::obtain('enable_weekly_finance_mail')
&& $hour === 0
&& $minute === 0
&& date('w') === '1'
) {
$jobs->sendWeeklyFinanceMail();
}
// Monthly finance report
if (Config::obtain('enable_monthly_finance_mail')
&& $hour === 0
&& $minute === 0
&& date('d') === '01'
) {
$jobs->sendMonthlyFinanceMail();
}
// Detect GFW
if (Config::obtain('enable_detect_gfw') && $minute === 0
) {
$detect = new Detect();
$detect->gfw();
}
// Detect ban
if (Config::obtain('enable_detect_ban') && $minute === 0
) {
$detect = new Detect();
$detect->ban();
}
if ($minute === 0) {
$jobs->mergeBandWidth();
}
// Run email queue
$jobs->processEmailQueue();
$jobs->processEmailQueue_bulk(); // 群发
}
}