diff --git a/app/Console/Commands/Test.php b/app/Console/Commands/Test.php index edc3b2e4..1f65219c 100644 --- a/app/Console/Commands/Test.php +++ b/app/Console/Commands/Test.php @@ -98,8 +98,7 @@ class Test extends Command */ public function handle() { - $diff = get_setting("system.alarm_email_receiver"); - dd($diff); + CleanupRepository::checkQueueFailedJobs(); } } diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index d7db3ca9..809698a6 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -3,7 +3,9 @@ namespace App\Console; use App\Jobs\CheckCleanup; +use App\Jobs\CheckQueueFailedJobs; use Carbon\Carbon; +use Illuminate\Console\Scheduling\Event; use Illuminate\Console\Scheduling\Schedule; use Illuminate\Foundation\Console\Kernel as ConsoleKernel; @@ -38,6 +40,7 @@ class Kernel extends ConsoleKernel })->withoutOverlapping(); $schedule->command('meilisearch:import')->weeklyOn(1, "03:00")->withoutOverlapping(); $schedule->command('torrent:load_pieces_hash')->dailyAt("01:00")->withoutOverlapping(); + $schedule->job(new CheckQueueFailedJobs())->everySixHours()->withoutOverlapping(); $this->registerScheduleCleanup($schedule); } diff --git a/app/Jobs/CheckQueueFailedJobs.php b/app/Jobs/CheckQueueFailedJobs.php new file mode 100644 index 00000000..b06f952a --- /dev/null +++ b/app/Jobs/CheckQueueFailedJobs.php @@ -0,0 +1,37 @@ +whereIn("id", $receiverUidArr)->get(User::$commonFields); foreach ($users as $user) { $locale = $user->locale; - $subject = self::getAlarmEmailSubject($locale); - $msg = self::getAlarmEmailBody($now, $level, $lastTime, $interval, $locale); + $subject = self::getAlarmEmailSubjectForCleanup($locale); + $msg = self::getAlarmEmailBodyForCleanup($now, $level, $lastTime, $interval, $locale); $result = $toolRep->sendMail($user->email, $subject, $msg); do_log(sprintf("send msg: %s result: %s", $msg, var_export($result, true)), $result ? "info" : "error"); } @@ -291,12 +291,12 @@ LUA; } } - private static function getAlarmEmailSubject(string|null $locale = null) + private static function getAlarmEmailSubjectForCleanup(string|null $locale = null) { return nexus_trans("cleanup.alarm_email_subject", ["site_name" => get_setting("basic.SITENAME")], $locale); } - private static function getAlarmEmailBody(Carbon $now, string $level, int $lastTime, int $interval, string|null $locale = null) + private static function getAlarmEmailBodyForCleanup(Carbon $now, string $level, int $lastTime, int $interval, string|null $locale = null) { return nexus_trans("cleanup.alarm_email_body", [ "now_time" => $now->toDateTimeString(), @@ -308,4 +308,49 @@ LUA; "interval_human" => mkprettytime($interval), ], $locale); } + + public static function checkQueueFailedJobs(): void + { + $now = Carbon::now(); + $since = $now->subHours(6)->toDateTimeString(); + $failedJobsTable = nexus_config("queue.failed.table"); + $failedJobsCount = NexusDB::table($failedJobsTable)->where("failed_at", ">=", $since)->count(); + if ($failedJobsCount == 0) { + do_log(sprintf("no failed jobs since: %s", $since)); + return; + } + $receiverUid = get_setting("system.alarm_email_receiver"); + do_log("receiverUid: $receiverUid"); + $toolRep = new ToolRepository(); + if (empty($receiverUid)) { + $locale = Locale::getDefault(); + $subject = self::getAlarmEmailSubjectForQueueFailedJobs($locale); + $msg = self::getAlarmEmailBodyForQueueFailedJobs($since, $failedJobsCount, $failedJobsTable, $locale); + do_log(sprintf("%s - %s", $subject, $msg), "error"); + } else { + $receiverUidArr = preg_split("/\s+/", $receiverUid); + $users = User::query()->whereIn("id", $receiverUidArr)->get(User::$commonFields); + foreach ($users as $user) { + $locale = $user->locale; + $subject = self::getAlarmEmailSubjectForQueueFailedJobs($locale); + $msg = self::getAlarmEmailBodyForQueueFailedJobs($since, $failedJobsCount, $failedJobsTable, $locale); + $result = $toolRep->sendMail($user->email, $subject, $msg); + do_log(sprintf("send msg: %s result: %s", $msg, var_export($result, true)), $result ? "info" : "error"); + } + } + } + + private static function getAlarmEmailSubjectForQueueFailedJobs(string|null $locale = null) + { + return nexus_trans("cleanup.alarm_email_subject_for_queue_failed_jobs", ["site_name" => get_setting("basic.SITENAME")], $locale); + } + + private static function getAlarmEmailBodyForQueueFailedJobs(string $since, int $count, string $failedJobTable, string|null $locale = null) + { + return nexus_trans("cleanup.alarm_email_body_for_queue_failed_jobs", [ + "since" => $since, + "count" => $count, + "failed_job_table" => $failedJobTable, + ], $locale); + } } diff --git a/include/constants.php b/include/constants.php index b8c9a1d6..b991ef23 100644 --- a/include/constants.php +++ b/include/constants.php @@ -1,6 +1,6 @@ 'Timed physical deletion of disabled accounts', 'alarm_email_subject' => '[:site_name] background cleanup task exception', 'alarm_email_body' => 'Current time: :now_time, level :level, Last run time was: :last_time, it has been more than: :elapsed_seconds seconds(:elapsed_seconds_human) since it was run, the set run interval is: :interval seconds(:interval_human), please check!', + 'alarm_email_subject_for_queue_failed_jobs' => '[:site_name]Asynchronous Task Exception', + 'alarm_email_body_for_queue_failed_jobs' => 'There are a total of :count failed asynchronous jobs since :since, recorded in database table :failed_job_table, please check it!', ]; diff --git a/resources/lang/zh_CN/cleanup.php b/resources/lang/zh_CN/cleanup.php index a8493ef9..677c68a6 100644 --- a/resources/lang/zh_CN/cleanup.php +++ b/resources/lang/zh_CN/cleanup.php @@ -10,4 +10,6 @@ return [ 'destroy_disabled_account' => '定时物理删除已封禁账号', 'alarm_email_subject' => '[:site_name]后台清理任务异常', 'alarm_email_body' => '当前时间::now_time, 级别 :level 上次运行时间是::last_time,已经超过::elapsed_seconds 秒(:elapsed_seconds_human)没有运行,设置的运行间隔是::interval 秒(:interval_human),请检查!', + 'alarm_email_subject_for_queue_failed_jobs' => '[:site_name]异步任务异常', + 'alarm_email_body_for_queue_failed_jobs' => '自 :since 起共有 :count 条失败的异步任务,记录在数据表 :failed_job_table 中,请检查!', ]; diff --git a/resources/lang/zh_TW/cleanup.php b/resources/lang/zh_TW/cleanup.php index 68ca86e1..beed7e2b 100644 --- a/resources/lang/zh_TW/cleanup.php +++ b/resources/lang/zh_TW/cleanup.php @@ -10,4 +10,6 @@ return [ 'destroy_disabled_account' => '定時物理刪除已封禁賬號', 'alarm_email_subject' => '[:site_name]後臺清理任務異常', 'alarm_email_body' => '當前時間::now_time, 級別 :level 上次運行時間是::last_time,已經超過::elapsed_seconds 秒(:elapsed_seconds_human)沒有運行,設置的運行間隔是::interval 秒(:interval_human),請檢查!', + 'alarm_email_subject_for_queue_failed_jobs' => '[:site_name]異步任務異常', + 'alarm_email_body_for_queue_failed_jobs' => '自 :since 起共有 :count 條失敗的異步任務,記錄在數據表 :failed_job_table 中,請檢查!', ];