Skip to content

Commit 0589418

Browse files
Merge branch 'feature/task-timing'
2 parents 95af33f + f38a848 commit 0589418

File tree

3 files changed

+104
-3
lines changed

3 files changed

+104
-3
lines changed

src/Command/DebugTasksCommand.php

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
3+
namespace Task\TaskBundle\Command;
4+
5+
use Symfony\Component\Console\Command\Command;
6+
use Symfony\Component\Console\Helper\Table;
7+
use Symfony\Component\Console\Input\InputInterface;
8+
use Symfony\Component\Console\Input\InputOption;
9+
use Symfony\Component\Console\Output\OutputInterface;
10+
use Task\Storage\StorageInterface;
11+
12+
/**
13+
* Run pending tasks.
14+
*
15+
* @author @wachterjohannes <[email protected]>
16+
*/
17+
class DebugTasksCommand extends Command
18+
{
19+
/**
20+
* @var StorageInterface
21+
*/
22+
private $storage;
23+
24+
public function __construct($name, StorageInterface $storage)
25+
{
26+
parent::__construct($name);
27+
28+
$this->storage = $storage;
29+
}
30+
31+
/**
32+
* {@inheritdoc}
33+
*/
34+
protected function configure()
35+
{
36+
$this->setDescription('Debug tasks')
37+
->addOption('limit', 'l', InputOption::VALUE_REQUIRED, '', null)
38+
->addOption('key', 'k', InputOption::VALUE_REQUIRED, '', null);
39+
}
40+
41+
/**
42+
* {@inheritdoc}
43+
*/
44+
protected function execute(InputInterface $input, OutputInterface $output)
45+
{
46+
$key = $input->getOption('key');
47+
$limit = $input->getOption('limit');
48+
49+
if (null !== $key) {
50+
$tasks = $this->storage->findByKey($key, $limit);
51+
} else {
52+
$tasks = $this->storage->findAll($limit);
53+
}
54+
55+
$table = new Table($output);
56+
$table->setHeaders(array('uuid', 'key', 'task-name', 'execution-date', 'completed', 'start', 'duration'));
57+
58+
foreach ($tasks as $task) {
59+
$start = null;
60+
$duration = null;
61+
if ($task->getLastExecution()) {
62+
$start = $task->getLastExecution()->getFinishedAtAsDateTime()->format(\DateTime::RFC3339);
63+
$duration = $task->getLastExecution()->getExecutionDuration();
64+
}
65+
66+
$table->addRow(
67+
[
68+
$task->getUuid(),
69+
$task->getKey(),
70+
$task->getTaskName(),
71+
$task->getExecutionDate()->format(\DateTime::RFC3339),
72+
$task->isCompleted(),
73+
$start,
74+
$duration,
75+
]
76+
);
77+
}
78+
79+
$table->render();
80+
}
81+
}

src/Resources/config/command.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,12 @@
2323

2424
<tag name="console.command"/>
2525
</service>
26+
27+
<service id="task.command.debug_tasks" class="Task\TaskBundle\Command\DebugTasksCommand">
28+
<argument type="string">debug:tasks</argument>
29+
<argument type="service" id="task.storage"/>
30+
31+
<tag name="console.command"/>
32+
</service>
2633
</services>
2734
</container>

src/Storage/DoctrineStorage.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,26 @@ function (TaskEntity $entity) {
7070
/**
7171
* {@inheritdoc}
7272
*/
73-
public function findAll()
73+
public function findAll($limit = null)
7474
{
7575
return array_map(
7676
function (TaskEntity $entity) {
7777
return $entity->getTask();
7878
},
79-
$this->taskRepository->findAll()
79+
$this->taskRepository->findBy([], null, $limit)
80+
);
81+
}
82+
83+
/**
84+
* {@inheritdoc}
85+
*/
86+
public function findByKey($key, $limit = null)
87+
{
88+
return array_map(
89+
function (TaskEntity $entity) {
90+
return $entity->getTask();
91+
},
92+
$this->taskRepository->findBy(['key' => $key], null, $limit)
8093
);
8194
}
8295

@@ -103,7 +116,7 @@ public function clear()
103116

104117
private function setTask(TaskEntity $entity, TaskInterface $task)
105118
{
106-
$entity->setTask($task);
119+
$entity->setTask(clone $task);
107120
$entity->setUuid($task->getUuid());
108121
$entity->setKey($task->getKey());
109122
$entity->setCompleted($task->isCompleted());

0 commit comments

Comments
 (0)