From 143022ecdd8199584a44d677cc1c1b2700ded45e Mon Sep 17 00:00:00 2001 From: Simon Gadient Date: Fri, 16 Aug 2019 13:01:23 +0200 Subject: [PATCH] TASK: Mark task as run before execution If there is a task taking longer than the interval the scheduler task runner is executed, the task is executed again even though it is not scheduled to execute again. Setting the last execution time before executing the task fixes this problem. --- Classes/Command/TaskCommandController.php | 28 +++++++++++++++++++---- Classes/Domain/Model/Task.php | 1 - 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/Classes/Command/TaskCommandController.php b/Classes/Command/TaskCommandController.php index f97274a..fd3fde9 100644 --- a/Classes/Command/TaskCommandController.php +++ b/Classes/Command/TaskCommandController.php @@ -10,6 +10,7 @@ * */ use Assert\Assertion; +use Neos\Flow\Persistence\PersistenceManagerInterface; use Ttree\Scheduler\Domain\Model\Task; use Ttree\Scheduler\Service\TaskService; use Ttree\Scheduler\Task\TaskInterface; @@ -37,6 +38,12 @@ class TaskCommandController extends CommandController */ protected $objectManager; + /** + * @var PersistenceManagerInterface + * @Flow\Inject + */ + protected $persistenceManager; + /** * @Flow\InjectConfiguration(package="Ttree.Scheduler", path="allowParallelExecution") * @var boolean @@ -70,6 +77,7 @@ public function runCommand($dryRun = false) $task = $taskDescriptor['object']; $arguments = [$task->getImplementation(), $taskDescriptor['identifier']]; + $this->markTaskAsRun($task, $taskDescriptor['type']); try { if (!$dryRun) { $task->execute($this->objectManager); @@ -78,10 +86,8 @@ public function runCommand($dryRun = false) $this->tellStatus('[Skipped, dry run] Skipped "%s" (%s)', $arguments); } } catch (\Exception $exception) { - $task->markAsRun(); $this->tellStatus('[Error] Task "%s" (%s) throw an exception, check your log', $arguments); } - $this->taskService->update($task, $taskDescriptor['type']); } if ($this->parallelExecutionLock instanceof Lock) { @@ -132,15 +138,13 @@ public function runSingleCommand($taskIdentifier) $task = $taskDescriptor['object']; $arguments = [$task->getImplementation(), $taskDescriptor['identifier']]; + $this->markTaskAsRun($task, $taskDescriptor['type']); try { $taskDescriptor['object']->execute($this->objectManager); $this->tellStatus('[Success] Run "%s" (%s)', $arguments); } catch (\Exception $exception) { - $task->markAsRun(); $this->tellStatus('[Error] Task "%s" (%s) throw an exception, check your log', $arguments); } - - $this->taskService->update($task, $taskDescriptor['type']); } /** @@ -210,4 +214,18 @@ protected function markFailedTaskAsRun(Task $task, $taskType) $task->initializeNextExecution(); $this->taskService->update($task, $taskType); } + + /** + * @param Task $task + * @param bool $taskType + */ + protected function markTaskAsRun(Task $task, $taskType) + { + $task->markAsRun(); + $this->taskService->update($task, $taskType); + if ($taskType === TaskInterface::TYPE_PERSISTED) { + $this->persistenceManager->whitelistObject($task); + $this->persistenceManager->persistAll(true); + } + } } diff --git a/Classes/Domain/Model/Task.php b/Classes/Domain/Model/Task.php index 34a1260..69cb3f5 100644 --- a/Classes/Domain/Model/Task.php +++ b/Classes/Domain/Model/Task.php @@ -222,7 +222,6 @@ public function execute(ObjectManagerInterface $objectManager) /** @var TaskInterface $task */ $task = $objectManager->get($this->implementation, $this); $task->execute($this->arguments); - $this->markAsRun(); } /**