From 25ff60b34be473c8940ffcedb8b33f3e0f6de7c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1vid=20Danyi?= Date: Thu, 13 Sep 2018 13:10:00 +0200 Subject: [PATCH] * add locking to the cli job so it only starts once --- src/App/Command/UpdatePageCachesCommand.php | 41 ++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/src/App/Command/UpdatePageCachesCommand.php b/src/App/Command/UpdatePageCachesCommand.php index 0a791d1..c247b68 100755 --- a/src/App/Command/UpdatePageCachesCommand.php +++ b/src/App/Command/UpdatePageCachesCommand.php @@ -12,19 +12,25 @@ use Symfony\Component\Console\Output\OutputInterface; class UpdatePageCachesCommand extends Command { + const LOCK_FILE = 'data/update-caches-cron.lock'; + /** @var JiraCollectorService */ private $jiraCollectorService; /** @var TeamService */ private $teamService; - public function __construct(JiraCollectorService $jiraCollectorService, TeamService $teamService) + public function __construct(JiraCollectorService $jiraCollectorService, + TeamService $teamService) { $this->jiraCollectorService = $jiraCollectorService; $this->teamService = $teamService; parent::__construct(); } + /** + * Configure the command + */ protected function configure() { $this->setName('cache:update') @@ -39,6 +45,11 @@ class UpdatePageCachesCommand extends Command */ protected function execute(InputInterface $input, OutputInterface $output) { + if ($this->isLocked()) { + $output->writeln("Lock file exists, not starting."); + return; + } + $this->createLock(); $teams = $this->teamService->listTeams(); foreach ($teams as $team) { set_time_limit(30); @@ -47,5 +58,33 @@ class UpdatePageCachesCommand extends Command } $this->jiraCollectorService->getTeamWatchedIssues($team->getId(), true); } + $this->releaseLock(); + } + + /** + * Create the lock file + * @return bool + */ + private function createLock(): bool + { + return touch(self::LOCK_FILE); + } + + /** + * Remove the lock file + * @return bool + */ + private function releaseLock(): bool + { + return unlink(self::LOCK_FILE); + } + + /** + * Check if lock file exists + * @return bool + */ + private function isLocked(): bool + { + return file_exists(self::LOCK_FILE); } }