Merge pull request #6693 from nextcloud/enh/noid/allow-force-stop-nextcloud

aio-interface: allow to force-stop Nextcloud container via API
This commit is contained in:
Simon L. 2025-07-28 14:41:11 +02:00 committed by GitHub
commit a9303b7918
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 22 additions and 9 deletions

View file

@ -83,17 +83,18 @@ readonly class DockerController {
} }
public function StartBackupContainerBackup(Request $request, Response $response, array $args) : Response { public function StartBackupContainerBackup(Request $request, Response $response, array $args) : Response {
$this->startBackup(); $forceStopNextcloud = true;
$this->startBackup($forceStopNextcloud);
return $response->withStatus(201)->withHeader('Location', '/'); return $response->withStatus(201)->withHeader('Location', '/');
} }
public function startBackup() : void { public function startBackup(bool $forceStopNextcloud = false) : void {
$config = $this->configurationManager->GetConfig(); $config = $this->configurationManager->GetConfig();
$config['backup-mode'] = 'backup'; $config['backup-mode'] = 'backup';
$this->configurationManager->WriteConfig($config); $this->configurationManager->WriteConfig($config);
$id = self::TOP_CONTAINER; $id = self::TOP_CONTAINER;
$this->PerformRecursiveContainerStop($id); $this->PerformRecursiveContainerStop($id, $forceStopNextcloud);
$id = 'nextcloud-aio-borgbackup'; $id = 'nextcloud-aio-borgbackup';
$this->PerformRecursiveContainerStart($id); $this->PerformRecursiveContainerStart($id);
@ -125,7 +126,8 @@ readonly class DockerController {
$this->configurationManager->WriteConfig($config); $this->configurationManager->WriteConfig($config);
$id = self::TOP_CONTAINER; $id = self::TOP_CONTAINER;
$this->PerformRecursiveContainerStop($id); $forceStopNextcloud = true;
$this->PerformRecursiveContainerStop($id, $forceStopNextcloud);
$id = 'nextcloud-aio-borgbackup'; $id = 'nextcloud-aio-borgbackup';
$this->PerformRecursiveContainerStart($id); $this->PerformRecursiveContainerStart($id);
@ -224,7 +226,7 @@ readonly class DockerController {
$this->PerformRecursiveContainerStart($id); $this->PerformRecursiveContainerStart($id);
} }
private function PerformRecursiveContainerStop(string $id) : void private function PerformRecursiveContainerStop(string $id, bool $forceStopNextcloud = false) : void
{ {
$container = $this->containerDefinitionFetcher->GetContainerById($id); $container = $this->containerDefinitionFetcher->GetContainerById($id);
@ -236,7 +238,12 @@ readonly class DockerController {
} }
// Stop itself first and then all the dependencies // Stop itself first and then all the dependencies
if ($id !== 'nextcloud-aio-nextcloud') {
$this->dockerActionManager->StopContainer($container); $this->dockerActionManager->StopContainer($container);
} else {
// We want to stop the Nextcloud container after 10s and not wait for the configured stop_grace_period
$this->dockerActionManager->StopContainer($container, $forceStopNextcloud);
}
foreach($container->GetDependsOn() as $dependency) { foreach($container->GetDependsOn() as $dependency) {
$this->PerformRecursiveContainerStop($dependency); $this->PerformRecursiveContainerStop($dependency);
} }
@ -245,7 +252,8 @@ readonly class DockerController {
public function StopContainer(Request $request, Response $response, array $args) : Response public function StopContainer(Request $request, Response $response, array $args) : Response
{ {
$id = self::TOP_CONTAINER; $id = self::TOP_CONTAINER;
$this->PerformRecursiveContainerStop($id); $forceStopNextcloud = true;
$this->PerformRecursiveContainerStop($id, $forceStopNextcloud);
return $response->withStatus(201)->withHeader('Location', '/'); return $response->withStatus(201)->withHeader('Location', '/');
} }

View file

@ -898,8 +898,13 @@ readonly class DockerActionManager {
} }
} }
public function StopContainer(Container $container): void { public function StopContainer(Container $container, bool $forceStopContainer = false): void {
$url = $this->BuildApiUrl(sprintf('containers/%s/stop?t=%s', urlencode($container->GetIdentifier()), $container->GetMaxShutdownTime())); if ($forceStopContainer) {
$maxShutDownTime = 10;
} else {
$maxShutDownTime = $container->GetMaxShutdownTime();
}
$url = $this->BuildApiUrl(sprintf('containers/%s/stop?t=%s', urlencode($container->GetIdentifier()), $maxShutDownTime));
try { try {
$this->guzzleClient->post($url); $this->guzzleClient->post($url);
} catch (RequestException $e) { } catch (RequestException $e) {