Skip to content

Commit

Permalink
TASK: Mark task as run before execution
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
simstern committed Aug 16, 2019
1 parent 3c3e3d8 commit dcfa6e6
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
29 changes: 24 additions & 5 deletions Classes/Command/TaskCommandController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand All @@ -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) {
Expand All @@ -104,6 +110,7 @@ public function listCommand()
$this->output->outputTable($tasks, [
'Type',
'Status',
'Running',
'Identifier',
'Interval',
'Implementation',
Expand Down Expand Up @@ -132,15 +139,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']);
}

/**
Expand Down Expand Up @@ -210,4 +215,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);
}
}
}
1 change: 0 additions & 1 deletion Classes/Domain/Model/Task.php
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,6 @@ public function execute(ObjectManagerInterface $objectManager)
/** @var TaskInterface $task */
$task = $objectManager->get($this->implementation, $this);
$task->execute($this->arguments);
$this->markAsRun();
}

/**
Expand Down

0 comments on commit dcfa6e6

Please sign in to comment.