Implements Spec 110 Ops‑UX Enforcement and applies the repo‑wide “enterprise” standard for operation start + dedup surfaces. Key points - Start surfaces: only ephemeral queued toast (no DB notifications for started/queued/running). - Dedup paths: canonical “already queued” toast. - Progress refresh: dispatch run-enqueued browser event so the global widget updates immediately. - Completion: exactly-once terminal DB notification on completion (per Ops‑UX contract). Tests & formatting - Full suite: 1738 passed, 8 skipped (8477 assertions). - Pint: `vendor/bin/sail bin pint --dirty --format agent` (pass). Notable change - Removed legacy `RunStatusChangedNotification` (replaced by the terminal-only completion notification policy). Co-authored-by: Ahmed Darrazi <ahmed.darrazi@live.de> Reviewed-on: #134
250 lines
9.7 KiB
PHP
250 lines
9.7 KiB
PHP
<?php
|
|
|
|
namespace App\Jobs;
|
|
|
|
use App\Jobs\Middleware\TrackOperationRun;
|
|
use App\Models\BackupItem;
|
|
use App\Models\BackupSet;
|
|
use App\Models\OperationRun;
|
|
use App\Models\Policy;
|
|
use App\Models\Tenant;
|
|
use App\Models\User;
|
|
use App\Services\OperationRunService;
|
|
use App\Support\OperationRunOutcome;
|
|
use App\Support\OperationRunStatus;
|
|
use Illuminate\Bus\Queueable;
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
use Illuminate\Queue\SerializesModels;
|
|
use Throwable;
|
|
|
|
class BulkPolicyExportJob implements ShouldQueue
|
|
{
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
public ?OperationRun $operationRun = null;
|
|
|
|
public function __construct(
|
|
public int $tenantId,
|
|
public int $userId,
|
|
/** @var array<int, int|string> */
|
|
public array $policyIds,
|
|
public string $backupName,
|
|
public ?string $backupDescription = null,
|
|
?OperationRun $operationRun = null,
|
|
) {
|
|
$this->operationRun = $operationRun;
|
|
}
|
|
|
|
public function middleware(): array
|
|
{
|
|
return [new TrackOperationRun];
|
|
}
|
|
|
|
public function handle(OperationRunService $operationRunService): void
|
|
{
|
|
$tenant = Tenant::query()->find($this->tenantId);
|
|
if (! $tenant instanceof Tenant) {
|
|
throw new \RuntimeException('Tenant not found.');
|
|
}
|
|
|
|
$user = User::query()->find($this->userId);
|
|
if (! $user instanceof User) {
|
|
throw new \RuntimeException('User not found.');
|
|
}
|
|
|
|
$ids = collect($this->policyIds)
|
|
->map(static fn ($id): int => (int) $id)
|
|
->unique()
|
|
->sort()
|
|
->values()
|
|
->all();
|
|
|
|
try {
|
|
// Create Backup Set
|
|
$backupSet = BackupSet::create([
|
|
'tenant_id' => $tenant->getKey(),
|
|
'name' => $this->backupName,
|
|
// 'description' => $this->backupDescription, // Not in schema
|
|
'status' => 'completed',
|
|
'created_by' => $user->name,
|
|
'item_count' => count($ids),
|
|
'completed_at' => now(),
|
|
]);
|
|
|
|
$itemCount = 0;
|
|
$succeeded = 0;
|
|
$failed = 0;
|
|
$failures = [];
|
|
|
|
$totalItems = count($ids);
|
|
$failureThreshold = (int) floor($totalItems / 2);
|
|
|
|
foreach ($ids as $policyId) {
|
|
$itemCount++;
|
|
|
|
try {
|
|
$policy = Policy::query()
|
|
->where('tenant_id', $tenant->getKey())
|
|
->find($policyId);
|
|
|
|
if (! $policy) {
|
|
$failed++;
|
|
$failures[] = ['code' => 'policy.not_found', 'message' => "Policy {$policyId} not found."];
|
|
|
|
if ($failed > $failureThreshold) {
|
|
$backupSet->update(['status' => 'failed']);
|
|
|
|
if ($this->operationRun) {
|
|
$operationRunService->updateRun(
|
|
$this->operationRun,
|
|
status: OperationRunStatus::Completed->value,
|
|
outcome: OperationRunOutcome::Failed->value,
|
|
summaryCounts: [
|
|
'total' => $totalItems,
|
|
'processed' => $itemCount,
|
|
'succeeded' => $succeeded,
|
|
'failed' => $failed,
|
|
'created' => $succeeded,
|
|
],
|
|
failures: array_merge($failures, [
|
|
['code' => 'export.circuit_breaker', 'message' => 'Circuit breaker: more than 50% of items failed.'],
|
|
]),
|
|
);
|
|
}
|
|
|
|
return;
|
|
}
|
|
|
|
continue;
|
|
}
|
|
|
|
// Get latest version for snapshot
|
|
$latestVersion = $policy->versions()->orderByDesc('captured_at')->first();
|
|
|
|
if (! $latestVersion) {
|
|
$failed++;
|
|
$failures[] = ['code' => 'policy.no_versions', 'message' => "No versions available for policy {$policyId}."];
|
|
|
|
if ($failed > $failureThreshold) {
|
|
$backupSet->update(['status' => 'failed']);
|
|
|
|
if ($this->operationRun) {
|
|
$operationRunService->updateRun(
|
|
$this->operationRun,
|
|
status: OperationRunStatus::Completed->value,
|
|
outcome: OperationRunOutcome::Failed->value,
|
|
summaryCounts: [
|
|
'total' => $totalItems,
|
|
'processed' => $itemCount,
|
|
'succeeded' => $succeeded,
|
|
'failed' => $failed,
|
|
'created' => $succeeded,
|
|
],
|
|
failures: array_merge($failures, [
|
|
['code' => 'export.circuit_breaker', 'message' => 'Circuit breaker: more than 50% of items failed.'],
|
|
]),
|
|
);
|
|
}
|
|
|
|
return;
|
|
}
|
|
|
|
continue;
|
|
}
|
|
|
|
// Create Backup Item
|
|
BackupItem::create([
|
|
'tenant_id' => $tenant->getKey(),
|
|
'backup_set_id' => $backupSet->id,
|
|
'policy_id' => $policy->id,
|
|
'policy_identifier' => $policy->external_id, // Added
|
|
'policy_type' => $policy->policy_type,
|
|
'platform' => $policy->platform ?? null, // Added
|
|
// 'display_name' => $policy->display_name, // Not in schema, maybe in metadata?
|
|
'payload' => $latestVersion->snapshot, // Mapped to payload
|
|
'metadata' => [
|
|
'display_name' => $policy->display_name, // Stored in metadata
|
|
'version_captured_at' => $latestVersion->captured_at->toIso8601String(),
|
|
],
|
|
]);
|
|
|
|
$succeeded++;
|
|
|
|
} catch (Throwable $e) {
|
|
$failed++;
|
|
$failures[] = ['code' => 'policy.export.failed', 'message' => $e->getMessage()];
|
|
|
|
if ($failed > $failureThreshold) {
|
|
$backupSet->update(['status' => 'failed']);
|
|
|
|
if ($this->operationRun) {
|
|
$operationRunService->updateRun(
|
|
$this->operationRun,
|
|
status: OperationRunStatus::Completed->value,
|
|
outcome: OperationRunOutcome::Failed->value,
|
|
summaryCounts: [
|
|
'total' => $totalItems,
|
|
'processed' => $itemCount,
|
|
'succeeded' => $succeeded,
|
|
'failed' => $failed,
|
|
'created' => $succeeded,
|
|
],
|
|
failures: array_merge($failures, [
|
|
['code' => 'export.circuit_breaker', 'message' => 'Circuit breaker: more than 50% of items failed.'],
|
|
]),
|
|
);
|
|
}
|
|
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
// Update BackupSet item count (if denormalized) or just leave it
|
|
// Assuming BackupSet might need an item count or status update
|
|
|
|
$outcome = OperationRunOutcome::Succeeded->value;
|
|
|
|
if ($failed > 0 && $failed < $totalItems) {
|
|
$outcome = OperationRunOutcome::PartiallySucceeded->value;
|
|
}
|
|
|
|
if ($failed >= $totalItems && $totalItems > 0) {
|
|
$outcome = OperationRunOutcome::Failed->value;
|
|
}
|
|
|
|
if ($this->operationRun) {
|
|
$operationRunService->updateRun(
|
|
$this->operationRun,
|
|
status: OperationRunStatus::Completed->value,
|
|
outcome: $outcome,
|
|
summaryCounts: [
|
|
'total' => $totalItems,
|
|
'processed' => $totalItems,
|
|
'succeeded' => $succeeded,
|
|
'failed' => $failed,
|
|
'created' => $succeeded,
|
|
],
|
|
failures: $failures,
|
|
);
|
|
}
|
|
|
|
} catch (Throwable $e) {
|
|
if ($this->operationRun) {
|
|
$operationRunService->updateRun(
|
|
$this->operationRun,
|
|
status: OperationRunStatus::Completed->value,
|
|
outcome: OperationRunOutcome::Failed->value,
|
|
failures: [
|
|
['code' => 'exception.unhandled', 'message' => $e->getMessage()],
|
|
],
|
|
);
|
|
}
|
|
|
|
throw $e;
|
|
}
|
|
}
|
|
}
|