initial commit
This commit is contained in:
75
app/Jobs/ProvisionCustomerJob.php
Normal file
75
app/Jobs/ProvisionCustomerJob.php
Normal file
@@ -0,0 +1,75 @@
|
||||
<?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',
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user