TenantAtlas/app/Jobs/BulkBackupSetRestoreJob.php
ahmido 845d21db6d feat: harden operation lifecycle monitoring (#190)
## Summary
- harden operation-run lifecycle handling with explicit reconciliation policy, stale-run healing, failed-job bridging, and monitoring visibility
- refactor audit log event inspection into a Filament slide-over and remove the stale inline detail/header-action coupling
- align panel theme asset resolution and supporting Filament UI updates, including the rounded 2xl theme token regression fix

## Testing
- ran focused Pest coverage for the affected audit-log inspection flow and related visibility tests
- ran formatting with `vendor/bin/sail bin pint --dirty --format agent`
- manually verified the updated audit-log slide-over flow in the integrated browser

## Notes
- branch includes the Spec 160 artifacts under `specs/160-operation-lifecycle-guarantees/`
- the full test suite was not rerun as part of this final commit/PR step

Co-authored-by: Ahmed Darrazi <ahmed.darrazi@live.de>
Reviewed-on: #190
2026-03-23 21:53:19 +00:00

122 lines
3.2 KiB
PHP

<?php
namespace App\Jobs;
use App\Jobs\Concerns\BridgesFailedOperationRun;
use App\Jobs\Operations\BackupSetRestoreWorkerJob;
use App\Models\OperationRun;
use App\Services\OperationRunService;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use RuntimeException;
class BulkBackupSetRestoreJob implements ShouldQueue
{
use BridgesFailedOperationRun;
use Dispatchable;
use InteractsWithQueue;
use Queueable;
use SerializesModels;
public int $timeout = 300;
public bool $failOnTimeout = true;
public int $bulkRunId = 0;
public ?OperationRun $operationRun = null;
/**
* @param array<int, mixed> $backupSetIds
* @param array<string, mixed> $context
*/
public function __construct(
public int $tenantId,
public int $userId,
public array $backupSetIds,
?OperationRun $operationRun = null,
public array $context = [],
) {
$this->operationRun = $operationRun;
$this->bulkRunId = $operationRun?->getKey() ? (int) $operationRun->getKey() : 0;
}
public function handle(OperationRunService $runs): void
{
$this->operationRun = $this->resolveOperationRun();
$this->operationRun->refresh();
if ($this->operationRun->status === 'completed') {
return;
}
$runs->updateRun($this->operationRun, 'running');
$ids = $this->normalizeIds($this->backupSetIds);
$runs->incrementSummaryCounts($this->operationRun, ['total' => count($ids)]);
$chunkSize = (int) config('tenantpilot.bulk_operations.chunk_size', 10);
$chunkSize = max(1, $chunkSize);
foreach (array_chunk($ids, $chunkSize) as $chunk) {
foreach ($chunk as $backupSetId) {
dispatch(new BackupSetRestoreWorkerJob(
tenantId: $this->tenantId,
userId: $this->userId,
backupSetId: $backupSetId,
operationRun: $this->operationRun,
context: $this->context,
));
}
}
}
private function resolveOperationRun(): OperationRun
{
if ($this->operationRun instanceof OperationRun) {
return $this->operationRun;
}
if ($this->bulkRunId > 0) {
$run = OperationRun::query()->find($this->bulkRunId);
if ($run instanceof OperationRun) {
return $run;
}
}
throw new RuntimeException('OperationRun is required for BulkBackupSetRestoreJob.');
}
/**
* @param array<int, mixed> $ids
* @return array<int, int>
*/
private function normalizeIds(array $ids): array
{
$normalized = [];
foreach ($ids as $id) {
if (is_int($id)) {
$normalized[] = $id;
continue;
}
if (is_numeric($id)) {
$normalized[] = (int) $id;
}
}
$normalized = array_values(array_unique($normalized));
sort($normalized);
return $normalized;
}
}