2025-04-02 17:38:31 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Jobs;
|
|
|
|
|
|
|
|
|
|
use App\Actions\Proxy\StartProxy;
|
|
|
|
|
use App\Actions\Proxy\StopProxy;
|
2025-12-03 09:29:39 +00:00
|
|
|
use App\Enums\ProxyTypes;
|
|
|
|
|
use App\Events\ProxyStatusChangedUI;
|
2025-04-02 17:38:31 +00:00
|
|
|
use App\Models\Server;
|
|
|
|
|
use Illuminate\Bus\Queueable;
|
|
|
|
|
use Illuminate\Contracts\Queue\ShouldBeEncrypted;
|
|
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
|
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
|
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
|
|
|
use Illuminate\Queue\Middleware\WithoutOverlapping;
|
|
|
|
|
use Illuminate\Queue\SerializesModels;
|
2025-12-03 09:29:39 +00:00
|
|
|
use Illuminate\Support\Facades\Log;
|
2025-04-02 17:38:31 +00:00
|
|
|
|
|
|
|
|
class RestartProxyJob implements ShouldBeEncrypted, ShouldQueue
|
|
|
|
|
{
|
|
|
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
|
|
|
|
|
|
public $tries = 1;
|
|
|
|
|
|
|
|
|
|
public $timeout = 60;
|
|
|
|
|
|
2025-12-03 09:29:39 +00:00
|
|
|
public ?int $activity_id = null;
|
|
|
|
|
|
2025-04-02 17:38:31 +00:00
|
|
|
public function middleware(): array
|
|
|
|
|
{
|
2025-07-01 08:50:27 +00:00
|
|
|
return [(new WithoutOverlapping('restart-proxy-'.$this->server->uuid))->expireAfter(60)->dontRelease()];
|
2025-04-02 17:38:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function __construct(public Server $server) {}
|
|
|
|
|
|
|
|
|
|
public function handle()
|
|
|
|
|
{
|
|
|
|
|
try {
|
2025-12-03 09:29:39 +00:00
|
|
|
$teamId = $this->server->team_id;
|
|
|
|
|
|
|
|
|
|
// Stop proxy
|
2025-11-13 12:38:57 +00:00
|
|
|
StopProxy::run($this->server, restarting: true);
|
2025-04-02 17:38:31 +00:00
|
|
|
|
2025-12-03 09:29:39 +00:00
|
|
|
// Clear force_stop flag
|
2025-04-02 17:38:31 +00:00
|
|
|
$this->server->proxy->force_stop = false;
|
|
|
|
|
$this->server->save();
|
2025-06-06 12:47:54 +00:00
|
|
|
|
2025-12-03 09:29:39 +00:00
|
|
|
// Start proxy asynchronously to get activity
|
|
|
|
|
$activity = StartProxy::run($this->server, force: true, restarting: true);
|
|
|
|
|
|
|
|
|
|
// Store activity ID and dispatch event with it
|
|
|
|
|
if ($activity && is_object($activity)) {
|
|
|
|
|
$this->activity_id = $activity->id;
|
|
|
|
|
ProxyStatusChangedUI::dispatch($teamId, $this->activity_id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check Traefik version after restart (same as original behavior)
|
|
|
|
|
if ($this->server->proxyType() === ProxyTypes::TRAEFIK->value) {
|
|
|
|
|
$traefikVersions = get_traefik_versions();
|
|
|
|
|
if ($traefikVersions !== null) {
|
|
|
|
|
CheckTraefikVersionForServerJob::dispatch($this->server, $traefikVersions);
|
|
|
|
|
} else {
|
|
|
|
|
Log::warning('Traefik version check skipped: versions.json data unavailable', [
|
|
|
|
|
'server_id' => $this->server->id,
|
|
|
|
|
'server_name' => $this->server->name,
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-04-02 17:38:31 +00:00
|
|
|
|
|
|
|
|
} catch (\Throwable $e) {
|
2025-12-03 09:29:39 +00:00
|
|
|
// Set error status
|
|
|
|
|
$this->server->proxy->status = 'error';
|
|
|
|
|
$this->server->save();
|
|
|
|
|
|
|
|
|
|
// Notify UI of error
|
|
|
|
|
ProxyStatusChangedUI::dispatch($this->server->team_id);
|
|
|
|
|
|
2025-04-02 17:38:31 +00:00
|
|
|
return handleError($e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|