Skip to content

Add tagged service task compilerpass #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jan 13, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions src/Command/RunHandlerCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace Task\TaskBundle\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Task\Handler\RegistryInterface;
use Task\SchedulerInterface;

/**
* Run pending tasks.
*
* @author Alexander Schranz <alexander.schranz@massiveart.com>
*/
class RunHandlerCommand extends Command
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tests missing

{
/**
* @var RegistryInterface
*/
private $registry;

public function __construct(RegistryInterface $registry)
{
parent::__construct('task:run:handler');
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should move this to service definition.


$this->registry = $registry;
}

/**
* {@inheritdoc}
*/
protected function configure()
{
$this->setDescription('Run pending tasks')
->addArgument('handler', InputArgument::REQUIRED)
->addArgument('workload', InputArgument::OPTIONAL);
}

/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$handler = $input->getArgument('handler');
$workload = $input->getArgument('workload');

$output->writeln(sprintf('Run command "%s" with workload "%s"', $handler, $workload));

$result = $this->registry->run($handler, $workload);

$output->writeln(sprintf('Result: "%s"', $result));
}
}
3 changes: 2 additions & 1 deletion src/DependencyInjection/HandlerCompilerPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class HandlerCompilerPass implements CompilerPassInterface
const REGISTRY_ID = 'task.handler_registry';
const HANDLER_TAG = 'task.handler';
const ADD_FUNCTION_NAME = 'add';
const HANDLER_NAME_ATTRIBUTE = 'handler-name';

/**
* {@inheritdoc}
Expand All @@ -33,7 +34,7 @@ public function process(ContainerBuilder $container)
foreach ($tags as $attributes) {
$definition->addMethodCall(
self::ADD_FUNCTION_NAME,
[$attributes['handler-name'], new Reference($id)]
[$attributes[self::HANDLER_NAME_ATTRIBUTE], new Reference($id)]
);
}
}
Expand Down
63 changes: 63 additions & 0 deletions src/DependencyInjection/TaskCompilerPass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

namespace Task\TaskBundle\DependencyInjection;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

/**
* Compiler pass which collects worker services.
*
* @author @wachterjohannes <johannes.wachter@massiveart.com>
*/
class TaskCompilerPass implements CompilerPassInterface
{
const SCHEDULER_ID = 'task.scheduler';
const INTERVAL_TAG = 'task.interval';
const KEY_ATTRIBUTE = 'key';
const INTERVAL_ATTRIBUTE = 'interval';
const WORKLOAD_ATTRIBUTE = 'workload';
const CREATE_FUNCTION_NAME = 'createTaskAndSchedule';

/**
* {@inheritdoc}
*/
public function process(ContainerBuilder $container)
{
if (!$container->has(self::SCHEDULER_ID)) {
return;
}

$schedulerDefinition = $container->getDefinition(self::SCHEDULER_ID);

$taggedServices = $container->findTaggedServiceIds(self::INTERVAL_TAG);
foreach ($taggedServices as $id => $tags) {
$handlerDefinition = $container->getDefinition($id);
$tag = $handlerDefinition->getTag(HandlerCompilerPass::HANDLER_TAG);

// TODO handle also multiple handler tag here
$handler = $tag[0][HandlerCompilerPass::HANDLER_NAME_ATTRIBUTE];

// remove all tasks with $id and not completed
foreach ($tags as $attributes) {
$interval = $attributes[self::INTERVAL_ATTRIBUTE];
$workload = isset($attributes[self::WORKLOAD_ATTRIBUTE]) ? $attributes[self::WORKLOAD_ATTRIBUTE] : null;
$key = isset($attributes[self::KEY_ATTRIBUTE]) ? $attributes[self::KEY_ATTRIBUTE] : null;

if (!$key) {
$key = $handler . '_' . $interval . '_' . serialize($workload);
}

$schedulerDefinition->addMethodCall(
self::CREATE_FUNCTION_NAME,
[
$handler,
$workload,
$interval,
$key,
]
);
}
}
}
}
22 changes: 22 additions & 0 deletions src/Entity/Task.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ class Task
*/
private $uuid;

/**
* @var string
*/
private $key;

/**
* @var TaskInterface
*/
Expand Down Expand Up @@ -102,4 +107,21 @@ public function setCompleted($completed)
{
$this->completed = $completed;
}

/**
* @return string
*/
public function getKey()
{
return $this->key;
}

/**
* @param string $key
* @return $this
*/
public function setKey($key)
{
$this->key = $key;
}
}
6 changes: 6 additions & 0 deletions src/Resources/config/command.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@
<tag name="console.command"/>
</service>

<service id="task.command.run.handler" class="Task\TaskBundle\Command\RunHandlerCommand">
<argument type="service" id="task.handler_registry"/>

<tag name="console.command"/>
</service>

<service id="task.command.schedule_task" class="Task\TaskBundle\Command\ScheduleTaskCommand">
<argument type="service" id="task.scheduler"/>

Expand Down
1 change: 1 addition & 0 deletions src/Resources/config/doctrine/Task.orm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

<field name="task" type="object"/>
<field name="uuid" type="string" length="100"/>
<field name="key" type="string" length="255" nullable="true" column="task_key"/>
<field name="executionDate" type="datetime"/>
<field name="completed" type="boolean"/>

Expand Down
17 changes: 17 additions & 0 deletions src/Storage/DoctrineStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,22 @@ public function __construct(EntityManagerInterface $entityManager, TaskRepositor
public function store(TaskInterface $task)
{
$entity = new TaskEntity();

if ($task->getKey()) {
$oldEntity = $this->taskRepository->findOneBy(
[
'key' => $task->getKey(),
'completed' => false,
]
);

if ($oldEntity) {
// TODO update task (warning execution date should not be changed)

return;
}
}

$this->setTask($entity, $task);

$this->entityManager->persist($entity);
Expand Down Expand Up @@ -89,6 +105,7 @@ private function setTask(TaskEntity $entity, TaskInterface $task)
{
$entity->setTask($task);
$entity->setUuid($task->getUuid());
$entity->setKey($task->getKey());
$entity->setCompleted($task->isCompleted());
$entity->setExecutionDate($task->getExecutionDate());
}
Expand Down
2 changes: 2 additions & 0 deletions src/TaskBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Bundle\Bundle;
use Task\TaskBundle\DependencyInjection\HandlerCompilerPass;
use Task\TaskBundle\DependencyInjection\TaskCompilerPass;

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

$container->addCompilerPass(new HandlerCompilerPass());
$container->addCompilerPass(new TaskCompilerPass());
}
}
52 changes: 52 additions & 0 deletions tests/Unit/Command/RunHandlerCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace Unit\Command;

use Prophecy\Argument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Task\Handler\RegistryInterface;
use Task\TaskBundle\Command\RunHandlerCommand;

class RunHandlerCommandTest extends \PHPUnit_Framework_TestCase
{
public function testConfigure()
{
$scheduler = $this->prophesize(RegistryInterface::class);
$command = new RunHandlerCommand($scheduler->reveal());

$this->assertEquals('task:run:handler', $command->getName());
}

public function runProvider()
{
return [
['test-handler', 'test-workload'],
['test-handler-1', 'test-workload-1'],
];
}

/**
* @dataProvider runProvider
*/
public function testRun($handlerName, $workload)
{
$input = $this->prophesize(InputInterface::class);
$output = $this->prophesize(OutputInterface::class);

$input->bind(Argument::any())->willReturn(true);
$input->validate()->willReturn(true);
$input->isInteractive()->willReturn(false);
$input->hasArgument('command')->willReturn(false);

$input->getArgument('handler')->willReturn($handlerName);
$input->getArgument('workload')->willReturn($workload);

$registry = $this->prophesize(RegistryInterface::class);
$command = new RunHandlerCommand($registry->reveal());

$command->run($input->reveal(), $output->reveal());

$registry->run($handlerName, $workload)->shouldBeCalledTimes(1);
}
}
6 changes: 3 additions & 3 deletions tests/Unit/DependencyInjection/HandlerCompilerPassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ public function testProcess()
->willReturn(
[
'id-1' => [
['handler-name' => 'name-1'],
[HandlerCompilerPass::HANDLER_NAME_ATTRIBUTE => 'name-1'],
],
'id-2' => [
['handler-name' => 'name-2-1'],
['handler-name' => 'name-2-2'],
[HandlerCompilerPass::HANDLER_NAME_ATTRIBUTE => 'name-2-1'],
[HandlerCompilerPass::HANDLER_NAME_ATTRIBUTE => 'name-2-2'],
],
]
);
Expand Down
71 changes: 71 additions & 0 deletions tests/Unit/DependencyInjection/TaskCompilerPassTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

namespace Unit\DependencyInjection;

use Prophecy\Argument;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Task\TaskBundle\DependencyInjection\HandlerCompilerPass;
use Task\TaskBundle\DependencyInjection\TaskCompilerPass;

class TaskCompilerPassTest extends \PHPUnit_Framework_TestCase
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

functional tests missing for this class

{
public function testProcess()
{
$containerBuilder = $this->prophesize(ContainerBuilder::class);
$schedulerDefinition = $this->prophesize(Definition::class);
$handler1Definition = $this->prophesize(Definition::class);
$handler2Definition = $this->prophesize(Definition::class);

$containerBuilder->has(TaskCompilerPass::SCHEDULER_ID)->willReturn(true);
$containerBuilder->getDefinition(TaskCompilerPass::SCHEDULER_ID)->willReturn($schedulerDefinition->reveal());
$containerBuilder->getDefinition('id-1')->willReturn($handler1Definition->reveal());
$containerBuilder->getDefinition('id-2')->willReturn($handler2Definition->reveal());

$containerBuilder->findTaggedServiceIds(TaskCompilerPass::INTERVAL_TAG)
->willReturn(
[
'id-1' => [
[
TaskCompilerPass::INTERVAL_ATTRIBUTE => 'daily',
TaskCompilerPass::WORKLOAD_ATTRIBUTE => 'test-workload',
TaskCompilerPass::KEY_ATTRIBUTE => 'test-key'
],
],
'id-2' => [
[
TaskCompilerPass::INTERVAL_ATTRIBUTE => 'daily',
TaskCompilerPass::KEY_ATTRIBUTE => 'test-key-1'
],
[
TaskCompilerPass::INTERVAL_ATTRIBUTE => 'daily',
TaskCompilerPass::WORKLOAD_ATTRIBUTE => 'test-workload-2'
],
],
]
);

$handler1Definition->getTag(HandlerCompilerPass::HANDLER_TAG)->willReturn(
[[HandlerCompilerPass::HANDLER_NAME_ATTRIBUTE => 'handler-1']]
);
$handler2Definition->getTag(HandlerCompilerPass::HANDLER_TAG)->willReturn(
[[HandlerCompilerPass::HANDLER_NAME_ATTRIBUTE => 'handler-2']]
);

$compilerPass = new TaskCompilerPass();
$compilerPass->process($containerBuilder->reveal());

$schedulerDefinition->addMethodCall(
TaskCompilerPass::CREATE_FUNCTION_NAME,
['handler-1', 'test-workload', 'daily', 'test-key']
)->shouldBeCalledTimes(1);
$schedulerDefinition->addMethodCall(
TaskCompilerPass::CREATE_FUNCTION_NAME,
['handler-2', null, 'daily', 'test-key-1']
)->shouldBeCalledTimes(1);
$schedulerDefinition->addMethodCall(
TaskCompilerPass::CREATE_FUNCTION_NAME,
['handler-2', 'test-workload-2', 'daily', 'handler-2_daily_s:15:"test-workload-2";']
)->shouldBeCalledTimes(1);
}
}
Loading