From 5a7d145c94e5478d1c75dc807ea745d9833d8e65 Mon Sep 17 00:00:00 2001 From: Vincent Vermeulen Date: Wed, 4 Oct 2017 22:40:29 +0200 Subject: [PATCH 1/2] Added command to schedule tasks if not ran for a specified X days --- .../Command/ScheduleBuildCommand.php | 98 +++++++++++++++++++ src/PHPCensor/Console/Application.php | 2 + 2 files changed, 100 insertions(+) create mode 100644 src/PHPCensor/Command/ScheduleBuildCommand.php diff --git a/src/PHPCensor/Command/ScheduleBuildCommand.php b/src/PHPCensor/Command/ScheduleBuildCommand.php new file mode 100644 index 00000000..5deaa04b --- /dev/null +++ b/src/PHPCensor/Command/ScheduleBuildCommand.php @@ -0,0 +1,98 @@ + + */ +class ScheduleBuildCommand extends Command +{ + /** + * @var ProjectStore + */ + protected $projectStore; + + /** + * @var BuildStore + */ + protected $buildStore; + + /** + * @var BuildService + */ + protected $buildService; + + /** + * @param ProjectStore $projectStore + * @param BuildStore $buildStore + * @param BuildService $buildService + */ + public function __construct(ProjectStore $projectStore, BuildStore $buildStore, BuildService $buildService) + { + parent::__construct(); + + $this->projectStore = $projectStore; + $this->buildService = $buildService; + $this->buildStore = $buildStore; + } + + /** + * {@inheritDoc} + */ + protected function configure() + { + $this + ->setName('php-censor:schedule-build') + ->setDescription('Schedules a build for active projects which have not been ran by X days') + ->addArgument('days', InputArgument::REQUIRED, 'Since specified days'); + } + + /** + * {@inheritDoc} + */ + public function execute(InputInterface $input, OutputInterface $output) + { + $sinceDays = $input->getArgument('days'); + $date = new \DateTime('now'); + $difference = new \DateInterval("P{$sinceDays}D"); + $date->sub($difference); + + $projects = $this->projectStore->getAll()['items']; + /** @var Project $project */ + foreach ($projects as $project) { + + $latestBuild = $this->buildStore->getByProjectId($project->getId(), 1); + if ($latestBuild['count'] > 0) { + /** @var Build $build */ + $build = $latestBuild['items'][0]; + if ((int)$build->getStatus() === 1 || (int)$build->getStatus() === 0) { + // If it's running or just created, we don't want to reschedule already. + continue; + } + if ($date < $build->getFinished()) { + // If finished date is newer then the specified since days, we don't want to reschedule + continue; + } + } + try { + $this->buildService->createBuild($project, null); + $output->writeln("Build Created for {$project->getTitle()}"); + } catch (\Exception $e) { + $output->writeln('Failed'); + $output->writeln(sprintf('%s', $e->getMessage())); + } + } + } +} diff --git a/src/PHPCensor/Console/Application.php b/src/PHPCensor/Console/Application.php index 79d6fa26..66310a02 100644 --- a/src/PHPCensor/Console/Application.php +++ b/src/PHPCensor/Console/Application.php @@ -13,6 +13,7 @@ use PHPCensor\Command\InstallCommand; use PHPCensor\Command\RebuildCommand; use PHPCensor\Command\RebuildQueueCommand; use PHPCensor\Command\RunCommand; +use PHPCensor\Command\ScheduleBuildCommand; use PHPCensor\Command\WorkerCommand; use PHPCensor\Logging\Handler; use PHPCensor\Service\BuildService; @@ -134,5 +135,6 @@ class Application extends BaseApplication $this->add(new CreateBuildCommand($projectStore, new BuildService($buildStore))); $this->add(new WorkerCommand($logger)); $this->add(new RebuildQueueCommand($logger)); + $this->add(new ScheduleBuildCommand($projectStore, $buildStore, new BuildService($buildStore))); } } From 9b198a128e495138e0bfed29dba80913b911acb9 Mon Sep 17 00:00:00 2001 From: Vincent Vermeulen Date: Thu, 5 Oct 2017 17:43:33 +0200 Subject: [PATCH 2/2] Use function getLatestBuild --- src/PHPCensor/Command/ScheduleBuildCommand.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/PHPCensor/Command/ScheduleBuildCommand.php b/src/PHPCensor/Command/ScheduleBuildCommand.php index 5deaa04b..82090ed2 100644 --- a/src/PHPCensor/Command/ScheduleBuildCommand.php +++ b/src/PHPCensor/Command/ScheduleBuildCommand.php @@ -73,7 +73,8 @@ class ScheduleBuildCommand extends Command /** @var Project $project */ foreach ($projects as $project) { - $latestBuild = $this->buildStore->getByProjectId($project->getId(), 1); + $latestBuild = $this->buildStore->getLatestBuilds($project->getId(), 1); + if ($latestBuild['count'] > 0) { /** @var Build $build */ $build = $latestBuild['items'][0];