Skip to content

Commit

Permalink
Merge pull request #2 from Flowpack/feature/task-execution-history
Browse files Browse the repository at this point in the history
FEATURE: Truncate the execution history to a definable amount of entries
  • Loading branch information
daniellienert authored Sep 17, 2021
2 parents 3e7e4e0 + 0b2739f commit 676db69
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 6 deletions.
16 changes: 13 additions & 3 deletions Classes/Command/TaskCommandController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Flowpack\Task\Domain\Scheduler\Scheduler;
use Flowpack\Task\Domain\Task\Task;
use Flowpack\Task\Domain\Task\TaskCollectionFactory;
use Flowpack\Task\Domain\Task\TaskExecutionHistory;
use Flowpack\Task\Domain\Task\TaskInterface;
use Flowpack\Task\Domain\Task\TaskStatus;
use Neos\Flow\Annotations as Flow;
Expand Down Expand Up @@ -41,6 +42,12 @@ class TaskCommandController extends CommandController
*/
protected $taskRunner;

/**
* @Flow\Inject
* @var TaskExecutionHistory
*/
protected $taskExecutionHistory;

protected array $lastExecutionStatusMapping = [
TaskStatus::FAILED => 'error',
TaskStatus::COMPLETED => 'success',
Expand All @@ -55,6 +62,7 @@ public function runCommand(): void
{
$this->scheduler->scheduleTasks();
$this->taskRunner->runTasks();
$this->taskExecutionHistory->cleanup();
}

/**
Expand All @@ -69,6 +77,7 @@ public function runSingleCommand(string $taskIdentifier): void
$this->scheduler->scheduleTask($task);
$this->taskRunner->runTasks();
$this->scheduler->scheduleTasks();
$this->taskExecutionHistory->cleanup();
}

/**
Expand All @@ -80,7 +89,7 @@ public function listCommand(): void

$this->output->outputTable(array_map(function (TaskInterface $task) {
/** @var TaskExecution $latestExecution */
$latestExecution = $this->taskExecutionRepository->findLatest($task)->getFirst();
$latestExecution = $this->taskExecutionRepository->findLatestExecution($task, 1, 0)->getFirst();
return [
$task->getIdentifier(),
$task->getLabel(),
Expand All @@ -98,6 +107,7 @@ public function listCommand(): void

/**
* @param string $taskIdentifier
* @throws \JsonException
*/
public function showCommand(string $taskIdentifier): void
{
Expand All @@ -119,14 +129,14 @@ public function showCommand(string $taskIdentifier): void
);

$this->outputLine(PHP_EOL . '<b>Task Executions</b>');
$taskExecutions = $this->taskExecutionRepository->findLatest($task);
$taskExecutions = $this->taskExecutionRepository->findLatestExecution($task);

if ($taskExecutions->count() === 0) {
$this->outputLine('This task has not yet been executed.');
return;
}

foreach ($this->taskExecutionRepository->findLatest($task) as $execution) {
foreach ($taskExecutions as $execution) {
/** @var TaskExecution $execution */
$this->outputLine(sprintf('<b>%s</b>', $execution->getScheduleTime()));
}
Expand Down
15 changes: 13 additions & 2 deletions Classes/Domain/Repository/TaskExecutionRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function removePlannedTask(Task $task): void
}
}

public function findLatest(Task $task, int $limit = 5): QueryResultInterface
public function findLatestExecution(Task $task, int $limit = 5, int $offset = 0): QueryResultInterface
{
$query = $this->createQuery();

Expand All @@ -69,7 +69,16 @@ public function findLatest(Task $task, int $limit = 5): QueryResultInterface
$query->equals('status', TaskStatus::PLANNED)
)
)
)->setOrderings(['scheduleTime' => QueryInterface::ORDER_DESCENDING]);
)
->setOrderings(['scheduleTime' => QueryInterface::ORDER_DESCENDING]);

if ($limit > 0) {
$query->setLimit($limit);
}

if ($offset > 0) {
$query->setOffset($offset);
}

return $query->execute();
}
Expand Down Expand Up @@ -100,4 +109,6 @@ public function findNextScheduled(DateTime $runTime, array $skippedExecutions =

return $queryBuilder->getQuery()->getOneOrNullResult(AbstractQuery::HYDRATE_OBJECT);
}


}
10 changes: 10 additions & 0 deletions Classes/Domain/Runner/TaskRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@

namespace Flowpack\Task\Domain\Runner;

/*
* This file is part of the Flowpack.Task package.
*
* (c) Contributors of the Flowpack Team - flowpack.org
*
* This package is Open Source Software. For the full copyright and license
* information, please view the LICENSE file which was distributed with this
* source code.
*/

use DateTime;
use Doctrine\ORM\OptimisticLockException;
use Doctrine\ORM\ORMException;
Expand Down
66 changes: 66 additions & 0 deletions Classes/Domain/Task/TaskExecutionHistory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php
declare(strict_types=1);

namespace Flowpack\Task\Domain\Task;

/*
* This file is part of the Flowpack.Task package.
*
* (c) Contributors of the Flowpack Team - flowpack.org
*
* This package is Open Source Software. For the full copyright and license
* information, please view the LICENSE file which was distributed with this
* source code.
*/

use Flowpack\Task\Domain\Model\TaskExecution;
use Neos\Flow\Annotations as Flow;
use Flowpack\Task\Domain\Repository\TaskExecutionRepository;
use Neos\Flow\Log\Utility\LogEnvironment;
use Psr\Log\LoggerInterface;

class TaskExecutionHistory
{

/**
* @Flow\Inject
* @var TaskExecutionRepository
*/
protected $taskExecutionRepository;

/**
* @Flow\InjectConfiguration(package="Flowpack.Task", path="keepTaskExecutionHistory")
* @var int
*/
protected int $keepTaskExecutionHistory = 3;

/**
* @Flow\Inject
* @var TaskCollectionFactory
*/
protected $taskCollectionFactory;

/**
* @Flow\Inject
* @var LoggerInterface
*/
protected $logger;

public function cleanup(): void
{
$removedTaskExecutions = 0;

/** @var Task $task */
foreach ($this->taskCollectionFactory->buildTasksFromConfiguration() as $task) {
/** @var TaskExecution $taskExecution */
foreach ($this->taskExecutionRepository->findLatestExecution($task, 0, $this->keepTaskExecutionHistory) as $taskExecution) {
$this->taskExecutionRepository->remove($taskExecution);
$removedTaskExecutions++;
}
}

if ($removedTaskExecutions > 0) {
$this->logger->info(sprintf('Removed %s completed task executions', $removedTaskExecutions), LogEnvironment::fromMethodName(__METHOD__));
}
}
}
1 change: 1 addition & 0 deletions Configuration/Settings.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ Flowpack:
# See https://symfony.com/doc/current/components/lock.html#available-stores for more options
lockStorage: 'flock://%FLOW_PATH_TEMPORARY%/SymfonyLockStorage'
tasks: []
keepTaskExecutionHistory: 3
9 changes: 8 additions & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ Flowpack:
### General Options
`lockStorage`: Configuration string for the lock storage used for taksHandler implementing `LockingTaskHandlerInterface`. See https://symfony.com/doc/current/components/lock.html#available-stores for more options
* `lockStorage`: Configuration string for the lock storage used for taskHandler implementing `LockingTaskHandlerInterface`. See https://symfony.com/doc/current/components/lock.html#available-stores for more options

* `keepTaskExecutionHistory`: Number of task executions to keep in the database. (default: 3)

## Implementing A Task Handler

Expand All @@ -58,6 +60,11 @@ Schedule and run due tasks
```sh
./flow task:run
```
Schedule and run a single task

```sh
./flow task:runSingle <taskIdentifier>
```

Show a list of all defined and scheduled tasks:

Expand Down

0 comments on commit 676db69

Please sign in to comment.