Skip to content

Commit 813ef8f

Browse files
fixed #7 and fixed #6 additionally added options to schedule task command
1 parent 0c7a1ae commit 813ef8f

File tree

6 files changed

+82
-11
lines changed

6 files changed

+82
-11
lines changed

.styleci.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
preset: symfony
2+
3+
enabled:
4+
- concat_with_spaces
5+
- ordered_use
6+
- short_array_syntax
7+
8+
disabled:
9+
- concat_without_spaces
10+
- phpdoc_align
11+
- phpdoc_indent
12+
- phpdoc_to_comment
13+
- blankline_after_open_tag

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# CHANGELOG
22

33
* dev-master
4-
* ENHANCEMENT #- Moved command name to service definition
4+
* ENHANCEMENT #-- Moved command name to service definition
5+
* FEATURE #13 Added options to command schedule task for cron tasks

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
],
1212
"require": {
1313
"php": "~5.5 || ~7.0",
14-
"php-task/php-task": "0.1.*",
14+
"php-task/php-task": "dev-master",
1515
"symfony/http-kernel": "^2.6",
1616
"symfony/dependency-injection": "^2.6",
1717
"symfony/config": "^2.6",

src/Command/RunHandlerCommand.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use Symfony\Component\Console\Input\InputInterface;
88
use Symfony\Component\Console\Output\OutputInterface;
99
use Task\Handler\RegistryInterface;
10-
use Task\SchedulerInterface;
1110

1211
/**
1312
* Run pending tasks.
@@ -46,10 +45,14 @@ protected function execute(InputInterface $input, OutputInterface $output)
4645
$handler = $input->getArgument('handler');
4746
$workload = $input->getArgument('workload');
4847

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

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

53-
$output->writeln(sprintf('Result: "%s"', $result));
54+
if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) {
55+
$output->writeln(sprintf('Result: %s', json_encode($result)));
56+
}
5457
}
5558
}

src/Command/ScheduleTaskCommand.php

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Symfony\Component\Console\Command\Command;
66
use Symfony\Component\Console\Input\InputArgument;
77
use Symfony\Component\Console\Input\InputInterface;
8+
use Symfony\Component\Console\Input\InputOption;
89
use Symfony\Component\Console\Output\OutputInterface;
910
use Task\SchedulerInterface;
1011

@@ -35,16 +36,42 @@ protected function configure()
3536
$this
3637
->setDescription('Run pending tasks')
3738
->addArgument('handler', InputArgument::REQUIRED)
38-
->addArgument('workload', InputArgument::OPTIONAL);
39+
->addArgument('workload', InputArgument::OPTIONAL)
40+
->addOption('cron-expression', 'c', InputOption::VALUE_REQUIRED)
41+
->addOption('end-date', 'e', InputOption::VALUE_REQUIRED)
42+
->addOption('key', 'k', InputOption::VALUE_REQUIRED);
3943
}
4044

4145
/**
4246
* {@inheritdoc}
4347
*/
4448
protected function execute(InputInterface $input, OutputInterface $output)
4549
{
46-
$this->scheduler
47-
->createTask($input->getArgument('handler'), $input->getArgument('workload'))
48-
->schedule();
50+
$handler = $input->getArgument('handler');
51+
$workload = $input->getArgument('workload');
52+
$cronExpression = $input->getOption('cron-expression');
53+
$endDateString = $input->getOption('end-date');
54+
$key = $input->getOption('key');
55+
56+
if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) {
57+
$output->writeln(sprintf('Schedule task "%s" with workload "%s"', $handler, $workload));
58+
}
59+
60+
$taskBuilder = $this->scheduler->createTask($input->getArgument('handler'), $input->getArgument('workload'));
61+
62+
if ($cronExpression !== null) {
63+
$endDate = null;
64+
if ($endDateString !== null) {
65+
$endDate = new \DateTime($endDateString);
66+
}
67+
68+
$taskBuilder->cron($cronExpression, new \DateTime(), $endDate);
69+
}
70+
71+
if ($key !== null) {
72+
$taskBuilder->setKey($key);
73+
}
74+
75+
$taskBuilder->schedule();
4976
}
5077
}

tests/Unit/Command/ScheduleTaskCommandTest.php

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,22 @@ public function testConfigure()
2424
public function runProvider()
2525
{
2626
return [
27+
['test-handler'],
2728
['test-handler', 'test-workload'],
2829
['test-handler-1', 'test-workload-1'],
30+
['test-handler', 'test-workload', '1 * * * *'],
31+
['test-handler', 'test-workload', '1 * * * *', '+1 week'],
32+
['test-handler', 'test-workload', '1 * * * *', '+1 week', 'test-key'],
33+
['test-handler', 'test-workload', '1 * * * *', null, 'test-key'],
34+
['test-handler', 'test-workload', null, null, 'test-key'],
35+
['test-handler', 'test-workload', null, '+1 week', 'test-key'],
2936
];
3037
}
3138

3239
/**
3340
* @dataProvider runProvider
3441
*/
35-
public function testRun($handler, $workload)
42+
public function testRun($handler, $workload = null, $cronExpression = null, $endDateString = null, $key = null)
3643
{
3744
$taskBuilder = $this->prophesize(TaskBuilderInterface::class);
3845

@@ -46,14 +53,34 @@ public function testRun($handler, $workload)
4653

4754
$input->getArgument('handler')->willReturn($handler);
4855
$input->getArgument('workload')->willReturn($workload);
56+
$input->getOption('cron-expression')->willReturn($cronExpression);
57+
$input->getOption('end-date')->willReturn($endDateString);
58+
$input->getOption('key')->willReturn($key);
4959

5060
$scheduler = $this->prophesize(SchedulerInterface::class);
5161
$command = new ScheduleTaskCommand('task:schedule:task', $scheduler->reveal());
5262

5363
$scheduler->createTask($handler, $workload)->shouldBeCalledTimes(1)->willReturn($taskBuilder->reveal());
5464

55-
$command->run($input->reveal(), $output->reveal());
65+
if ($key !== null) {
66+
$taskBuilder->setKey($key)->shouldBeCalled();
67+
} else {
68+
$taskBuilder->setKey(Argument::any())->shouldNotBeCalled();
69+
}
70+
if ($cronExpression !== null) {
71+
$endDate = null;
72+
if ($endDateString !== null) {
73+
$endDate = new \DateTime($endDateString);
74+
}
5675

76+
$taskBuilder->cron($cronExpression, Argument::type(\DateTime::class), $endDate)->shouldBeCalled()
77+
->willReturn($taskBuilder->reveal());
78+
} else {
79+
$taskBuilder->cron(Argument::any(), Argument::any(), Argument::any())->shouldNotBeCalled()
80+
->willReturn($taskBuilder->reveal());
81+
}
5782
$taskBuilder->schedule()->shouldBeCalledTimes(1);
83+
84+
$command->run($input->reveal(), $output->reveal());
5885
}
5986
}

0 commit comments

Comments
 (0)