Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TASK: Mark task as run before execution #30

Merged
merged 1 commit into from
Oct 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 23 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 Down Expand Up @@ -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']);
}

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

/**
Expand Down