Start installing servers quicker (#454)

This commit is contained in:
Saeed Vaziry 2025-01-31 00:20:41 +01:00 committed by GitHub
parent 6966f06b1a
commit ea396786e4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 27 additions and 27 deletions

View File

@ -6,6 +6,7 @@
use App\Enums\ServerProvider; use App\Enums\ServerProvider;
use App\Enums\ServerStatus; use App\Enums\ServerStatus;
use App\Enums\ServerType; use App\Enums\ServerType;
use App\Exceptions\SSHConnectionError;
use App\Facades\Notifier; use App\Facades\Notifier;
use App\Models\Project; use App\Models\Project;
use App\Models\Server; use App\Models\Server;
@ -15,7 +16,6 @@
use App\ValidationRules\RestrictedIPAddressesRule; use App\ValidationRules\RestrictedIPAddressesRule;
use Exception; use Exception;
use Illuminate\Database\Query\Builder; use Illuminate\Database\Query\Builder;
use Illuminate\Support\Facades\Bus;
use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Log;
use Illuminate\Support\Str; use Illuminate\Support\Str;
use Illuminate\Validation\Rule; use Illuminate\Validation\Rule;
@ -81,18 +81,25 @@ public function create(User $creator, Project $project, array $input): Server
private function install(Server $server): void private function install(Server $server): void
{ {
$bus = Bus::chain([ dispatch(function () use ($server) {
function () use ($server) { $maxWait = 180;
if (! $server->provider()->isRunning()) { while ($maxWait > 0) {
sleep(2); sleep(10);
$maxWait -= 10;
try {
$server->ssh()->connect();
break;
} catch (SSHConnectionError) {
// ignore
}
} }
$server->type()->install(); $server->type()->install();
$server->update([ $server->update([
'status' => ServerStatus::READY, 'status' => ServerStatus::READY,
]); ]);
Notifier::send($server, new ServerInstallationSucceed($server)); Notifier::send($server, new ServerInstallationSucceed($server));
}, })
])->catch(function (Throwable $e) use ($server) { ->catch(function (Throwable $e) use ($server) {
$server->update([ $server->update([
'status' => ServerStatus::INSTALLATION_FAILED, 'status' => ServerStatus::INSTALLATION_FAILED,
]); ]);
@ -100,15 +107,8 @@ function () use ($server) {
Log::error('server-installation-error', [ Log::error('server-installation-error', [
'error' => (string) $e, 'error' => (string) $e,
]); ]);
}); })
->onConnection('ssh');
if ($server->provider != ServerProvider::CUSTOM) {
$server->progress_step = 'waiting-for-provider';
$server->save();
$bus->delay(now()->addMinutes(3));
}
$bus->onConnection('ssh')->dispatch();
} }
public static function rules(Project $project, array $input): array public static function rules(Project $project, array $input): array

View File

@ -60,7 +60,7 @@ public function setLog(?ServerLog $log): self
} }
/** /**
* @throws Throwable * @throws SSHConnectionError
*/ */
public function connect(bool $sftp = false): void public function connect(bool $sftp = false): void
{ {