What Implements tenant-scoped backup scheduling end-to-end: schedules CRUD, minute-based dispatch, queued execution, run history, manual “Run now/Retry”, retention (keep last N), and auditability. Key changes Filament UI: Backup Schedules resource with tenant scoping + SEC-002 role gating. Scheduler + queue: tenantpilot:schedules:dispatch command wired in scheduler (runs every minute), creates idempotent BackupScheduleRun records and dispatches jobs. Execution: RunBackupScheduleJob syncs policies, creates immutable backup sets, updates run status, writes audit logs, applies retry/backoff mapping, and triggers retention. Run history: Relation manager + “View” modal rendering run details. UX polish: row actions grouped; bulk actions grouped (run now / retry / delete). Bulk dispatch writes DB notifications (shows in notifications panel). Validation: policy type hard-validation on save; unknown policy types handled safely at runtime (skipped/partial). Tests: comprehensive Pest coverage for CRUD/scoping/validation, idempotency, job outcomes, error mapping, retention, view modal, run-now/retry notifications, bulk delete (incl. operator forbidden). Files / Areas Filament: BackupScheduleResource.php and app/Filament/Resources/BackupScheduleResource/* Scheduling/Jobs: app/Console/Commands/TenantpilotDispatchBackupSchedules.php, app/Jobs/RunBackupScheduleJob.php, app/Jobs/ApplyBackupScheduleRetentionJob.php, console.php Models/Migrations: app/Models/BackupSchedule.php, app/Models/BackupScheduleRun.php, database/migrations/backup_schedules, backup_schedule_runs Notifications: BackupScheduleRunDispatchedNotification.php Specs: specs/032-backup-scheduling-mvp/* (tasks/checklist/quickstart updates) How to test (Sail) Run tests: ./vendor/bin/sail artisan test tests/Feature/BackupScheduling Run formatter: ./vendor/bin/sail php ./vendor/bin/pint --dirty Apply migrations: ./vendor/bin/sail artisan migrate Manual dispatch: ./vendor/bin/sail artisan tenantpilot:schedules:dispatch Notes Uses DB notifications for queued UI actions to ensure they appear in the notifications panel even under queue fakes in tests. Checklist gate for 032 is PASS; tasks updated accordingly. Co-authored-by: Ahmed Darrazi <ahmeddarrazi@adsmac.local> Reviewed-on: #34
101 lines
3.1 KiB
PHP
101 lines
3.1 KiB
PHP
<?php
|
|
|
|
namespace App\Jobs;
|
|
|
|
use App\Models\BackupSchedule;
|
|
use App\Models\BackupScheduleRun;
|
|
use App\Models\BackupSet;
|
|
use App\Services\Intune\AuditLogger;
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
use Illuminate\Foundation\Queue\Queueable;
|
|
use Illuminate\Support\Collection;
|
|
|
|
class ApplyBackupScheduleRetentionJob implements ShouldQueue
|
|
{
|
|
use Queueable;
|
|
|
|
public function __construct(public int $backupScheduleId) {}
|
|
|
|
public function handle(AuditLogger $auditLogger): void
|
|
{
|
|
$schedule = BackupSchedule::query()
|
|
->with('tenant')
|
|
->find($this->backupScheduleId);
|
|
|
|
if (! $schedule || ! $schedule->tenant) {
|
|
return;
|
|
}
|
|
|
|
$keepLast = (int) ($schedule->retention_keep_last ?? 30);
|
|
|
|
if ($keepLast < 1) {
|
|
$keepLast = 1;
|
|
}
|
|
|
|
/** @var Collection<int, int> $keepBackupSetIds */
|
|
$keepBackupSetIds = BackupScheduleRun::query()
|
|
->where('backup_schedule_id', $schedule->id)
|
|
->whereNotNull('backup_set_id')
|
|
->orderByDesc('scheduled_for')
|
|
->limit($keepLast)
|
|
->pluck('backup_set_id')
|
|
->filter()
|
|
->values();
|
|
|
|
/** @var Collection<int, int> $deleteBackupSetIds */
|
|
$deleteBackupSetIds = BackupScheduleRun::query()
|
|
->where('backup_schedule_id', $schedule->id)
|
|
->whereNotNull('backup_set_id')
|
|
->when($keepBackupSetIds->isNotEmpty(), fn ($query) => $query->whereNotIn('backup_set_id', $keepBackupSetIds->all()))
|
|
->pluck('backup_set_id')
|
|
->filter()
|
|
->unique()
|
|
->values();
|
|
|
|
if ($deleteBackupSetIds->isEmpty()) {
|
|
$auditLogger->log(
|
|
tenant: $schedule->tenant,
|
|
action: 'backup_schedule.retention_applied',
|
|
resourceType: 'backup_schedule',
|
|
resourceId: (string) $schedule->id,
|
|
status: 'success',
|
|
context: [
|
|
'metadata' => [
|
|
'keep_last' => $keepLast,
|
|
'deleted_backup_sets' => 0,
|
|
],
|
|
],
|
|
);
|
|
|
|
return;
|
|
}
|
|
|
|
$deletedCount = 0;
|
|
|
|
BackupSet::query()
|
|
->where('tenant_id', $schedule->tenant_id)
|
|
->whereIn('id', $deleteBackupSetIds->all())
|
|
->whereNull('deleted_at')
|
|
->chunkById(200, function (Collection $sets) use (&$deletedCount): void {
|
|
foreach ($sets as $set) {
|
|
$set->delete();
|
|
$deletedCount++;
|
|
}
|
|
});
|
|
|
|
$auditLogger->log(
|
|
tenant: $schedule->tenant,
|
|
action: 'backup_schedule.retention_applied',
|
|
resourceType: 'backup_schedule',
|
|
resourceId: (string) $schedule->id,
|
|
status: 'success',
|
|
context: [
|
|
'metadata' => [
|
|
'keep_last' => $keepLast,
|
|
'deleted_backup_sets' => $deletedCount,
|
|
],
|
|
],
|
|
);
|
|
}
|
|
}
|