Skip to content

Improved architecture of bundle #15

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 12 commits into from
Aug 30, 2016
Merged
40 changes: 13 additions & 27 deletions src/Command/DebugTasksCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Task\Storage\StorageInterface;
use Task\Execution\TaskExecutionRepositoryInterface;

/**
* Run pending tasks.
Expand All @@ -17,11 +17,11 @@
class DebugTasksCommand extends Command
{
/**
* @var StorageInterface
* @var TaskExecutionRepositoryInterface
*/
private $storage;

public function __construct($name, StorageInterface $storage)
public function __construct($name, TaskExecutionRepositoryInterface $storage)
{
parent::__construct($name);

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

/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$key = $input->getOption('key');
$limit = $input->getOption('limit');

if (null !== $key) {
$tasks = $this->storage->findByKey($key, $limit, 'DESC');
} else {
$tasks = $this->storage->findAll($limit, 'DESC');
}
$executions = $this->storage->findAll($limit);

$table = new Table($output);
$table->setHeaders(['uuid', 'key', 'task-name', 'execution-date', 'completed', 'start', 'duration']);

foreach ($tasks as $task) {
$start = null;
$duration = null;
if ($task->getLastExecution()) {
$start = $task->getLastExecution()->getFinishedAtAsDateTime()->format(\DateTime::RFC3339);
$duration = $task->getLastExecution()->getExecutionDuration();
}
$table->setHeaders(['uuid', 'status', 'handler', 'schedule time', 'end time', 'duration']);

foreach ($executions as $execution) {
$table->addRow(
[
$task->getUuid(),
$task->getKey(),
$task->getTaskName(),
$task->getExecutionDate()->format(\DateTime::RFC3339),
$task->isCompleted(),
$start,
$duration,
$execution->getUuid(),
$execution->getStatus(),
$execution->getHandlerClass(),
$execution->getScheduleTime()->format(\DateTime::RFC3339),
!$execution->getEndTime() ? '' : $execution->getEndTime()->format(\DateTime::RFC3339),
(round($execution->getDuration(), 6) * 1000000) . 'ms',
]
);
}
Expand Down
14 changes: 11 additions & 3 deletions src/Command/RunCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Task\SchedulerInterface;
use Task\Runner\TaskRunnerInterface;
use Task\Scheduler\SchedulerInterface;

/**
* Run pending tasks.
Expand All @@ -14,15 +15,21 @@
*/
class RunCommand extends Command
{
/**
* @var TaskRunnerInterface
*/
private $runner;

/**
* @var SchedulerInterface
*/
private $scheduler;

public function __construct($name, SchedulerInterface $scheduler)
public function __construct($name, TaskRunnerInterface $runner, SchedulerInterface $scheduler)
{
parent::__construct($name);

$this->runner = $runner;
$this->scheduler = $scheduler;
}

Expand All @@ -39,6 +46,7 @@ protected function configure()
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->scheduler->run();
$this->runner->runTasks();
$this->scheduler->scheduleTasks();
}
}
18 changes: 9 additions & 9 deletions src/Command/RunHandlerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Task\Handler\RegistryInterface;
use Task\Handler\TaskHandlerFactoryInterface;

/**
* Run pending tasks.
Expand All @@ -16,15 +16,15 @@
class RunHandlerCommand extends Command
{
/**
* @var RegistryInterface
* @var TaskHandlerFactoryInterface
*/
private $registry;
private $handlerFactory;

public function __construct($name, RegistryInterface $registry)
public function __construct($name, TaskHandlerFactoryInterface $handlerFactory)
{
parent::__construct($name);

$this->registry = $registry;
$this->handlerFactory = $handlerFactory;
}

/**
Expand All @@ -33,7 +33,7 @@ public function __construct($name, RegistryInterface $registry)
protected function configure()
{
$this->setDescription('Run pending tasks')
->addArgument('handler', InputArgument::REQUIRED)
->addArgument('handlerClass', InputArgument::REQUIRED)
->addArgument('workload', InputArgument::OPTIONAL);
}

Expand All @@ -42,14 +42,14 @@ protected function configure()
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$handler = $input->getArgument('handler');
$handlerClass = $input->getArgument('handlerClass');
$workload = $input->getArgument('workload');

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

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

if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) {
$output->writeln(sprintf('Result: %s', json_encode($result)));
Expand Down
16 changes: 5 additions & 11 deletions src/Command/ScheduleTaskCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Task\SchedulerInterface;
use Task\Scheduler\SchedulerInterface;

/**
* Schedule task.
Expand All @@ -21,11 +21,11 @@ class ScheduleTaskCommand extends Command
*/
private $scheduler;

public function __construct($name, SchedulerInterface $scheduler)
public function __construct($name, SchedulerInterface $runner)
{
parent::__construct($name);

$this->scheduler = $scheduler;
$this->scheduler = $runner;
}

/**
Expand All @@ -38,8 +38,7 @@ protected function configure()
->addArgument('handler', InputArgument::REQUIRED)
->addArgument('workload', InputArgument::OPTIONAL)
->addOption('cron-expression', 'c', InputOption::VALUE_REQUIRED)
->addOption('end-date', null, InputOption::VALUE_REQUIRED)
->addOption('key', 'k', InputOption::VALUE_REQUIRED);
->addOption('end-date', null, InputOption::VALUE_REQUIRED);
}

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

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

if ($key !== null) {
$taskBuilder->setKey($key);
}

$taskBuilder->schedule();
$this->scheduler->addTask($taskBuilder->getTask());
}
}
2 changes: 1 addition & 1 deletion src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function getConfigTreeBuilder()

$treeBuilder->root('task')
->children()
->enumNode('storage')->values(['array', 'doctrine'])->defaultValue('array')->end()
->enumNode('storage')->values(['array', 'doctrine'])->defaultValue('doctrine')->end()
->arrayNode('run')
->addDefaultsIfNotSet()
->children()
Expand Down
17 changes: 7 additions & 10 deletions src/DependencyInjection/HandlerCompilerPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@
*/
class HandlerCompilerPass implements CompilerPassInterface
{
const REGISTRY_ID = 'task.handler_registry';
const REGISTRY_ID = 'task.handler.factory';
const HANDLER_TAG = 'task.handler';
const ADD_FUNCTION_NAME = 'add';
const HANDLER_NAME_ATTRIBUTE = 'handler-name';

/**
Expand All @@ -27,16 +26,14 @@ public function process(ContainerBuilder $container)
return;
}

$definition = $container->findDefinition(self::REGISTRY_ID);

$handler = [];
$taggedServices = $container->findTaggedServiceIds(self::HANDLER_TAG);
foreach ($taggedServices as $id => $tags) {
foreach ($tags as $attributes) {
$definition->addMethodCall(
self::ADD_FUNCTION_NAME,
[$attributes[self::HANDLER_NAME_ATTRIBUTE], new Reference($id)]
);
}
$service = $container->getDefinition($id);
$handler[$service->getClass()] = new Reference($id);
}

$definition = $container->findDefinition(self::REGISTRY_ID);
$definition->replaceArgument(0, $handler);
}
}
63 changes: 0 additions & 63 deletions src/DependencyInjection/TaskCompilerPass.php

This file was deleted.

Loading