Skip to content

Commit 1c4c8dc

Browse files
add tagged service task compilerpass
1 parent 3296668 commit 1c4c8dc

File tree

6 files changed

+108
-1
lines changed

6 files changed

+108
-1
lines changed

src/DependencyInjection/HandlerCompilerPass.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class HandlerCompilerPass implements CompilerPassInterface
1616
const REGISTRY_ID = 'task.handler_registry';
1717
const HANDLER_TAG = 'task.handler';
1818
const ADD_FUNCTION_NAME = 'add';
19+
const HANDLER_NAME_ATTRIBUTE = 'handler-name';
1920

2021
/**
2122
* {@inheritdoc}
@@ -33,7 +34,7 @@ public function process(ContainerBuilder $container)
3334
foreach ($tags as $attributes) {
3435
$definition->addMethodCall(
3536
self::ADD_FUNCTION_NAME,
36-
[$attributes['handler-name'], new Reference($id)]
37+
[$attributes[self::HANDLER_NAME_ATTRIBUTE], new Reference($id)]
3738
);
3839
}
3940
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
namespace Task\TaskBundle\DependencyInjection;
4+
5+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
6+
use Symfony\Component\DependencyInjection\ContainerBuilder;
7+
use Symfony\Component\DependencyInjection\Reference;
8+
9+
/**
10+
* Compiler pass which collects worker services.
11+
*
12+
* @author @wachterjohannes <[email protected]>
13+
*/
14+
class TaskCompilerPass implements CompilerPassInterface
15+
{
16+
const SCHEDULER_ID = 'task.scheduler';
17+
const INTERVAL_TAG = 'task.interval';
18+
const KEY_ATTRIBUTE = 'key';
19+
const INTERVAL_ATTRIBUTE = 'interval';
20+
const WORKLOAD_ATTRIBUTE = 'workload';
21+
const CREATE_FUNCTION_NAME = 'createTaskAndSchedule';
22+
23+
/**
24+
* {@inheritdoc}
25+
*/
26+
public function process(ContainerBuilder $container)
27+
{
28+
if (!$container->has(self::SCHEDULER_ID)) {
29+
return;
30+
}
31+
32+
$schedulerDefinition = $container->getDefinition(self::SCHEDULER_ID);
33+
34+
$taggedServices = $container->findTaggedServiceIds(self::INTERVAL_TAG);
35+
foreach ($taggedServices as $id => $tags) {
36+
$handlerDefinition = $container->getDefinition($id);
37+
$tag = $handlerDefinition->getTag(HandlerCompilerPass::HANDLER_TAG);
38+
39+
$handler = $tag[0][HandlerCompilerPass::HANDLER_NAME_ATTRIBUTE];
40+
41+
// remove all tasks with $id and not completed
42+
foreach ($tags as $attributes) {
43+
$interval = $attributes[self::INTERVAL_ATTRIBUTE];
44+
$workload = isset($attributes[self::WORKLOAD_ATTRIBUTE]) ? $attributes[self::WORKLOAD_ATTRIBUTE] : null;
45+
$key = isset($attributes[self::KEY_ATTRIBUTE]) ? $attributes[self::KEY_ATTRIBUTE] : null;
46+
47+
if (!$key) {
48+
$key = $handler . '_' . $interval . '_' . serialize($workload);
49+
}
50+
51+
52+
$schedulerDefinition->addMethodCall(
53+
self::CREATE_FUNCTION_NAME,
54+
[
55+
$handler,
56+
$interval,
57+
$workload,
58+
$key,
59+
]
60+
);
61+
}
62+
}
63+
}
64+
}

src/Entity/Task.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ class Task
1616
*/
1717
private $uuid;
1818

19+
/**
20+
* @var string
21+
*/
22+
private $key;
23+
1924
/**
2025
* @var TaskInterface
2126
*/
@@ -102,4 +107,21 @@ public function setCompleted($completed)
102107
{
103108
$this->completed = $completed;
104109
}
110+
111+
/**
112+
* @return string
113+
*/
114+
public function getKey()
115+
{
116+
return $this->key;
117+
}
118+
119+
/**
120+
* @param string $key
121+
* @return $this
122+
*/
123+
public function setKey($key)
124+
{
125+
$this->key = $key;
126+
}
105127
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
<field name="task" type="object"/>
1414
<field name="uuid" type="string" length="100"/>
15+
<field name="key" type="string" length="255" nullable="true" column="task_key"/>
1516
<field name="executionDate" type="datetime"/>
1617
<field name="completed" type="boolean"/>
1718

src/Storage/DoctrineStorage.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,22 @@ public function __construct(EntityManagerInterface $entityManager, TaskRepositor
3232
public function store(TaskInterface $task)
3333
{
3434
$entity = new TaskEntity();
35+
36+
if ($task->getKey()) {
37+
$oldEntity = $this->taskRepository->findOneBy(
38+
[
39+
'key' => $task->getKey(),
40+
'completed' => false,
41+
]
42+
);
43+
44+
if ($oldEntity) {
45+
// TODO update task (warning execution date should not be changed)
46+
47+
return;
48+
}
49+
}
50+
3551
$this->setTask($entity, $task);
3652

3753
$this->entityManager->persist($entity);
@@ -89,6 +105,7 @@ private function setTask(TaskEntity $entity, TaskInterface $task)
89105
{
90106
$entity->setTask($task);
91107
$entity->setUuid($task->getUuid());
108+
$entity->setKey($task->getKey());
92109
$entity->setCompleted($task->isCompleted());
93110
$entity->setExecutionDate($task->getExecutionDate());
94111
}

src/TaskBundle.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Symfony\Component\DependencyInjection\ContainerBuilder;
66
use Symfony\Component\HttpKernel\Bundle\Bundle;
77
use Task\TaskBundle\DependencyInjection\HandlerCompilerPass;
8+
use Task\TaskBundle\DependencyInjection\TaskCompilerPass;
89

910
/**
1011
* Integrates php-task into symfony.
@@ -18,5 +19,6 @@ public function build(ContainerBuilder $container)
1819
parent::build($container);
1920

2021
$container->addCompilerPass(new HandlerCompilerPass());
22+
$container->addCompilerPass(new TaskCompilerPass());
2123
}
2224
}

0 commit comments

Comments
 (0)