76 lines
2.3 KiB
PHP
76 lines
2.3 KiB
PHP
<?php
|
|
|
|
namespace App\Jobs;
|
|
|
|
use App\Models\Customer;
|
|
use App\Services\Hosting\DTO\CustomerProvisionData;
|
|
use App\Services\Hosting\Notifications\HostingNotificationService;
|
|
use App\Services\Hosting\Provisioning\ProvisioningService;
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
use Illuminate\Foundation\Queue\Queueable;
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
use Illuminate\Queue\SerializesModels;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Throwable;
|
|
|
|
class ProvisionCustomerJob implements ShouldQueue
|
|
{
|
|
use InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
public int $tries = 3;
|
|
|
|
public int $backoff = 30;
|
|
|
|
public function __construct(
|
|
public readonly int $customerId,
|
|
) {}
|
|
|
|
public function handle(ProvisioningService $provisioning, HostingNotificationService $notifications): void
|
|
{
|
|
$customer = Customer::query()->with('owner')->findOrFail($this->customerId);
|
|
|
|
if ($customer->status === 'active') {
|
|
Log::info('ProvisionCustomerJob: customer already active', ['id' => $customer->id]);
|
|
|
|
return;
|
|
}
|
|
|
|
$customer->update([
|
|
'status' => 'pending',
|
|
'provisioning_step' => 'queued',
|
|
'error_message' => null,
|
|
]);
|
|
|
|
$data = CustomerProvisionData::fromCustomer($customer);
|
|
|
|
$provisioning->provision($customer, $data);
|
|
|
|
if ($customer->owner) {
|
|
$notifications->provisioningCompleted($customer->fresh(), $customer->owner);
|
|
}
|
|
}
|
|
|
|
public function failed(?Throwable $exception): void
|
|
{
|
|
Log::error('ProvisionCustomerJob failed permanently', [
|
|
'customer_id' => $this->customerId,
|
|
'error' => $exception?->getMessage(),
|
|
]);
|
|
|
|
$customer = Customer::query()->with('owner')->find($this->customerId);
|
|
if ($customer) {
|
|
$customer->update([
|
|
'status' => 'failed',
|
|
'error_message' => $exception?->getMessage() ?? 'Unknown provisioning failure',
|
|
]);
|
|
if ($customer->owner) {
|
|
app(HostingNotificationService::class)->provisioningFailed(
|
|
$customer,
|
|
$customer->owner,
|
|
$customer->error_message ?? 'Unbekannter Fehler',
|
|
);
|
|
}
|
|
}
|
|
}
|
|
}
|