Skip to content

Commit

Permalink
Merge pull request #12 from daniellienert/feature/run-single-dynamic-…
Browse files Browse the repository at this point in the history
…task

FEATURE: Run a single dynamic task by identifier
  • Loading branch information
dfeyer authored Aug 29, 2016
2 parents 34e1c62 + bb22d4d commit be2fd68
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 17 deletions.
16 changes: 11 additions & 5 deletions Classes/Ttree/Scheduler/Command/TaskCommandController.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function runCommand($dryRun = false)
foreach ($this->taskService->getDueTasks() as $taskDescriptor) {
/** @var Task $task */
$task = $taskDescriptor['object'];
$arguments = [$task->getImplementation(), $taskDescriptor['identifier'] ?: $taskDescriptor['type']];
$arguments = [$task->getImplementation(), $taskDescriptor['identifier']];
try {
if (!$dryRun) {
$task->execute($this->objectManager);
Expand Down Expand Up @@ -90,11 +90,17 @@ public function listCommand()
/**
* Run a single persisted task ignoring status and schedule.
*
* @param Task $task
* @param string $taskIdentifier
*/
public function runSingleCommand(Task $task) {
$taskDescriptor = $this->taskService->getTaskDescriptor(TaskInterface::TYPE_PERSISTED, $task);
$arguments = [$task->getImplementation(), $taskDescriptor['identifier'] ?: $taskDescriptor['type']];
public function runSingleCommand($taskIdentifier) {

$taskDescriptors = $this->taskService->getTasks();
Assertion::keyExists($taskDescriptors, $taskIdentifier, sprintf('Task with identifier %s does not exist.', $taskIdentifier));

$taskDescriptor = $taskDescriptors[$taskIdentifier];
/** @var Task $task */
$task = $taskDescriptor['object'];
$arguments = [$task->getImplementation(), $taskDescriptor['identifier']];

try {
$taskDescriptor['object']->execute($this->objectManager);
Expand Down
21 changes: 9 additions & 12 deletions Classes/Ttree/Scheduler/Service/TaskService.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class TaskService
public static function getAllTaskImplementations(ObjectManagerInterface $objectManager)
{
/** @var ReflectionService $reflectionService */
$reflectionService = $objectManager->get('TYPO3\Flow\Reflection\ReflectionService');
$reflectionService = $objectManager->get(ReflectionService::class);
return $reflectionService->getAllImplementationClassNamesForInterface(self::TASK_INTERFACE);
}

Expand All @@ -81,15 +81,14 @@ public static function getAllDynamicTaskImplementations(ObjectManagerInterface $
$tasks = [];
/** @var ReflectionService $reflectionService */
$reflectionService = $objectManager->get(ReflectionService::class);
foreach (self::getAllTaskImplementations($objectManager) as $className) {

foreach (self::getAllTaskImplementations($objectManager) as $className) {
if (!$reflectionService->isClassAnnotatedWith($className, Annotations\Schedule::class)) {
continue;
}

/** @var Annotations\Schedule $scheduleAnnotation */
/** @var Schedule $scheduleAnnotation */
$scheduleAnnotation = $reflectionService->getClassAnnotation($className, Annotations\Schedule::class);
$task = [
$tasks[$className] = [
'implementation' => $className,
'expression' => $scheduleAnnotation->expression,
'description' => ''
Expand All @@ -98,10 +97,8 @@ public static function getAllDynamicTaskImplementations(ObjectManagerInterface $
if($reflectionService->isClassAnnotatedWith($className, Annotations\Meta::class)) {
/** @var Annotations\Meta $metaAnnotation */
$metaAnnotation = $reflectionService->getClassAnnotation($className, Annotations\Meta::class);
$task['description'] = $metaAnnotation->description;
$tasks[$className]['description'] = $metaAnnotation->description;
}

$tasks[] = $task;
}

return $tasks;
Expand Down Expand Up @@ -148,7 +145,7 @@ public function getPersistedTasks()
$tasks = [];
foreach ($this->taskRepository->findAll() as $task) {
/** @var Task $task */
$tasks[] = $this->getTaskDescriptor(TaskInterface::TYPE_PERSISTED, $task);
$tasks[$this->persistenceManager->getIdentifierByObject($task)] = $this->getTaskDescriptor(TaskInterface::TYPE_PERSISTED, $task);
}
return $tasks;
}
Expand All @@ -173,8 +170,8 @@ public function getDynamicTasks($dueOnly = false)
$taskDecriptor = $this->getTaskDescriptor(TaskInterface::TYPE_DYNAMIC, $task);

$taskDecriptor['lastExecution'] = $lastExecution instanceof \DateTime ? $lastExecution->format(\DateTime::ISO8601) : '';
$taskDecriptor['identifier'] = '';
$tasks[] = $taskDecriptor;
$taskDecriptor['identifier'] = md5($dynamicTask['implementation']);
$tasks[$taskDecriptor['identifier']] = $taskDecriptor;
}
return $tasks;
}
Expand Down Expand Up @@ -247,7 +244,7 @@ public function update(Task $task, $type)
*/
protected function cleanupAndSortTaskList(array $tasks)
{
usort($tasks, function ($a, $b) {
uasort($tasks, function ($a, $b) {
return $a['nextExecutionTimestamp'] > $b['nextExecutionTimestamp'];
});

Expand Down

0 comments on commit be2fd68

Please sign in to comment.