Summary Kurz: Implementiert Feature 054 — canonical OperationRun-flow, Monitoring UI, dispatch-safety, notifications, dedupe, plus small UX safety clarifications (RBAC group search delegated; Restore group mapping DB-only). What Changed Core service: OperationRun lifecycle, dedupe and dispatch helpers — OperationRunService.php. Model + migration: OperationRun model and migration — OperationRun.php, 2026_01_16_180642_create_operation_runs_table.php. Notifications: queued + terminal DB notifications (initiator-only) — OperationRunQueued.php, OperationRunCompleted.php. Monitoring UI: Filament list/detail + Livewire pieces (DB-only render) — OperationRunResource.php and related pages/views. Start surfaces / Jobs: instrumented start surfaces, job middleware, and job updates to use canonical runs — multiple app/Jobs/* and app/Filament/* updates (see tests for full coverage). RBAC + Restore UX clarifications: RBAC group search is delegated-Graph-based and disabled without delegated token; Restore group mapping remains DB-only (directory cache) and helper text always visible — TenantResource.php, RestoreRunResource.php. Specs / Constitution: updated spec & quickstart and added one-line constitution guideline about Graph usage: spec.md quickstart.md constitution.md Tests & Verification Unit / Feature tests added/updated for run lifecycle, notifications, idempotency, and UI guards: see tests/Feature/* (notably OperationRunServiceTest, MonitoringOperationsTest, OperationRunNotificationTest, and various Filament feature tests). Full test run locally: ./vendor/bin/sail artisan test → 587 passed, 5 skipped. Migrations Adds create_operation_runs_table migration; run php artisan migrate in staging after review. Notes / Rationale Monitoring pages are explicitly DB-only at render time (no Graph calls). Start surfaces enqueue work only and return a “View run” link. Delegated Graph access is used only for explicit user actions (RBAC group search); restore mapping intentionally uses cached DB data only to avoid render-time Graph calls. Dispatch wrapper marks runs failed immediately if background dispatch throws synchronously to avoid misleading “queued” states. Upgrade / Deploy Considerations Run migrations: ./vendor/bin/sail artisan migrate. Background workers should be running to process queued jobs (recommended to monitor queue health during rollout). No secret or token persistence changes. PR checklist Tests updated/added for changed behavior Specs updated: 054-unify-runs-suitewide docs + quickstart Constitution note added (.specify) Pint formatting applied Co-authored-by: Ahmed Darrazi <ahmeddarrazi@adsmac.local> Reviewed-on: #63
214 lines
7.1 KiB
PHP
214 lines
7.1 KiB
PHP
<?php
|
|
|
|
namespace App\Jobs;
|
|
|
|
use App\Jobs\Middleware\TrackOperationRun;
|
|
use App\Models\BulkOperationRun;
|
|
use App\Models\InventorySyncRun;
|
|
use App\Models\OperationRun;
|
|
use App\Models\Tenant;
|
|
use App\Services\BulkOperationService;
|
|
use App\Services\Drift\DriftFindingGenerator;
|
|
use App\Services\OperationRunService;
|
|
use App\Support\OperationRunLinks;
|
|
use Filament\Actions\Action;
|
|
use Filament\Notifications\Notification;
|
|
use Illuminate\Bus\Queueable;
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
use Illuminate\Queue\SerializesModels;
|
|
use Illuminate\Support\Facades\Log;
|
|
use RuntimeException;
|
|
use Throwable;
|
|
|
|
class GenerateDriftFindingsJob implements ShouldQueue
|
|
{
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
public ?OperationRun $operationRun = null;
|
|
|
|
public function __construct(
|
|
public int $tenantId,
|
|
public int $userId,
|
|
public int $baselineRunId,
|
|
public int $currentRunId,
|
|
public string $scopeKey,
|
|
public int $bulkOperationRunId,
|
|
?OperationRun $operationRun = null
|
|
) {
|
|
$this->operationRun = $operationRun;
|
|
}
|
|
|
|
public function middleware(): array
|
|
{
|
|
return [new TrackOperationRun];
|
|
}
|
|
|
|
/**
|
|
* Execute the job.
|
|
*/
|
|
public function handle(DriftFindingGenerator $generator, BulkOperationService $bulkOperationService): void
|
|
{
|
|
Log::info('GenerateDriftFindingsJob: started', [
|
|
'tenant_id' => $this->tenantId,
|
|
'baseline_run_id' => $this->baselineRunId,
|
|
'current_run_id' => $this->currentRunId,
|
|
'scope_key' => $this->scopeKey,
|
|
'bulk_operation_run_id' => $this->bulkOperationRunId,
|
|
]);
|
|
|
|
$tenant = Tenant::query()->find($this->tenantId);
|
|
if (! $tenant instanceof Tenant) {
|
|
throw new RuntimeException('Tenant not found.');
|
|
}
|
|
|
|
$baseline = InventorySyncRun::query()->find($this->baselineRunId);
|
|
if (! $baseline instanceof InventorySyncRun) {
|
|
throw new RuntimeException('Baseline run not found.');
|
|
}
|
|
|
|
$current = InventorySyncRun::query()->find($this->currentRunId);
|
|
if (! $current instanceof InventorySyncRun) {
|
|
throw new RuntimeException('Current run not found.');
|
|
}
|
|
|
|
$run = BulkOperationRun::query()
|
|
->where('tenant_id', $tenant->getKey())
|
|
->find($this->bulkOperationRunId);
|
|
|
|
if (! $run instanceof BulkOperationRun) {
|
|
throw new RuntimeException('Bulk operation run not found.');
|
|
}
|
|
|
|
$bulkOperationService->start($run);
|
|
|
|
try {
|
|
$created = $generator->generate(
|
|
tenant: $tenant,
|
|
baseline: $baseline,
|
|
current: $current,
|
|
scopeKey: $this->scopeKey,
|
|
);
|
|
|
|
Log::info('GenerateDriftFindingsJob: completed', [
|
|
'tenant_id' => $this->tenantId,
|
|
'baseline_run_id' => $this->baselineRunId,
|
|
'current_run_id' => $this->currentRunId,
|
|
'scope_key' => $this->scopeKey,
|
|
'bulk_operation_run_id' => $this->bulkOperationRunId,
|
|
'created_findings_count' => $created,
|
|
]);
|
|
|
|
$bulkOperationService->recordSuccess($run);
|
|
$bulkOperationService->complete($run);
|
|
|
|
if ($this->operationRun) {
|
|
/** @var OperationRunService $opService */
|
|
$opService = app(OperationRunService::class);
|
|
$opService->updateRun(
|
|
$this->operationRun,
|
|
'completed',
|
|
'succeeded',
|
|
['findings_created' => $created]
|
|
);
|
|
}
|
|
|
|
$this->notifyStatus($run->refresh());
|
|
} catch (Throwable $e) {
|
|
Log::error('GenerateDriftFindingsJob: failed', [
|
|
'tenant_id' => $this->tenantId,
|
|
'baseline_run_id' => $this->baselineRunId,
|
|
'current_run_id' => $this->currentRunId,
|
|
'scope_key' => $this->scopeKey,
|
|
'bulk_operation_run_id' => $this->bulkOperationRunId,
|
|
'error' => $e->getMessage(),
|
|
]);
|
|
|
|
$bulkOperationService->recordFailure(
|
|
run: $run,
|
|
itemId: $this->scopeKey,
|
|
reason: $e->getMessage(),
|
|
reasonCode: 'unknown',
|
|
);
|
|
|
|
$bulkOperationService->fail($run, $e->getMessage());
|
|
|
|
// TrackOperationRun middleware might catch this, but explicit fail ensures structure
|
|
if ($this->operationRun) {
|
|
/** @var OperationRunService $opService */
|
|
$opService = app(OperationRunService::class);
|
|
$opService->failRun($this->operationRun, $e);
|
|
}
|
|
|
|
$this->notifyStatus($run->refresh());
|
|
|
|
throw $e;
|
|
}
|
|
}
|
|
|
|
private function notifyStatus(BulkOperationRun $run): void
|
|
{
|
|
try {
|
|
if (! $run->relationLoaded('user')) {
|
|
$run->loadMissing('user');
|
|
}
|
|
|
|
if (! $run->user) {
|
|
return;
|
|
}
|
|
|
|
$tenant = Tenant::query()->find((int) $run->tenant_id);
|
|
|
|
if (! $tenant instanceof Tenant) {
|
|
return;
|
|
}
|
|
|
|
$status = $run->statusBucket();
|
|
|
|
$title = match ($status) {
|
|
'queued' => 'Drift generation queued',
|
|
'running' => 'Drift generation started',
|
|
'succeeded' => 'Drift generation completed',
|
|
'partially succeeded' => 'Drift generation completed (partial)',
|
|
default => 'Drift generation failed',
|
|
};
|
|
|
|
$body = sprintf(
|
|
'Total: %d, processed: %d, succeeded: %d, failed: %d, skipped: %d.',
|
|
(int) $run->total_items,
|
|
(int) $run->processed_items,
|
|
(int) $run->succeeded,
|
|
(int) $run->failed,
|
|
(int) $run->skipped,
|
|
);
|
|
|
|
$notification = Notification::make()
|
|
->title($title)
|
|
->body($body)
|
|
->actions([
|
|
Action::make('view_run')
|
|
->label('View run')
|
|
->url($this->operationRun ? OperationRunLinks::view($this->operationRun, $tenant) : OperationRunLinks::index($tenant)),
|
|
]);
|
|
|
|
match ($status) {
|
|
'succeeded' => $notification->success(),
|
|
'partially succeeded' => $notification->warning(),
|
|
'queued', 'running' => $notification->info(),
|
|
default => $notification->danger(),
|
|
};
|
|
|
|
$notification
|
|
->sendToDatabase($run->user)
|
|
->send();
|
|
} catch (Throwable $e) {
|
|
Log::warning('GenerateDriftFindingsJob: status notification failed', [
|
|
'tenant_id' => (int) $run->tenant_id,
|
|
'bulk_operation_run_id' => (int) $run->getKey(),
|
|
'error' => $e->getMessage(),
|
|
]);
|
|
}
|
|
}
|
|
}
|