|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of php-task library. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) php-task |
|
7
|
|
|
* |
|
8
|
|
|
* This source file is subject to the MIT license that is bundled |
|
9
|
|
|
* with this source code in the file LICENSE. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Task\TaskBundle\Command; |
|
13
|
|
|
|
|
14
|
|
|
use Symfony\Component\Console\Command\Command; |
|
15
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
16
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
17
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
18
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
19
|
|
|
use Task\Scheduler\TaskSchedulerInterface; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Schedule task. |
|
23
|
|
|
*/ |
|
24
|
|
|
class ScheduleTaskCommand extends Command |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* @var TaskSchedulerInterface |
|
28
|
|
|
*/ |
|
29
|
|
|
private $scheduler; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @param string $name |
|
33
|
|
|
* @param TaskSchedulerInterface $runner |
|
34
|
|
|
*/ |
|
35
|
|
|
public function __construct($name, TaskSchedulerInterface $runner) |
|
36
|
|
|
{ |
|
37
|
|
|
parent::__construct($name); |
|
38
|
|
|
|
|
39
|
|
|
$this->scheduler = $runner; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* {@inheritdoc} |
|
44
|
|
|
*/ |
|
45
|
|
|
protected function configure() |
|
46
|
|
|
{ |
|
47
|
|
|
$this |
|
48
|
|
|
->setDescription('Schedule task') |
|
49
|
|
|
->setHelp(<<<'EOT' |
|
50
|
|
|
The <info>%command.name%</info> command schedules given handler. |
|
51
|
|
|
|
|
52
|
|
|
$ %command.full_name% AppBundle\\Handler\\ImageHandler |
|
53
|
|
|
|
|
54
|
|
|
Execute without any arguments in order to see schedule a single task, use the |
|
55
|
|
|
<comment>--workload</comment> option in order to specify a workload or |
|
56
|
|
|
<comment>--cron-expression</comment> to create a recurring task. |
|
57
|
|
|
EOT |
|
58
|
|
|
) |
|
59
|
|
|
->addArgument('handlerClass', InputArgument::REQUIRED, 'Handler which will be called') |
|
60
|
|
|
->addArgument('workload', InputArgument::OPTIONAL, 'This will be passed to the handler') |
|
61
|
|
|
->addOption('cron-expression', 'c', InputOption::VALUE_REQUIRED, 'Specifies interval for recurring task') |
|
62
|
|
|
->addOption('execution-date', null, InputOption::VALUE_REQUIRED, 'Specifies execution-date for single task') |
|
63
|
|
|
->addOption('end-date', null, InputOption::VALUE_REQUIRED, 'Specifies last run date for recurring tasks'); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* {@inheritdoc} |
|
68
|
|
|
*/ |
|
69
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
70
|
|
|
{ |
|
71
|
|
|
$handlerClass = $input->getArgument('handlerClass'); |
|
72
|
|
|
$workload = $input->getArgument('workload'); |
|
73
|
|
|
$cronExpression = $input->getOption('cron-expression'); |
|
74
|
|
|
$executionDateString = $input->getOption('execution-date'); |
|
75
|
|
|
$endDateString = $input->getOption('end-date'); |
|
76
|
|
|
|
|
77
|
|
|
if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) { |
|
78
|
|
|
$output->writeln(sprintf('Schedule task "%s" with workload "%s"', $handlerClass, $workload)); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
$taskBuilder = $this->scheduler->createTask($handlerClass, $workload); |
|
|
|
|
|
|
82
|
|
|
|
|
83
|
|
|
if (null !== $cronExpression) { |
|
84
|
|
|
$endDate = null; |
|
85
|
|
|
if (null !== $endDateString) { |
|
86
|
|
|
$endDate = new \DateTime($endDateString); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
$taskBuilder->cron($cronExpression, new \DateTime(), $endDate); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
if (null !== $executionDateString) { |
|
93
|
|
|
$taskBuilder->executeAt(new \DateTime($executionDateString)); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
$taskBuilder->schedule(); |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.