Skip to content

Commit 5e67817

Browse files
adapted to fit new architecture
1 parent d057083 commit 5e67817

File tree

10 files changed

+112
-46
lines changed

10 files changed

+112
-46
lines changed

src/Command/RunCommand.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use Symfony\Component\Console\Command\Command;
66
use Symfony\Component\Console\Input\InputInterface;
77
use Symfony\Component\Console\Output\OutputInterface;
8-
use Task\SchedulerInterface;
8+
use Task\Runner\TaskRunnerInterface;
99

1010
/**
1111
* Run pending tasks.
@@ -15,15 +15,15 @@
1515
class RunCommand extends Command
1616
{
1717
/**
18-
* @var SchedulerInterface
18+
* @var TaskRunnerInterface
1919
*/
20-
private $scheduler;
20+
private $runner;
2121

22-
public function __construct($name, SchedulerInterface $scheduler)
22+
public function __construct($name, TaskRunnerInterface $scheduler)
2323
{
2424
parent::__construct($name);
2525

26-
$this->scheduler = $scheduler;
26+
$this->runner = $scheduler;
2727
}
2828

2929
/**
@@ -39,6 +39,6 @@ protected function configure()
3939
*/
4040
protected function execute(InputInterface $input, OutputInterface $output)
4141
{
42-
$this->scheduler->run();
42+
$this->runner->runTasks();
4343
}
4444
}

src/Command/ScheduleTaskCommand.php

Lines changed: 2 additions & 8 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\SchedulerInterface;
10+
use Task\Scheduler\SchedulerInterface;
1111

1212
/**
1313
* Schedule task.
@@ -38,8 +38,7 @@ protected function configure()
3838
->addArgument('handler', InputArgument::REQUIRED)
3939
->addArgument('workload', InputArgument::OPTIONAL)
4040
->addOption('cron-expression', 'c', InputOption::VALUE_REQUIRED)
41-
->addOption('end-date', 'e', InputOption::VALUE_REQUIRED)
42-
->addOption('key', 'k', InputOption::VALUE_REQUIRED);
41+
->addOption('end-date', 'e', InputOption::VALUE_REQUIRED);
4342
}
4443

4544
/**
@@ -51,7 +50,6 @@ protected function execute(InputInterface $input, OutputInterface $output)
5150
$workload = $input->getArgument('workload');
5251
$cronExpression = $input->getOption('cron-expression');
5352
$endDateString = $input->getOption('end-date');
54-
$key = $input->getOption('key');
5553

5654
if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) {
5755
$output->writeln(sprintf('Schedule task "%s" with workload "%s"', $handler, $workload));
@@ -68,10 +66,6 @@ protected function execute(InputInterface $input, OutputInterface $output)
6866
$taskBuilder->cron($cronExpression, new \DateTime(), $endDate);
6967
}
7068

71-
if ($key !== null) {
72-
$taskBuilder->setKey($key);
73-
}
74-
7569
$taskBuilder->schedule();
7670
}
7771
}

src/DependencyInjection/HandlerCompilerPass.php

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ class HandlerCompilerPass implements CompilerPassInterface
1515
{
1616
const REGISTRY_ID = 'task.handler_registry';
1717
const HANDLER_TAG = 'task.handler';
18-
const ADD_FUNCTION_NAME = 'add';
1918
const HANDLER_NAME_ATTRIBUTE = 'handler-name';
2019

2120
/**
@@ -27,16 +26,14 @@ public function process(ContainerBuilder $container)
2726
return;
2827
}
2928

30-
$definition = $container->findDefinition(self::REGISTRY_ID);
31-
29+
$handler = [];
3230
$taggedServices = $container->findTaggedServiceIds(self::HANDLER_TAG);
3331
foreach ($taggedServices as $id => $tags) {
34-
foreach ($tags as $attributes) {
35-
$definition->addMethodCall(
36-
self::ADD_FUNCTION_NAME,
37-
[$attributes[self::HANDLER_NAME_ATTRIBUTE], new Reference($id)]
38-
);
39-
}
32+
$service = $container->getDefinition($id);
33+
$handler[$service->getClass()] = new Reference($id);
4034
}
35+
36+
$definition = $container->findDefinition(self::REGISTRY_ID);
37+
$definition->replaceArgument(0, $handler);
4138
}
4239
}

src/Entity/TaskExecutionRepository.php

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

55
use Doctrine\ORM\EntityRepository;
6+
use Task\Execution\TaskExecutionInterface;
7+
use Task\Storage\TaskExecutionRepositoryInterface;
68
use Task\TaskInterface;
79
use Task\TaskStatus;
810

9-
class TaskExecutionRepository extends EntityRepository
11+
class TaskExecutionRepository extends EntityRepository implements TaskExecutionRepositoryInterface
1012
{
1113
public function findByStartTime(TaskInterface $task, \DateTime $scheduleTime)
1214
{
@@ -20,8 +22,12 @@ public function findByStartTime(TaskInterface $task, \DateTime $scheduleTime)
2022
->getResult();
2123
}
2224

23-
public function findScheduled(\DateTime $dateTime)
25+
public function findScheduled(\DateTime $dateTime = null)
2426
{
27+
if ($dateTime === null) {
28+
$dateTime = new \DateTime();
29+
}
30+
2531
return $this->createQueryBuilder('e')
2632
->innerJoin('e.task', 't')
2733
->where('e.status = :status AND e.scheduleTime < :dateTime')
@@ -30,4 +36,15 @@ public function findScheduled(\DateTime $dateTime)
3036
->getQuery()
3137
->getResult();
3238
}
39+
40+
public function save(TaskExecutionInterface $execution)
41+
{
42+
$this->_em->flush();
43+
}
44+
45+
public function add(TaskExecutionInterface $execution)
46+
{
47+
$this->_em->persist($execution);
48+
$this->_em->flush();
49+
}
3350
}

src/Entity/TaskRepository.php

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

55
use Doctrine\ORM\EntityRepository;
6+
use Task\Storage\TaskRepositoryInterface;
7+
use Task\TaskInterface;
68

7-
class TaskRepository extends EntityRepository
9+
class TaskRepository extends EntityRepository implements TaskRepositoryInterface
810
{
11+
/**
12+
* {@inheritdoc}
13+
*/
914
public function findEndBefore(\DateTime $dateTime)
1015
{
11-
$this->createQueryBuilder('t')
16+
return $this->createQueryBuilder('t')
1217
->where('t.lastExecution IS NOT NULL OR t.lastExecution > :dateTime')
1318
->setParameter('dateTime', $dateTime)
14-
->getQuery()->getResult();
19+
->getQuery()
20+
->getResult();
21+
}
22+
23+
/**
24+
* {@inheritdoc}
25+
*/
26+
public function add(TaskInterface $task)
27+
{
28+
$this->_em->persist($task);
29+
$this->_em->flush();
30+
}
31+
32+
/**
33+
* {@inheritdoc}
34+
*/
35+
public function findEndBeforeNow()
36+
{
37+
return $this->findEndBefore(new \DateTime());
1538
}
1639
}

src/Factory.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Task\TaskBundle;
4+
5+
use Task\TaskBundle\Entity\Task;
6+
7+
class Factory extends \Task\Factory
8+
{
9+
/**
10+
* {@inheritdoc}
11+
*/
12+
public function createTask($handlerClass, $workload)
13+
{
14+
return new Task($handlerClass, $workload);
15+
}
16+
}

src/Resources/config/command.xml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,17 @@
55
<services>
66
<service id="task.command.run" class="Task\TaskBundle\Command\RunCommand">
77
<argument type="string">task:run</argument>
8-
<argument type="service" id="task.scheduler"/>
8+
<argument type="service" id="task.runner"/>
99

1010
<tag name="console.command"/>
1111
</service>
1212

13-
<service id="task.command.run.handler" class="Task\TaskBundle\Command\RunHandlerCommand">
13+
<!--<service id="task.command.run.handler" class="Task\TaskBundle\Command\RunHandlerCommand">
1414
<argument type="string">task:run:handler</argument>
1515
<argument type="service" id="task.handler_registry"/>
1616
1717
<tag name="console.command"/>
18-
</service>
18+
</service>-->
1919

2020
<service id="task.command.schedule_task" class="Task\TaskBundle\Command\ScheduleTaskCommand">
2121
<argument type="string">task:schedule:task</argument>
@@ -24,11 +24,11 @@
2424
<tag name="console.command"/>
2525
</service>
2626

27-
<service id="task.command.debug_tasks" class="Task\TaskBundle\Command\DebugTasksCommand">
27+
<!--<service id="task.command.debug_tasks" class="Task\TaskBundle\Command\DebugTasksCommand">
2828
<argument type="string">debug:tasks</argument>
2929
<argument type="service" id="task.storage"/>
3030
3131
<tag name="console.command"/>
32-
</service>
32+
</service>-->
3333
</services>
3434
</container>

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
33
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
44
xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
5-
<entity name="Task\TaskBundle\Entity\Task" table="ta_tasks" repository-class="Entity\TaskRepository">
5+
<entity name="Task\TaskBundle\Entity\Task" table="ta_tasks"
6+
repository-class="Task\TaskBundle\Entity\TaskRepository">
67

78
<indexes>
89
<index columns="uuid"/>
@@ -15,7 +16,7 @@
1516
<field name="uuid" type="string" length="100" unique="true"/>
1617
<field name="handlerClass" type="string" length="255"/>
1718
<field name="intervalExpression" type="string" length="255" nullable="true"/>
18-
<field name="firstExecution" type="datetime"/>
19+
<field name="firstExecution" type="datetime" nullable="true"/>
1920
<field name="lastExecution" type="datetime" nullable="true"/>
2021
<field name="workload" type="object"/>
2122

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
44
xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
55
<entity name="Task\TaskBundle\Entity\TaskExecution" table="ta_task_executions"
6-
repository-class="Entity\TaskExecutionRepository">
6+
repository-class="Task\TaskBundle\Entity\TaskExecutionRepository">
77

88
<indexes>
99
<index columns="uuid"/>
10-
<index columns="scheduleTime"/>
10+
<index columns="schedule_time"/>
1111
</indexes>
1212

1313
<id name="id" type="integer">
@@ -20,7 +20,7 @@
2020
<field name="duration" type="float"/>
2121
<field name="startTime" type="datetime"/>
2222
<field name="endTime" type="datetime"/>
23-
<field name="scheduleTime" type="datetime"/>
23+
<field name="scheduleTime" column="schedule_time" type="datetime"/>
2424
<field name="exception" type="object"/>
2525
<field name="result" type="object"/>
2626
<field name="status" type="string" length="20"/>

src/Resources/config/scheduler.xml

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,33 @@
33
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
44
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
55
<services>
6-
<service id="task.handler_registry" class="Task\Handler\Registry"/>
7-
<service id="task.builder_factory" class="Task\TaskBuilderFactory"/>
6+
<service id="task.factory" class="Task\TaskBundle\Factory"/>
87

9-
<service id="task.scheduler" class="Task\Scheduler">
10-
<argument type="service" id="task.storage"/>
11-
<argument type="service" id="task.handler_registry"/>
12-
<argument type="service" id="task.builder_factory"/>
13-
<argument type="service" id="event_dispatcher"/>
14-
<argument type="service" id="logger" on-invalid="ignore"/>
8+
<service id="task.storage.task" class="Task\TaskBundle\Entity\TaskRepository">
9+
<factory service="doctrine.orm.entity_manager" method="getRepository"/>
10+
11+
<argument type="string">TaskBundle:Task</argument>
12+
</service>
13+
14+
<service id="task.storage.task_execution" class="Task\TaskBundle\Entity\TaskExecutionRepository">
15+
<factory service="doctrine.orm.entity_manager" method="getRepository"/>
16+
17+
<argument type="string">TaskBundle:TaskExecution</argument>
18+
</service>
19+
20+
<service id="task.scheduler" class="Task\Scheduler\Scheduler">
21+
<argument type="service" id="task.factory"/>
22+
<argument type="service" id="task.storage.task"/>
23+
<argument type="service" id="task.storage.task_execution"/>
24+
</service>
25+
26+
<service id="task.handler.factory" class="Task\TaskBundle\Handler\TaskHandlerFactory">
27+
<argument type="collection"/>
28+
</service>
29+
30+
<service id="task.runner" class="Task\Runner\TaskRunner">
31+
<argument type="service" id="task.storage.task_execution"/>
32+
<argument type="service" id="task.handler.factory"/>
1533
</service>
1634
</services>
1735
</container>

0 commit comments

Comments
 (0)