Skip to content
This repository has been archived by the owner on Dec 13, 2022. It is now read-only.

Commit

Permalink
feat(api): add update locker (#11341)
Browse files Browse the repository at this point in the history
Refs: MON-12296
  • Loading branch information
kduret authored Jul 7, 2022
1 parent b8589b3 commit 2efc0e8
Show file tree
Hide file tree
Showing 11 changed files with 433 additions and 18 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
"symfony/framework-bundle": "5.4.*",
"symfony/http-client": "5.4.*",
"symfony/http-kernel": "5.4.*",
"symfony/lock": "5.4.*",
"symfony/maker-bundle": "^1.11",
"symfony/monolog-bundle": "^3.7",
"symfony/options-resolver": "5.4.*",
Expand Down
83 changes: 81 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions config/packages/Centreon.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,10 @@ services:
class: Core\Platform\Infrastructure\Repository\FsReadUpdateRepository
public: true

Core\Platform\Application\Repository\UpdateLockerRepositoryInterface:
class: Core\Platform\Infrastructure\Repository\SymfonyUpdateLockerRepository
public: true

Core\Platform\Application\Repository\WriteUpdateRepositoryInterface:
class: Core\Platform\Infrastructure\Repository\DbWriteUpdateRepository
public: true
Expand Down
21 changes: 21 additions & 0 deletions lang/fr_FR.UTF-8/LC_MESSAGES/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -16916,3 +16916,24 @@ msgstr "Une erreur s'est produite lors de la récupération des catégories d'h

msgid "Warning, maximum size exceeded for input '%s' (max: %d), it will be truncated upon saving"
msgstr "Attention, taille maximale dépassée pour le champ '%s' (max: %d), il sera tronqué à l'enregistrement"

msgid "Update already in progress"
msgstr "Une mise à jour est déjà en cours"

msgid "An error occurred when retrieving current version"
msgstr "Une erreur s'est produite lors de la récupération de la version actuelle"

msgid "Cannot retrieve current version"
msgstr "La version actuelle n'a pas pu être trouvée"

msgid "An error occurred when getting available updates"
msgstr "Une erreur s'est produite lors de la récupération des mises à jour disponibles"

msgid "An error occurred when applying update %s (%s)"
msgstr "Une erreur s'est produite lors de l'application de la mise à jour %s (%s)"

msgid "Error while locking update process"
msgstr "Erreur lors du verrouillage du processus de mise à jour"

msgid "'Error while unlocking update process"
msgstr "Erreur lors du déverrouillage du processus de mise à jour"
44 changes: 44 additions & 0 deletions src/Core/Platform/Application/Repository/UpdateLockerException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

/*
* Copyright 2005 - 2022 Centreon (https://www.centreon.com/)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* For more information : contact@centreon.com
*
*/
declare(strict_types=1);

namespace Core\Platform\Application\Repository;

use Centreon\Domain\Repository\RepositoryException;

class UpdateLockerException extends RepositoryException
{
/**
* @return self
*/
public static function errorWhileLockingUpdate(\Throwable $e): self
{
return new self(_('Error while locking update process'), 0, $e);
}

/**
* @return self
*/
public static function errorWhileUnlockingUpdate(\Throwable $e): self
{
return new self(_('Error while unlocking update process'), 0, $e);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

/*
* Copyright 2005 - 2022 Centreon (https://www.centreon.com/)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* For more information : contact@centreon.com
*
*/
declare(strict_types=1);

namespace Core\Platform\Application\Repository;

interface UpdateLockerRepositoryInterface
{
/**
* Lock update process
*
* @return bool if the lock has been properly acquired
*
* @throws UpdateLockerException
*/
public function lock(): bool;

/**
* Unlock update process
*
* @throws UpdateLockerException
*/
public function unlock(): void;
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
namespace Core\Platform\Application\UseCase\UpdateVersions;

use Centreon\Domain\Log\LoggerTrait;
use Core\Platform\Application\Repository\UpdateLockerRepositoryInterface;
use Core\Platform\Application\Repository\ReadVersionRepositoryInterface;
use Core\Platform\Application\Repository\ReadUpdateRepositoryInterface;
use Core\Platform\Application\Repository\WriteUpdateRepositoryInterface;
Expand All @@ -34,11 +35,13 @@ class UpdateVersions
use LoggerTrait;

/**
* @param UpdateLockerRepositoryInterface $updateLocker
* @param ReadVersionRepositoryInterface $readVersionRepository
* @param ReadUpdateRepositoryInterface $readUpdateRepository
* @param WriteUpdateRepositoryInterface $writeUpdateRepository
*/
public function __construct(
private UpdateLockerRepositoryInterface $updateLocker,
private ReadVersionRepositoryInterface $readVersionRepository,
private ReadUpdateRepositoryInterface $readUpdateRepository,
private WriteUpdateRepositoryInterface $writeUpdateRepository,
Expand All @@ -54,11 +57,15 @@ public function __invoke(
$this->info('Updating versions');

try {
$this->lockUpdate();

$currentVersion = $this->getCurrentVersionOrFail();

$availableUpdates = $this->getAvailableUpdatesOrFail($currentVersion);

$this->runUpdates($availableUpdates);

$this->unlockUpdate();
} catch (\Throwable $e) {
$this->error(
$e->getMessage(),
Expand All @@ -73,6 +80,28 @@ public function __invoke(
$presenter->setResponseStatus(new NoContentResponse());
}

/**
* Lock update process
*/
private function lockUpdate(): void
{
$this->info('Locking centreon update process...');

if (!$this->updateLocker->lock()) {
throw UpdateVersionsException::updateAlreadyInProgress();
}
}

/**
* Unlock update process
*/
private function unlockUpdate(): void
{
$this->info('Unlocking centreon update process...');

$this->updateLocker->unlock();
}

/**
* Get current version or fail
*
Expand All @@ -87,11 +116,11 @@ private function getCurrentVersionOrFail(): string
try {
$currentVersion = $this->readVersionRepository->findCurrentVersion();
} catch (\Exception $e) {
throw new \Exception('An error occurred when retrieving current version', 0, $e);
throw UpdateVersionsException::errorWhenRetrievingCurrentVersion($e);
}

if ($currentVersion === null) {
throw new \Exception('Cannot retrieve current version');
throw UpdateVersionsException::cannotRetrieveCurrentVersion();
}

return $currentVersion;
Expand All @@ -115,7 +144,7 @@ private function getAvailableUpdatesOrFail(string $currentVersion): array

return $this->readUpdateRepository->findOrderedAvailableUpdates($currentVersion);
} catch (\Throwable $e) {
throw new \Exception('An error occurred when getting available updates', 0, $e);
throw UpdateVersionsException::errorWhenRetrievingAvailableUpdates($e);
}
}

Expand All @@ -133,15 +162,7 @@ private function runUpdates(array $versions): void
$this->info("Running update $version");
$this->writeUpdateRepository->runUpdate($version);
} catch (\Throwable $e) {
throw new \Exception(
sprintf(
'An error occurred when applying update %s (%s)',
$version,
$e->getMessage(),
),
0,
$e,
);
throw UpdateVersionsException::errorWhenApplyingUpdate($version, $e->getMessage(), $e);
}
}
}
Expand Down
Loading

0 comments on commit 2efc0e8

Please sign in to comment.