|
| 1 | +<?php |
| 2 | + |
| 3 | +// |
| 4 | +// Copyright (C) 2026 Nethesis S.r.l. |
| 5 | +// SPDX-License-Identifier: AGPL-3.0-or-later |
| 6 | +// |
| 7 | + |
| 8 | +namespace App\Jobs; |
| 9 | + |
| 10 | +use App\Models\Repository; |
| 11 | +use Carbon\Carbon; |
| 12 | +use Illuminate\Bus\Batch; |
| 13 | +use Illuminate\Contracts\Queue\ShouldQueue; |
| 14 | +use Illuminate\Foundation\Queue\Queueable; |
| 15 | +use Illuminate\Support\Facades\Bus; |
| 16 | +use Illuminate\Support\Facades\Log; |
| 17 | +use Illuminate\Support\Facades\Storage; |
| 18 | +use Throwable; |
| 19 | + |
| 20 | +class Release implements ShouldQueue |
| 21 | +{ |
| 22 | + use Queueable; |
| 23 | + |
| 24 | + /** |
| 25 | + * Sync the release. |
| 26 | + */ |
| 27 | + public function __construct(public readonly Repository $repository) {} |
| 28 | + |
| 29 | + /** |
| 30 | + * Execute the job. |
| 31 | + */ |
| 32 | + public function handle(): void |
| 33 | + { |
| 34 | + Log::debug("Releasing {$this->repository->name}."); |
| 35 | + |
| 36 | + // Sync the repository to create a fresh snapshot |
| 37 | + SyncRepository::dispatchSync($this->repository); |
| 38 | + |
| 39 | + // Get all snapshot directories and filter to only valid snapshot timestamps |
| 40 | + $allDirectories = Storage::directories($this->repository->snapshotDir()); |
| 41 | + $snapshots = collect($allDirectories)->filter(function ($dir) { |
| 42 | + $basename = basename($dir); |
| 43 | + try { |
| 44 | + Carbon::createFromFormat(DATE_ATOM, $basename); |
| 45 | + |
| 46 | + return true; |
| 47 | + } catch (\Exception $e) { |
| 48 | + return false; |
| 49 | + } |
| 50 | + })->sortByDesc(function ($dir) { |
| 51 | + return Carbon::createFromFormat(DATE_ATOM, basename($dir)); |
| 52 | + })->values(); |
| 53 | + |
| 54 | + if ($snapshots->isEmpty()) { |
| 55 | + Log::warning("No valid snapshots found for {$this->repository->name}."); |
| 56 | + |
| 57 | + return; |
| 58 | + } |
| 59 | + |
| 60 | + // Freeze to the most recent snapshot |
| 61 | + $lastSnapshot = $snapshots->first(); |
| 62 | + $this->repository->freeze = basename($lastSnapshot); |
| 63 | + $this->repository->save(); |
| 64 | + Log::info("Froze {$this->repository->name} to snapshot: {$this->repository->freeze}"); |
| 65 | + |
| 66 | + // Prepare deletion jobs for all other snapshots |
| 67 | + $snapshotsToDelete = $snapshots->slice(1); |
| 68 | + |
| 69 | + if ($snapshotsToDelete->isEmpty()) { |
| 70 | + Log::info("No snapshots to delete for {$this->repository->name}."); |
| 71 | + |
| 72 | + return; |
| 73 | + } |
| 74 | + |
| 75 | + $deletionJobs = $snapshotsToDelete->map(fn ($dir) => new DeleteSnapshot($dir))->all(); |
| 76 | + |
| 77 | + // Dispatch batch with unfreeze on success |
| 78 | + Bus::batch($deletionJobs) |
| 79 | + ->then(function (Batch $batch) { |
| 80 | + $repo = $this->repository->fresh(); |
| 81 | + $repo->freeze = null; |
| 82 | + $repo->save(); |
| 83 | + Log::info("Repository {$repo->name} unfrozen after successful cleanup."); |
| 84 | + }) |
| 85 | + ->catch(function (Batch $batch, Throwable $e) { |
| 86 | + Log::error("Deletion batch failed for {$this->repository->name}", [ |
| 87 | + 'batch_id' => $batch->id, |
| 88 | + 'error' => $e->getMessage(), |
| 89 | + ]); |
| 90 | + }) |
| 91 | + ->name("Release cleanup for {$this->repository->name}") |
| 92 | + ->dispatch(); |
| 93 | + |
| 94 | + Log::debug("Release completed for {$this->repository->name}."); |
| 95 | + } |
| 96 | +} |
0 commit comments