-
Notifications
You must be signed in to change notification settings - Fork 8
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
wachterjohannes
merged 6 commits into
master
from
alexander-schranz-feature/service-tasks
Jan 13, 2016
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
1c4c8dc
add tagged service task compilerpass
alexander-schranz e496fe9
fixed testcases
wachterjohannes 7c9ec72
added testcases for new feature
wachterjohannes 1fbb7ec
add runhandler
alexander-schranz 4ff1233
added testcases for run handler command and fixed code-style
wachterjohannes 68f68b9
removed wrong todo
wachterjohannes File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
/** | ||
* @var RegistryInterface | ||
*/ | ||
private $registry; | ||
|
||
public function __construct(RegistryInterface $registry) | ||
{ | ||
parent::__construct('task:run:handler'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
] | ||
); | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tests missing