-
-
Notifications
You must be signed in to change notification settings - Fork 177
Expand file tree
/
Copy pathReconcileServerCreationJob.php
More file actions
151 lines (124 loc) · 5.36 KB
/
Copy pathReconcileServerCreationJob.php
File metadata and controls
151 lines (124 loc) · 5.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<?php
namespace App\Jobs;
use App\Classes\PterodactylClient;
use App\Models\Server;
use App\Services\CreditService;
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\Log;
class ReconcileServerCreationJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public int $tries = 5;
public string $serverId;
public int $chargedPrice;
public function __construct(string $serverId, int $chargedPrice)
{
$this->serverId = $serverId;
$this->chargedPrice = $chargedPrice;
$this->queue = 'default';
}
public function handle(PterodactylClient $pterodactylClient, CreditService $creditService): void
{
$server = Server::find($this->serverId);
if (!$server) {
return;
}
if ($server->status === Server::STATUS_FAILED) {
return;
}
if ($server->status === Server::STATUS_ACTIVE && $server->pterodactyl_id) {
return;
}
if ($server->pterodactyl_id) {
$server->update(['status' => Server::STATUS_ACTIVE]);
dispatch(new PostServerCreationJob($server->id));
return;
}
try {
$response = $pterodactylClient->getServerByExternalId($server->id);
} catch (\Exception $e) {
Log::warning('ReconcileServerCreationJob: pterodactyl API request failed', [
'server_id' => $server->id,
'error' => $e->getMessage(),
]);
throw $e;
}
if ($response->successful()) {
$attributes = $response->json()['attributes'] ?? null;
if (!$attributes || !isset($attributes['id']) || !isset($attributes['identifier'])) {
throw new \Exception('ReconcileServerCreationJob: invalid pterodactyl server payload');
}
$server->update([
'pterodactyl_id' => $attributes['id'],
'identifier' => $attributes['identifier'],
'status' => Server::STATUS_ACTIVE,
]);
dispatch(new PostServerCreationJob($server->id));
return;
}
if ($response->status() === 404) {
$server->delete();
$creditService->refund($server->user, $this->chargedPrice);
Log::info('ReconcileServerCreationJob: deleted server and refunded credits on confirmed 404', [
'server_id' => $this->serverId,
'amount' => $this->chargedPrice,
]);
return;
}
throw new \Exception('ReconcileServerCreationJob: pterodactyl API returned non-404 failure status ' . $response->status());
}
public function failed(\Throwable $exception): void
{
$server = Server::find($this->serverId);
if (!$server || $server->status === Server::STATUS_ACTIVE) {
return;
}
$pterodactylClient = app(PterodactylClient::class);
$creditService = app(CreditService::class);
try {
$response = $pterodactylClient->getServerByExternalId($server->id);
if ($response->successful()) {
$attributes = $response->json()['attributes'] ?? null;
if ($attributes && isset($attributes['id']) && isset($attributes['identifier'])) {
$server->update([
'pterodactyl_id' => $attributes['id'],
'identifier' => $attributes['identifier'],
'status' => Server::STATUS_ACTIVE,
]);
dispatch(new PostServerCreationJob($server->id));
Log::critical('ReconcileServerCreationJob failed after retries but remote server found; marked active', [
'server_id' => $this->serverId,
'error' => $exception->getMessage(),
]);
return;
}
}
if ($response->status() === 404) {
$server->delete();
$creditService->refund($server->user, $this->chargedPrice);
Log::critical('ReconcileServerCreationJob failed after retries with remote 404; deleted server and refunded credits', [
'server_id' => $this->serverId,
'amount' => $this->chargedPrice,
'error' => $exception->getMessage(),
]);
return;
}
$server->update(['status' => Server::STATUS_PENDING_RECONCILIATION]);
Log::critical('ReconcileServerCreationJob failed after maximum retries; remote state unknown, keeping pending_reconciliation', [
'server_id' => $this->serverId,
'error' => $exception->getMessage(),
]);
} catch (\Exception $e) {
// If remote check also fails, preserve pending state and log.
$server->update(['status' => Server::STATUS_PENDING_RECONCILIATION]);
Log::critical('ReconcileServerCreationJob failed and remote check failed; keeping pending_reconciliation', [
'server_id' => $this->serverId,
'exception' => $e->getMessage(),
]);
}
}
}