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
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
48 changes: 40 additions & 8 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,43 @@
# CHANGELOG
# Change Log

* 0.2.1 (2016-03-30)
* HOTFIX #18 Fixed command issues (sorting and shortcut)
## [0.2.1](https://github.com/php-task/TaskBundle/tree/0.2.1) (2016-03-30)
[Full Changelog](https://github.com/php-task/TaskBundle/compare/0.2.0...0.2.1)

* 0.2.0 (2016-02-27)
* ENHANCEMENT #-- Added debug tasks command and extended storage
* ENHANCEMENT #-- Moved command name to service definition
* FEATURE #13 Added options to command schedule task for cron tasks
**Closed issues:**

* 0.1.0 (2016-01-31)
- Debug tasks should be sorted DESC for execution date [\#17](https://github.com/php-task/TaskBundle/issues/17)
- Schedule task command option e \(end-date\) bug [\#16](https://github.com/php-task/TaskBundle/issues/16)

**Merged pull requests:**

- Fixed command issues [\#18](https://github.com/php-task/TaskBundle/pull/18) ([wachterjohannes](https://github.com/wachterjohannes))

## [0.2.0](https://github.com/php-task/TaskBundle/tree/0.2.0) (2016-02-27)
[Full Changelog](https://github.com/php-task/TaskBundle/compare/0.1.0...0.2.0)

**Fixed bugs:**

- If handle return object output failed [\#7](https://github.com/php-task/TaskBundle/issues/7)
- Commands should only output something on error or in verbosity mode [\#6](https://github.com/php-task/TaskBundle/issues/6)

**Closed issues:**

- Move command names to service definition [\#4](https://github.com/php-task/TaskBundle/issues/4)

**Merged pull requests:**

- Added command and extended storage [\#14](https://github.com/php-task/TaskBundle/pull/14) ([wachterjohannes](https://github.com/wachterjohannes))
- Added options to command schedule task for cron tasks [\#13](https://github.com/php-task/TaskBundle/pull/13) ([wachterjohannes](https://github.com/wachterjohannes))

## [0.1.0](https://github.com/php-task/TaskBundle/tree/0.1.0) (2016-01-31)
**Fixed bugs:**

- Next Execution date should be in the future [\#12](https://github.com/php-task/TaskBundle/issues/12)

**Merged pull requests:**

- Add tagged service task compilerpass [\#2](https://github.com/php-task/TaskBundle/pull/2) ([wachterjohannes](https://github.com/wachterjohannes))



\* *This Change Log was automatically generated by [github_changelog_generator](https://github.com/skywinder/Github-Changelog-Generator)*
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
],
"require": {
"php": "~5.5 || ~7.0",
"php-task/php-task": "0.2.*",
"php-task/php-task": "dev-master",
"symfony/http-kernel": "^2.6",
"symfony/dependency-injection": "^2.6",
"symfony/config": "^2.6",
Expand Down
55 changes: 26 additions & 29 deletions src/Command/DebugTasksCommand.php
Original file line number Diff line number Diff line change
@@ -1,27 +1,38 @@
<?php

/*
* This file is part of php-task library.
*
* (c) php-task
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Task\TaskBundle\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\Table;
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.
*
* @author @wachterjohannes <johannes.wachter@massiveart.com>
*/
class DebugTasksCommand extends Command
{
/**
* @var StorageInterface
* @var TaskExecutionRepositoryInterface
*/
private $storage;

public function __construct($name, StorageInterface $storage)
/**
* @param string $name
* @param TaskExecutionRepositoryInterface $storage
*/
public function __construct($name, TaskExecutionRepositoryInterface $storage)
{
parent::__construct($name);

Expand All @@ -34,44 +45,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
30 changes: 25 additions & 5 deletions src/Command/RunCommand.php
Original file line number Diff line number Diff line change
@@ -1,28 +1,47 @@
<?php

/*
* This file is part of php-task library.
*
* (c) php-task
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Task\TaskBundle\Command;

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.
*
* @author @wachterjohannes <johannes.wachter@massiveart.com>
*/
class RunCommand extends Command
{
/**
* @var TaskRunnerInterface
*/
private $runner;

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

public function __construct($name, SchedulerInterface $scheduler)
/**
* @param string $name
* @param TaskRunnerInterface $runner
* @param SchedulerInterface $scheduler
*/
public function __construct($name, TaskRunnerInterface $runner, SchedulerInterface $scheduler)
{
parent::__construct($name);

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

Expand All @@ -39,6 +58,7 @@ protected function configure()
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->scheduler->run();
$this->runner->runTasks();
$this->scheduler->scheduleTasks();
}
}
33 changes: 22 additions & 11 deletions src/Command/RunHandlerCommand.php
Original file line number Diff line number Diff line change
@@ -1,30 +1,41 @@
<?php

/*
* This file is part of php-task library.
*
* (c) php-task
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

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\Handler\TaskHandlerFactoryInterface;

/**
* Run pending tasks.
*
* @author Alexander Schranz <alexander.schranz@massiveart.com>
*/
class RunHandlerCommand extends Command
{
/**
* @var RegistryInterface
* @var TaskHandlerFactoryInterface
*/
private $registry;
private $handlerFactory;

public function __construct($name, RegistryInterface $registry)
/**
* @param string $name
* @param TaskHandlerFactoryInterface $handlerFactory
*/
public function __construct($name, TaskHandlerFactoryInterface $handlerFactory)
{
parent::__construct($name);

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

/**
Expand All @@ -33,7 +44,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 +53,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
31 changes: 18 additions & 13 deletions src/Command/ScheduleTaskCommand.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
<?php

/*
* This file is part of php-task library.
*
* (c) php-task
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

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\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Task\SchedulerInterface;
use Task\Scheduler\SchedulerInterface;

/**
* Schedule task.
*
* @author @wachterjohannes <johannes.wachter@massiveart.com>
*/
class ScheduleTaskCommand extends Command
{
Expand All @@ -21,11 +28,15 @@ class ScheduleTaskCommand extends Command
*/
private $scheduler;

public function __construct($name, SchedulerInterface $scheduler)
/**
* @param string $name
* @param SchedulerInterface $runner
*/
public function __construct($name, SchedulerInterface $runner)
{
parent::__construct($name);

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

/**
Expand All @@ -38,8 +49,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 +61,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 +77,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());
}
}
11 changes: 10 additions & 1 deletion src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
<?php

/*
* This file is part of php-task library.
*
* (c) php-task
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Task\TaskBundle\DependencyInjection;

use Symfony\Component\Config\Definition\Builder\TreeBuilder;
Expand All @@ -19,7 +28,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
Loading