Skip to content

Commit

Permalink
Merge pull request #14 from daniellienert/feature/do-not-run-tasks-in…
Browse files Browse the repository at this point in the history
…-parralel

FEATURE: Add configuration option to prevent parallel scheduler execution
  • Loading branch information
dfeyer authored Nov 11, 2016
2 parents be2fd68 + ff203bc commit f676afc
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
26 changes: 26 additions & 0 deletions Classes/Ttree/Scheduler/Command/TaskCommandController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -35,17 +37,39 @@ 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
*
* @param boolean $dryRun do not execute tasks
*/
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);
Expand All @@ -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();
}

/**
Expand Down
6 changes: 5 additions & 1 deletion Configuration/Settings.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,8 @@ TYPO3:
object:
excludeClasses:
'beberlei.assert' : ['.*']
'mtdowling.cronexpression' : ['.*']
'mtdowling.cronexpression' : ['.*']

Ttree:
Scheduler:
allowParallelExecution: true
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
---------------------

Expand Down

0 comments on commit f676afc

Please sign in to comment.