Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/suspend server offline node #871

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -792,7 +792,11 @@ public function form(Form $form): Form
->color('warning')
->hidden(fn (Server $server) => $server->isSuspended())
->action(function (SuspensionService $suspensionService, Server $server) {
$suspensionService->toggle($server, 'suspend');
try {
$suspensionService->handle($server, SuspensionService::ACTION_SUSPEND);
} catch (\Exception $exception) {
Notification::make()->warning()->title('Server Suspension')->body($exception->getMessage())->send();
}
Notification::make()->success()->title('Server Suspended!')->send();

$this->refreshFormData(['status', 'docker']);
Expand All @@ -802,7 +806,11 @@ public function form(Form $form): Form
->color('success')
->hidden(fn (Server $server) => !$server->isSuspended())
->action(function (SuspensionService $suspensionService, Server $server) {
$suspensionService->toggle($server, 'unsuspend');
try {
$suspensionService->handle($server, SuspensionService::ACTION_UNSUSPEND);
} catch (\Exception $exception) {
Notification::make()->warning()->title('Server Suspension')->body($exception->getMessage())->send();
}
Notification::make()->success()->title('Server Unsuspended!')->send();

$this->refreshFormData(['status', 'docker']);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function table(Table $table): Table
->color('warning')
->action(function (SuspensionService $suspensionService) use ($user) {
foreach ($user->servers()->whereNot('status', ServerState::Suspended)->get() as $server) {
$suspensionService->toggle($server);
$suspensionService->handle($server);
}
}),
Actions\Action::make('toggleUnsuspend')
Expand All @@ -43,7 +43,7 @@ public function table(Table $table): Table
->color('primary')
->action(function (SuspensionService $suspensionService) use ($user) {
foreach ($user->servers()->where('status', ServerState::Suspended)->get() as $server) {
$suspensionService->toggle($server, SuspensionService::ACTION_UNSUSPEND);
$suspensionService->handle($server, SuspensionService::ACTION_UNSUSPEND);
}
}),
])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function __construct(
*/
public function suspend(ServerWriteRequest $request, Server $server): Response
{
$this->suspensionService->toggle($server);
$this->suspensionService->handle($server);
RMartinOscar marked this conversation as resolved.
Show resolved Hide resolved

return $this->returnNoContent();
}
Expand All @@ -44,7 +44,7 @@ public function suspend(ServerWriteRequest $request, Server $server): Response
*/
public function unsuspend(ServerWriteRequest $request, Server $server): Response
{
$this->suspensionService->toggle($server, SuspensionService::ACTION_UNSUSPEND);
$this->suspensionService->handle($server, SuspensionService::ACTION_UNSUSPEND);

return $this->returnNoContent();
}
Expand Down
9 changes: 3 additions & 6 deletions app/Services/Servers/SuspensionService.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Webmozart\Assert\Assert;
use App\Models\Server;
use App\Repositories\Daemon\DaemonServerRepository;
use Doctrine\DBAL\Exception\ConnectionException;
use Symfony\Component\HttpKernel\Exception\ConflictHttpException;

class SuspensionService
Expand All @@ -27,7 +28,7 @@ public function __construct(
*
* @throws \Throwable
*/
public function toggle(Server $server, string $action = self::ACTION_SUSPEND): void
public function handle(Server $server, string $action = self::ACTION_SUSPEND): void
RMartinOscar marked this conversation as resolved.
Show resolved Hide resolved
{
Assert::oneOf($action, [self::ACTION_SUSPEND, self::ACTION_UNSUSPEND]);

Expand Down Expand Up @@ -55,11 +56,7 @@ public function toggle(Server $server, string $action = self::ACTION_SUSPEND): v
try {
// Tell daemon to re-sync the server state.
$this->daemonServerRepository->setServer($server)->sync();
} catch (\Exception $exception) {
// Rollback the server's suspension status if daemon fails to sync the server.
$server->update([
'status' => $isSuspending ? null : ServerState::Suspended,
]);
} catch (ConnectionException $exception) {
lancepioch marked this conversation as resolved.
Show resolved Hide resolved
throw $exception;
}
}
Expand Down
10 changes: 5 additions & 5 deletions tests/Integration/Services/Servers/SuspensionServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ public function testServerIsSuspendedAndUnsuspended(): void

$this->repository->expects('setServer->sync')->twice()->andReturnSelf();

$this->getService()->toggle($server);
$this->getService()->handle($server);

$this->assertTrue($server->refresh()->isSuspended());

$this->getService()->toggle($server, SuspensionService::ACTION_UNSUSPEND);
$this->getService()->handle($server, SuspensionService::ACTION_UNSUSPEND);

$this->assertFalse($server->refresh()->isSuspended());
}
Expand All @@ -42,13 +42,13 @@ public function testNoActionIsTakenIfSuspensionStatusIsUnchanged(): void
{
$server = $this->createServerModel();

$this->getService()->toggle($server, SuspensionService::ACTION_UNSUSPEND);
$this->getService()->handle($server, SuspensionService::ACTION_UNSUSPEND);

$server->refresh();
$this->assertFalse($server->isSuspended());

$server->update(['status' => ServerState::Suspended]);
$this->getService()->toggle($server);
$this->getService()->handle($server);

$server->refresh();
$this->assertTrue($server->isSuspended());
Expand All @@ -61,7 +61,7 @@ public function testExceptionIsThrownIfInvalidActionsArePassed(): void
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Expected one of: "suspend", "unsuspend". Got: "foo"');

$this->getService()->toggle($server, 'foo');
$this->getService()->handle($server, 'foo');
}

private function getService(): SuspensionService
Expand Down
Loading