feat(queue): write batch activation last_result summary for observability
This commit is contained in:
@@ -55,6 +55,10 @@ class BatchActivateSubscriptionsJob implements ShouldQueue
|
||||
// 批次号:用于把一次队列批量执行关联起来,便于后续追溯/筛选/可观测。
|
||||
$runId = 'BAS' . now()->format('YmdHis') . str_pad((string) random_int(1, 9999), 4, '0', STR_PAD_LEFT);
|
||||
|
||||
$success = 0;
|
||||
$failed = 0;
|
||||
$failedReasonCounts = [];
|
||||
|
||||
foreach ($this->orderIds as $orderId) {
|
||||
/** @var PlatformOrder|null $order */
|
||||
$order = PlatformOrder::query()->find($orderId);
|
||||
@@ -100,16 +104,81 @@ class BatchActivateSubscriptionsJob implements ShouldQueue
|
||||
|
||||
$order->meta = $meta;
|
||||
$order->save();
|
||||
|
||||
$success++;
|
||||
} catch (\Throwable $e) {
|
||||
$failed++;
|
||||
|
||||
$reason = trim((string) $e->getMessage());
|
||||
$reason = $reason !== '' ? $reason : '未知错误';
|
||||
$failedReasonCounts[$reason] = ($failedReasonCounts[$reason] ?? 0) + 1;
|
||||
|
||||
$meta = (array) ($order->meta ?? []);
|
||||
$nowStr = now()->toDateTimeString();
|
||||
|
||||
data_set($meta, 'subscription_activation_error', [
|
||||
'message' => trim((string) $e->getMessage()) !== '' ? trim((string) $e->getMessage()) : '未知错误',
|
||||
'at' => now()->toDateTimeString(),
|
||||
'message' => $reason,
|
||||
'at' => $nowStr,
|
||||
'admin_id' => $this->adminId,
|
||||
]);
|
||||
|
||||
// 即使失败也写入 batch_activation(包含 run_id),确保本批次可追溯/可汇总。
|
||||
data_set($meta, 'batch_activation', [
|
||||
'at' => $nowStr,
|
||||
'admin_id' => $this->adminId,
|
||||
'scope' => $this->scope,
|
||||
'mode' => 'queue',
|
||||
'run_id' => $runId,
|
||||
]);
|
||||
|
||||
$order->meta = $meta;
|
||||
$order->save();
|
||||
}
|
||||
}
|
||||
|
||||
// 最小结果汇总(写入到每个订单的 meta.batch_activation.last_result),便于运营在列表页直接看到“本次队列批量的执行结果”。
|
||||
// 注意:为避免引入新表,当前阶段采取“冗余写入到每条订单”策略;后续可演进为独立批次表或日志表。
|
||||
$topReasons = [];
|
||||
if ($failed > 0 && count($failedReasonCounts) > 0) {
|
||||
arsort($failedReasonCounts);
|
||||
$top = array_slice($failedReasonCounts, 0, 3, true);
|
||||
foreach ($top as $reason => $cnt) {
|
||||
$topReasons[] = [
|
||||
'reason' => mb_substr((string) $reason, 0, 80),
|
||||
'count' => (int) $cnt,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
$summary = [
|
||||
'run_id' => $runId,
|
||||
'success' => $success,
|
||||
'failed' => $failed,
|
||||
'matched' => $this->matchedTotal,
|
||||
'processed' => $this->processed,
|
||||
'top_reasons' => $topReasons,
|
||||
'at' => now()->toDateTimeString(),
|
||||
];
|
||||
|
||||
foreach ($this->orderIds as $orderId) {
|
||||
$order = PlatformOrder::query()->find($orderId);
|
||||
if (! $order) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$meta = (array) ($order->meta ?? []);
|
||||
$ba = (array) (data_get($meta, 'batch_activation', []) ?? []);
|
||||
|
||||
// 仅当 run_id 与当前 job 一致时才回写 last_result,避免并发覆盖。
|
||||
if ((string) (data_get($ba, 'run_id') ?? '') !== $runId) {
|
||||
continue;
|
||||
}
|
||||
|
||||
data_set($ba, 'last_result', $summary);
|
||||
data_set($meta, 'batch_activation', $ba);
|
||||
|
||||
$order->meta = $meta;
|
||||
$order->save();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user