From 4f9e1a3e5e105984ddfed4b997084d6d90ffdeab Mon Sep 17 00:00:00 2001 From: peaklabs-dev <122374094+peaklabs-dev@users.noreply.github.com> Date: Sun, 8 Sep 2024 19:37:00 +0200 Subject: [PATCH] Feat: Cleanup stale multiplexing connections --- app/Console/Kernel.php | 6 ++- .../CleanupStaleMultiplexedConnections.php | 43 +++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 app/Jobs/CleanupStaleMultiplexedConnections.php diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index 96740ab24..b960a4a8b 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -4,6 +4,7 @@ namespace App\Console; use App\Jobs\CheckForUpdatesJob; use App\Jobs\CleanupInstanceStuffsJob; +use App\Jobs\CleanupStaleMultiplexedConnections; use App\Jobs\DatabaseBackupJob; use App\Jobs\DockerCleanupJob; use App\Jobs\PullHelperImageJob; @@ -29,7 +30,8 @@ class Kernel extends ConsoleKernel $this->all_servers = Server::all(); $settings = InstanceSettings::get(); - $schedule->command('telescope:prune')->daily(); + $schedule->job(new CleanupStaleMultiplexedConnections)->hourly(); + if (isDev()) { // Instance Jobs $schedule->command('horizon:snapshot')->everyMinute(); @@ -39,6 +41,8 @@ class Kernel extends ConsoleKernel $this->check_resources($schedule); $this->check_scheduled_tasks($schedule); $schedule->command('uploads:clear')->everyTwoMinutes(); + + $schedule->command('telescope:prune')->daily(); } else { // Instance Jobs $schedule->command('horizon:snapshot')->everyFiveMinutes(); diff --git a/app/Jobs/CleanupStaleMultiplexedConnections.php b/app/Jobs/CleanupStaleMultiplexedConnections.php new file mode 100644 index 000000000..733adb8d4 --- /dev/null +++ b/app/Jobs/CleanupStaleMultiplexedConnections.php @@ -0,0 +1,43 @@ +cleanupStaleConnection($server); + } + }); + } + + private function cleanupStaleConnection(Server $server) + { + $cacheKey = "mux_connection_{$server->id}"; + $cachedConnection = cache()->get($cacheKey); + + if ($cachedConnection) { + $muxSocket = $cachedConnection['muxSocket']; + $checkCommand = "ssh -O check -o ControlPath=$muxSocket {$server->user}@{$server->ip} 2>/dev/null"; + $checkProcess = Process::run($checkCommand); + + if ($checkProcess->exitCode() !== 0) { + $closeCommand = "ssh -O exit -o ControlPath=$muxSocket {$server->user}@{$server->ip} 2>/dev/null"; + Process::run($closeCommand); + cache()->forget($cacheKey); + } + } + } +}