CluPilotCloud/app/Provisioning/Jobs/PurgeHost.php

53 lines
1.5 KiB
PHP

<?php
namespace App\Provisioning\Jobs;
use App\Models\Host;
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\Cache;
/**
* Finalizes host removal off the web request: deletes each run under its runner
* lock (waiting for any in-flight step to finish rather than racing it), then
* removes the WireGuard peer and the host record. Runs on the provisioning queue.
*/
class PurgeHost implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public $tries = 1;
public $timeout = 2200;
public function __construct(public string $uuid)
{
$this->onConnection('provisioning');
$this->onQueue('provisioning');
}
public function handle(): void
{
$host = Host::query()->where('uuid', $this->uuid)->first();
if ($host === null) {
return;
}
foreach ($host->runs()->get() as $run) {
// Block until the runner lock is free (a long step may hold it for up
// to the step timeout), then delete — cascades events + resources.
Cache::lock('run:'.$run->uuid, 2200)->block(2100, fn () => $run->delete());
}
if (filled($host->wg_pubkey)) {
RemoveWireguardPeer::dispatch($host->wg_pubkey);
}
$host->delete();
}
}