44 lines
1.3 KiB
PHP
44 lines
1.3 KiB
PHP
<?php
|
|
|
|
namespace App\Jobs;
|
|
|
|
use App\Models\Server;
|
|
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\Process;
|
|
|
|
class CleanupStaleMultiplexedConnections implements ShouldQueue
|
|
{
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
public function handle()
|
|
{
|
|
Server::chunk(100, function ($servers) {
|
|
foreach ($servers as $server) {
|
|
$this->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);
|
|
}
|
|
}
|
|
}
|
|
}
|