Skip to content

Commit

Permalink
Daemon remove: refactoring ExApps removal
Browse files Browse the repository at this point in the history
Signed-off-by: Alexander Piskun <bigcat88@icloud.com>
  • Loading branch information
bigcat88 committed Jan 24, 2024
1 parent adf89c9 commit 536437e
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 29 deletions.
12 changes: 11 additions & 1 deletion lib/Command/Daemon/UnregisterDaemon.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,18 @@

use OCA\AppAPI\Service\DaemonConfigService;

use OCA\AppAPI\Service\ExAppService;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class UnregisterDaemon extends Command {

public function __construct(private DaemonConfigService $daemonConfigService) {
public function __construct(
private readonly DaemonConfigService $daemonConfigService,
private readonly ExAppService $exAppService,
) {
parent::__construct();
}

Expand All @@ -33,6 +37,12 @@ protected function execute(InputInterface $input, OutputInterface $output): int
return 1;
}

$countExApps = count($this->exAppService->getExAppsByDaemonName($daemonConfigName));
if ($countExApps > 0) {
$output->writeln(sprintf('Error: %s daemon contains %d ExApps, please remove them first to proceed.', $daemonConfigName, $countExApps));
return 1;
}

if ($this->daemonConfigService->unregisterDaemonConfig($daemonConfig) === null) {
$output->writeln(sprintf('Failed to unregister daemon config %s.', $daemonConfigName));
return 1;
Expand Down
8 changes: 3 additions & 5 deletions lib/Controller/DaemonConfigController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
use OCA\AppAPI\Db\DaemonConfig;

use OCA\AppAPI\DeployActions\DockerActions;
use OCA\AppAPI\Service\AppAPIService;
use OCA\AppAPI\Service\DaemonConfigService;
use OCA\AppAPI\Service\ExAppService;
use OCP\AppFramework\ApiController;
use OCP\AppFramework\Http\Attribute\NoCSRFRequired;
use OCP\AppFramework\Http\JSONResponse;
Expand All @@ -27,7 +27,7 @@ public function __construct(
private readonly IConfig $config,
private readonly DaemonConfigService $daemonConfigService,
private readonly DockerActions $dockerActions,
private readonly ExAppService $exAppService,
private readonly AppAPIService $service,
) {
parent::__construct(Application::APP_ID, $request);
}
Expand Down Expand Up @@ -69,9 +69,7 @@ public function updateDaemonConfig(string $name, array $params): Response {
public function unregisterDaemonConfig(string $name): Response {
$daemonConfig = $this->daemonConfigService->getDaemonConfigByName($name);
$defaultDaemonConfig = $this->config->getAppValue(Application::APP_ID, 'default_daemon_config', '');
if ($daemonConfig->getAcceptsDeployId() === $this->dockerActions->getAcceptsDeployId()) {
$this->exAppService->removeExAppsByDaemonConfigName($daemonConfig, $this->dockerActions);
}
$this->service->removeExAppsByDaemonConfigName($daemonConfig);
if ($daemonConfig->getName() === $defaultDaemonConfig) {
$this->config->deleteAppValue(Application::APP_ID, 'default_daemon_config');
}
Expand Down
20 changes: 20 additions & 0 deletions lib/Service/AppAPIService.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace OCA\AppAPI\Service;

use OCA\AppAPI\AppInfo\Application;
use OCA\AppAPI\Db\DaemonConfig;
use OCA\AppAPI\Db\ExApp;
use OCA\AppAPI\DeployActions\DockerActions;
use OCA\AppAPI\DeployActions\ManualActions;
Expand Down Expand Up @@ -596,4 +597,23 @@ public function setAppInitProgress(string $appId, int $progress, string $error =
$this->enableExApp($exApp);
}
}

public function removeExAppsByDaemonConfigName(DaemonConfig $daemonConfig): void {
try {
$targetDaemonExApps = $this->exAppService->getExAppsByDaemonName($daemonConfig->getName());
if (count($targetDaemonExApps) === 0) {
return;
}
foreach ($targetDaemonExApps as $exApp) {
$this->disableExApp($exApp);
if ($daemonConfig->getAcceptsDeployId() === 'docker-install') {
$this->dockerActions->initGuzzleClient($daemonConfig);
$this->dockerActions->removePrevExAppContainer($this->dockerActions->buildDockerUrl($daemonConfig), $this->dockerActions->buildExAppContainerName($exApp->getAppid()));
$this->dockerActions->removeVolume($this->dockerActions->buildDockerUrl($daemonConfig), $this->dockerActions->buildExAppVolumeName($exApp->getAppid()));
}
$this->exAppService->unregisterExApp($exApp->getAppid());
}
} catch (Exception) {
}
}
}
34 changes: 11 additions & 23 deletions lib/Service/ExAppService.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@
namespace OCA\AppAPI\Service;

use OCA\AppAPI\AppInfo\Application;
use OCA\AppAPI\Db\DaemonConfig;
use OCA\AppAPI\Db\ExApp;
use OCA\AppAPI\Db\ExAppMapper;
use OCA\AppAPI\DeployActions\DockerActions;
use OCA\AppAPI\Fetcher\ExAppArchiveFetcher;
use OCA\AppAPI\Fetcher\ExAppFetcher;
use OCA\AppAPI\Service\ProvidersAI\SpeechToTextService;
Expand Down Expand Up @@ -53,7 +51,6 @@ public function __construct(
private readonly TextProcessingService $textProcessingService,
private readonly TranslationService $translationService,
private readonly TalkBotsService $talkBotsService,

) {
$this->cache = $cacheFactory->createDistributed(Application::APP_ID . '/service');
}
Expand Down Expand Up @@ -163,6 +160,17 @@ public function disableExAppInternal(ExApp $exApp): void {
$this->resetCaches();
}

public function getExAppsByDaemonName(string $daemonName): array {
try {
return array_filter($this->exAppMapper->findAll(), function (ExApp $exApp) use ($daemonName) {
return $exApp->getDaemonConfigName() === $daemonName;
});
} catch (Exception $e) {
$this->logger->error(sprintf('Error while getting ExApps list. Error: %s', $e->getMessage()), ['exception' => $e]);
return [];
}
}

public function getExAppsList(string $list = 'enabled'): array {
try {
$exApps = $this->exAppMapper->findAll();
Expand Down Expand Up @@ -304,24 +312,4 @@ private function resetCaches(): void {
$this->speechToTextService->resetCacheEnabled();
$this->translationService->resetCacheEnabled();
}

public function removeExAppsByDaemonConfigName(DaemonConfig $daemonConfig, DockerActions $dockerActions): void {
try {
$targetDaemonExApps = array_filter($this->exAppMapper->findAll(), function (ExApp $exApp) use ($daemonConfig) {
return $exApp->getDaemonConfigName() === $daemonConfig->getName();
});
if (count($targetDaemonExApps) === 0) {
return;
}

$dockerActions->initGuzzleClient($daemonConfig);
foreach ($targetDaemonExApps as $exApp) {
$this->disableExAppInternal($exApp);
$dockerActions->removePrevExAppContainer($dockerActions->buildDockerUrl($daemonConfig), $dockerActions->buildExAppContainerName($exApp->getAppid()));
$dockerActions->removeVolume($dockerActions->buildDockerUrl($daemonConfig), $dockerActions->buildExAppVolumeName($exApp->getAppid()));
$this->unregisterExApp($exApp->getAppid());
}
} catch (Exception) {
}
}
}

0 comments on commit 536437e

Please sign in to comment.