From ff203bc4eac57325c094e020a0738a4e1bbcf7c4 Mon Sep 17 00:00:00 2001 From: Daniel Lienert Date: Fri, 7 Oct 2016 10:30:09 +0200 Subject: [PATCH] FEATURE: Add configuration option to prevent parallel scheduler execution --- .../Command/TaskCommandController.php | 26 +++++++++++++++++++ Configuration/Settings.yaml | 6 ++++- README.md | 8 ++++++ 3 files changed, 39 insertions(+), 1 deletion(-) diff --git a/Classes/Ttree/Scheduler/Command/TaskCommandController.php b/Classes/Ttree/Scheduler/Command/TaskCommandController.php index 3b75ded..c67c97b 100644 --- a/Classes/Ttree/Scheduler/Command/TaskCommandController.php +++ b/Classes/Ttree/Scheduler/Command/TaskCommandController.php @@ -16,6 +16,8 @@ use TYPO3\Flow\Annotations as Flow; use TYPO3\Flow\Cli\CommandController; use TYPO3\Flow\Object\ObjectManagerInterface; +use TYPO3\Flow\Utility\Lock\Lock; +use TYPO3\Flow\Utility\Lock\LockNotAcquiredException; /** * Task Command Controller @@ -35,6 +37,17 @@ class TaskCommandController extends CommandController */ protected $objectManager; + /** + * @Flow\InjectConfiguration(package="Ttree.Scheduler", path="allowParallelExecution") + * @var boolean + */ + protected $allowParallelExecution = true; + + /** + * @var Lock + */ + protected $parallelExecutionLock; + /** * Run all pending task * @@ -42,10 +55,21 @@ class TaskCommandController extends CommandController */ public function runCommand($dryRun = false) { + + if($this->allowParallelExecution !== true) { + try { + $this->parallelExecutionLock = new Lock('Ttree.Scheduler.ParallelExecutionLock'); + } catch (LockNotAcquiredException $exception) { + $this->tellStatus('The scheduler is already running and parallel execution is disabled.'); + $this->sendAndExit(0); + } + } + foreach ($this->taskService->getDueTasks() as $taskDescriptor) { /** @var Task $task */ $task = $taskDescriptor['object']; $arguments = [$task->getImplementation(), $taskDescriptor['identifier']]; + try { if (!$dryRun) { $task->execute($this->objectManager); @@ -58,6 +82,8 @@ public function runCommand($dryRun = false) $this->tellStatus('[Error] Task "%s" (%s) throw an exception, check your log', $arguments); } } + + $this->parallelExecutionLock->release(); } /** diff --git a/Configuration/Settings.yaml b/Configuration/Settings.yaml index bce00ab..2088d10 100644 --- a/Configuration/Settings.yaml +++ b/Configuration/Settings.yaml @@ -3,4 +3,8 @@ TYPO3: object: excludeClasses: 'beberlei.assert' : ['.*'] - 'mtdowling.cronexpression' : ['.*'] \ No newline at end of file + 'mtdowling.cronexpression' : ['.*'] + +Ttree: + Scheduler: + allowParallelExecution: true \ No newline at end of file diff --git a/README.md b/README.md index 2e80fe5..71c856c 100644 --- a/README.md +++ b/README.md @@ -55,6 +55,14 @@ multiple times, if your arguments are different between each task. You can pass flow task:register --expression "* */3 * * *" --task "Ttree\Aggregator\Task\AggregatorTask" --arguments '{"node": "af97b530-0c70-7b87-3cf4-f9a611f88c18"}' +Available Configuration Options +------------------------------- + +| Option | Default | Description | +|------------------------|---------|-----------------------------------------------------------------------------------------------------------------------------------------------| +| allowParallelExecution | true | If the scheduler command is executed while the scheduler is already running tasks, the second scheduler waits until the first one is finished | + + Available CLI helpers ---------------------