* add locking to the cli job so it only starts once

This commit is contained in:
Dávid Danyi 2018-09-13 13:10:00 +02:00
parent c7a2e68a82
commit 25ff60b34b

View File

@ -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);
}
}