Skip to content

Commit 85506c7

Browse files
finished prototype
1 parent 5e67817 commit 85506c7

15 files changed

+117
-87
lines changed

src/Command/DebugTasksCommand.php

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use Symfony\Component\Console\Input\InputInterface;
88
use Symfony\Component\Console\Input\InputOption;
99
use Symfony\Component\Console\Output\OutputInterface;
10-
use Task\Storage\StorageInterface;
10+
use Task\Storage\TaskExecutionRepositoryInterface;
1111

1212
/**
1313
* Run pending tasks.
@@ -17,11 +17,11 @@
1717
class DebugTasksCommand extends Command
1818
{
1919
/**
20-
* @var StorageInterface
20+
* @var TaskExecutionRepositoryInterface
2121
*/
2222
private $storage;
2323

24-
public function __construct($name, StorageInterface $storage)
24+
public function __construct($name, TaskExecutionRepositoryInterface $storage)
2525
{
2626
parent::__construct($name);
2727

@@ -34,44 +34,30 @@ public function __construct($name, StorageInterface $storage)
3434
protected function configure()
3535
{
3636
$this->setDescription('Debug tasks')
37-
->addOption('limit', 'l', InputOption::VALUE_REQUIRED, '', null)
38-
->addOption('key', 'k', InputOption::VALUE_REQUIRED, '', null);
37+
->addOption('limit', 'l', InputOption::VALUE_REQUIRED, '', null);
3938
}
4039

4140
/**
4241
* {@inheritdoc}
4342
*/
4443
protected function execute(InputInterface $input, OutputInterface $output)
4544
{
46-
$key = $input->getOption('key');
4745
$limit = $input->getOption('limit');
4846

49-
if (null !== $key) {
50-
$tasks = $this->storage->findByKey($key, $limit);
51-
} else {
52-
$tasks = $this->storage->findAll($limit);
53-
}
47+
$executions = $this->storage->findAll($limit);
5448

5549
$table = new Table($output);
56-
$table->setHeaders(['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-
}
50+
$table->setHeaders(['uuid', 'status', 'handler', 'schedule time', 'end time', 'duration']);
6551

52+
foreach ($executions as $execution) {
6653
$table->addRow(
6754
[
68-
$task->getUuid(),
69-
$task->getKey(),
70-
$task->getTaskName(),
71-
$task->getExecutionDate()->format(\DateTime::RFC3339),
72-
$task->isCompleted(),
73-
$start,
74-
$duration,
55+
$execution->getUuid(),
56+
$execution->getStatus(),
57+
$execution->getHandlerClass(),
58+
$execution->getScheduleTime()->format(\DateTime::RFC3339),
59+
!$execution->getEndTime() ? '' : $execution->getEndTime()->format(\DateTime::RFC3339),
60+
$execution->getDuration(),
7561
]
7662
);
7763
}

src/Command/RunCommand.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Symfony\Component\Console\Input\InputInterface;
77
use Symfony\Component\Console\Output\OutputInterface;
88
use Task\Runner\TaskRunnerInterface;
9+
use Task\Scheduler\SchedulerInterface;
910

1011
/**
1112
* Run pending tasks.
@@ -19,11 +20,17 @@ class RunCommand extends Command
1920
*/
2021
private $runner;
2122

22-
public function __construct($name, TaskRunnerInterface $scheduler)
23+
/**
24+
* @var SchedulerInterface
25+
*/
26+
private $scheduler;
27+
28+
public function __construct($name, TaskRunnerInterface $runner, SchedulerInterface $scheduler)
2329
{
2430
parent::__construct($name);
2531

26-
$this->runner = $scheduler;
32+
$this->runner = $runner;
33+
$this->scheduler = $scheduler;
2734
}
2835

2936
/**
@@ -40,5 +47,6 @@ protected function configure()
4047
protected function execute(InputInterface $input, OutputInterface $output)
4148
{
4249
$this->runner->runTasks();
50+
$this->scheduler->scheduleTasks();
4351
}
4452
}

src/Command/RunHandlerCommand.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use Symfony\Component\Console\Input\InputArgument;
77
use Symfony\Component\Console\Input\InputInterface;
88
use Symfony\Component\Console\Output\OutputInterface;
9-
use Task\Handler\RegistryInterface;
9+
use Task\Handler\TaskHandlerFactoryInterface;
1010

1111
/**
1212
* Run pending tasks.
@@ -16,15 +16,15 @@
1616
class RunHandlerCommand extends Command
1717
{
1818
/**
19-
* @var RegistryInterface
19+
* @var TaskHandlerFactoryInterface
2020
*/
21-
private $registry;
21+
private $handlerFactory;
2222

23-
public function __construct($name, RegistryInterface $registry)
23+
public function __construct($name, TaskHandlerFactoryInterface $handlerFactory)
2424
{
2525
parent::__construct($name);
2626

27-
$this->registry = $registry;
27+
$this->handlerFactory = $handlerFactory;
2828
}
2929

3030
/**
@@ -33,7 +33,7 @@ public function __construct($name, RegistryInterface $registry)
3333
protected function configure()
3434
{
3535
$this->setDescription('Run pending tasks')
36-
->addArgument('handler', InputArgument::REQUIRED)
36+
->addArgument('handlerClass', InputArgument::REQUIRED)
3737
->addArgument('workload', InputArgument::OPTIONAL);
3838
}
3939

@@ -42,14 +42,14 @@ protected function configure()
4242
*/
4343
protected function execute(InputInterface $input, OutputInterface $output)
4444
{
45-
$handler = $input->getArgument('handler');
45+
$handlerClass = $input->getArgument('handlerClass');
4646
$workload = $input->getArgument('workload');
4747

4848
if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) {
49-
$output->writeln(sprintf('Run command "%s" with workload "%s"', $handler, $workload));
49+
$output->writeln(sprintf('Run command "%s" with workload "%s"', $handlerClass, $workload));
5050
}
5151

52-
$result = $this->registry->run($handler, $workload);
52+
$result = $this->handlerFactory->create($handlerClass)->handle($workload);
5353

5454
if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) {
5555
$output->writeln(sprintf('Result: %s', json_encode($result)));

src/Command/ScheduleTaskCommand.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ class ScheduleTaskCommand extends Command
2121
*/
2222
private $scheduler;
2323

24-
public function __construct($name, SchedulerInterface $scheduler)
24+
public function __construct($name, SchedulerInterface $runner)
2525
{
2626
parent::__construct($name);
2727

28-
$this->scheduler = $scheduler;
28+
$this->scheduler = $runner;
2929
}
3030

3131
/**

src/DependencyInjection/HandlerCompilerPass.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
*/
1414
class HandlerCompilerPass implements CompilerPassInterface
1515
{
16-
const REGISTRY_ID = 'task.handler_registry';
16+
const REGISTRY_ID = 'task.handler.factory';
1717
const HANDLER_TAG = 'task.handler';
1818
const HANDLER_NAME_ATTRIBUTE = 'handler-name';
1919

src/DoctrineStorage/TaskExecutionRepository.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,16 @@ class TaskExecutionRepository implements TaskExecutionRepositoryInterface
2020
*/
2121
private $taskExecutionRepository;
2222

23+
/**
24+
* @param ObjectManager $objectManager
25+
* @param ORMTaskExecutionRepository $taskExecutionRepository
26+
*/
27+
public function __construct(ObjectManager $objectManager, ORMTaskExecutionRepository $taskExecutionRepository)
28+
{
29+
$this->objectManager = $objectManager;
30+
$this->taskExecutionRepository = $taskExecutionRepository;
31+
}
32+
2333
/**
2434
* {@inheritdoc}
2535
*/
@@ -34,6 +44,7 @@ public function save(TaskExecutionInterface $execution)
3444
*/
3545
public function add(TaskExecutionInterface $execution)
3646
{
47+
$this->objectManager->persist($execution);
3748
$this->objectManager->flush();
3849
}
3950

@@ -52,4 +63,9 @@ public function findByStartTime(TaskInterface $task, \DateTime $scheduleTime)
5263
{
5364
return $this->taskExecutionRepository->findByStartTime($task, $scheduleTime);
5465
}
66+
67+
public function findAll($limit = null)
68+
{
69+
return $this->taskExecutionRepository->findBy([], ['scheduleTime' => 'ASC'], $limit);
70+
}
5571
}

src/DoctrineStorage/TaskRepository.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,16 @@ class TaskRepository implements TaskRepositoryInterface
1919
*/
2020
private $taskRepository;
2121

22+
/**
23+
* @param ObjectManager $objectManager
24+
* @param ORMTaskRepository $taskRepository
25+
*/
26+
public function __construct(ObjectManager $objectManager, ORMTaskRepository $taskRepository)
27+
{
28+
$this->objectManager = $objectManager;
29+
$this->taskRepository = $taskRepository;
30+
}
31+
2232
/**
2333
* {@inheritdoc}
2434
*/
@@ -31,9 +41,9 @@ public function add(TaskInterface $task)
3141
/**
3242
* {@inheritdoc}
3343
*/
34-
public function findAll()
44+
public function findAll($limit = null)
3545
{
36-
return $this->taskRepository->findAll();
46+
return $this->taskRepository->findBy([], null, $limit);
3747
}
3848

3949
/**

src/Entity/TaskExecutionRepository.php

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,27 @@
33
namespace Task\TaskBundle\Entity;
44

55
use Doctrine\ORM\EntityRepository;
6+
use Doctrine\ORM\NoResultException;
67
use Task\Execution\TaskExecutionInterface;
7-
use Task\Storage\TaskExecutionRepositoryInterface;
88
use Task\TaskInterface;
99
use Task\TaskStatus;
1010

11-
class TaskExecutionRepository extends EntityRepository implements TaskExecutionRepositoryInterface
11+
class TaskExecutionRepository extends EntityRepository
1212
{
1313
public function findByStartTime(TaskInterface $task, \DateTime $scheduleTime)
1414
{
15-
return $this->createQueryBuilder('e')
16-
->innerJoin('e.task', 't')
17-
->where('t.uuid = :uuid')
18-
->andWhere('e.scheduleTime = :scheduleTime')
19-
->setParameter('uuid', $task->getUuid())
20-
->setParameter('scheduleTime', $scheduleTime)
21-
->getQuery()
22-
->getResult();
15+
try {
16+
return $this->createQueryBuilder('e')
17+
->innerJoin('e.task', 't')
18+
->where('t.uuid = :uuid')
19+
->andWhere('e.scheduleTime = :scheduleTime')
20+
->setParameter('uuid', $task->getUuid())
21+
->setParameter('scheduleTime', $scheduleTime)
22+
->getQuery()
23+
->getSingleResult();
24+
} catch (NoResultException $e) {
25+
return null;
26+
}
2327
}
2428

2529
public function findScheduled(\DateTime $dateTime = null)

src/Entity/TaskRepository.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,17 @@
33
namespace Task\TaskBundle\Entity;
44

55
use Doctrine\ORM\EntityRepository;
6-
use Task\Storage\TaskRepositoryInterface;
76
use Task\TaskInterface;
87

9-
class TaskRepository extends EntityRepository implements TaskRepositoryInterface
8+
class TaskRepository extends EntityRepository
109
{
1110
/**
1211
* {@inheritdoc}
1312
*/
1413
public function findEndBefore(\DateTime $dateTime)
1514
{
1615
return $this->createQueryBuilder('t')
17-
->where('t.lastExecution IS NOT NULL OR t.lastExecution > :dateTime')
16+
->where('t.lastExecution IS NULL OR t.lastExecution >= :dateTime')
1817
->setParameter('dateTime', $dateTime)
1918
->getQuery()
2019
->getResult();

src/Factory.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace Task\TaskBundle;
44

55
use Task\TaskBundle\Entity\Task;
6+
use Task\TaskBundle\Entity\TaskExecution;
7+
use Task\TaskInterface;
68

79
class Factory extends \Task\Factory
810
{
@@ -13,4 +15,12 @@ public function createTask($handlerClass, $workload)
1315
{
1416
return new Task($handlerClass, $workload);
1517
}
18+
19+
/**
20+
* {@inheritdoc}
21+
*/
22+
public function createTaskExecution(TaskInterface $task, \DateTime $scheduleTime)
23+
{
24+
return new TaskExecution($task, $task->getHandlerClass(), $scheduleTime, $task->getWorkload());
25+
}
1626
}

src/Resources/config/command.xml

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,17 @@
66
<service id="task.command.run" class="Task\TaskBundle\Command\RunCommand">
77
<argument type="string">task:run</argument>
88
<argument type="service" id="task.runner"/>
9+
<argument type="service" id="task.scheduler"/>
910

1011
<tag name="console.command"/>
1112
</service>
1213

13-
<!--<service id="task.command.run.handler" class="Task\TaskBundle\Command\RunHandlerCommand">
14+
<service id="task.command.run.handler" class="Task\TaskBundle\Command\RunHandlerCommand">
1415
<argument type="string">task:run:handler</argument>
15-
<argument type="service" id="task.handler_registry"/>
16+
<argument type="service" id="task.handler.factory"/>
1617

1718
<tag name="console.command"/>
18-
</service>-->
19+
</service>
1920

2021
<service id="task.command.schedule_task" class="Task\TaskBundle\Command\ScheduleTaskCommand">
2122
<argument type="string">task:schedule:task</argument>
@@ -24,11 +25,11 @@
2425
<tag name="console.command"/>
2526
</service>
2627

27-
<!--<service id="task.command.debug_tasks" class="Task\TaskBundle\Command\DebugTasksCommand">
28+
<service id="task.command.debug_tasks" class="Task\TaskBundle\Command\DebugTasksCommand">
2829
<argument type="string">debug:tasks</argument>
29-
<argument type="service" id="task.storage"/>
30+
<argument type="service" id="task.storage.task_execution"/>
3031

3132
<tag name="console.command"/>
32-
</service>-->
33+
</service>
3334
</services>
3435
</container>

src/Resources/config/doctrine/TaskExecution.orm.xml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@
1717
<field name="uuid" type="string" length="100" unique="true"/>
1818
<field name="handlerClass" type="string" length="255"/>
1919
<field name="workload" type="object"/>
20-
<field name="duration" type="float"/>
21-
<field name="startTime" type="datetime"/>
22-
<field name="endTime" type="datetime"/>
20+
<field name="duration" type="float" nullable="true"/>
21+
<field name="startTime" type="datetime" nullable="true"/>
22+
<field name="endTime" type="datetime" nullable="true"/>
2323
<field name="scheduleTime" column="schedule_time" type="datetime"/>
24-
<field name="exception" type="object"/>
25-
<field name="result" type="object"/>
24+
<field name="exception" type="object" nullable="true"/>
25+
<field name="result" type="object" nullable="true"/>
2626
<field name="status" type="string" length="20"/>
2727

2828
<many-to-one target-entity="Task\TaskBundle\Entity\Task" field="task"/>

0 commit comments

Comments
 (0)