27 lines
1.0 KiB
PHP
27 lines
1.0 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use App\Models\MailDailyReport;
|
|
use App\Services\MailFlow\DailyReportService;
|
|
use App\Services\MailFlow\Settings;
|
|
use Illuminate\Console\Command;
|
|
|
|
class ReportMailUsage extends Command
|
|
{
|
|
protected $signature = 'mail:report-usage';
|
|
protected $description = 'Snapshot the complete 08:00-to-08:00 mail period and report to master';
|
|
|
|
public function handle(DailyReportService $reports): int
|
|
{
|
|
if (!Settings::get('enabled') || !Settings::get('report_enabled')) { return self::SUCCESS; }
|
|
// 00:00 UTC is 08:00 Asia/Shanghai, independent of the application's timezone.
|
|
$reports->snapshot(now()->utc()->startOfDay());
|
|
MailDailyReport::where(function ($query) {
|
|
$query->whereIn('status', ['pending', 'failed'])->orWhere(fn ($q) => $q->where('status', 'sending')->where('updated_at', '<', now()->subMinutes(2)));
|
|
})->where('attempts', '<', 3)
|
|
->orderBy('id')->limit(10)->get()->each(fn ($report) => $reports->send($report));
|
|
return self::SUCCESS;
|
|
}
|
|
}
|