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
|
12 |
|
public function __construct($name, TaskSchedulerInterface $runner) |
36
|
|
|
{ |
37
|
12 |
|
parent::__construct($name); |
38
|
|
|
|
39
|
12 |
|
$this->scheduler = $runner; |
40
|
12 |
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* {@inheritdoc} |
44
|
|
|
*/ |
45
|
12 |
|
protected function configure() |
46
|
|
|
{ |
47
|
12 |
|
$this |
48
|
12 |
|
->setDescription('Run pending tasks') |
49
|
12 |
|
->addArgument('handlerClass', InputArgument::REQUIRED) |
50
|
12 |
|
->addArgument('workload', InputArgument::OPTIONAL) |
51
|
12 |
|
->addOption('cron-expression', 'c', InputOption::VALUE_REQUIRED) |
52
|
12 |
|
->addOption('execution-date', null, InputOption::VALUE_REQUIRED) |
53
|
12 |
|
->addOption('end-date', null, InputOption::VALUE_REQUIRED); |
54
|
12 |
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* {@inheritdoc} |
58
|
|
|
*/ |
59
|
5 |
|
protected function execute(InputInterface $input, OutputInterface $output) |
60
|
|
|
{ |
61
|
5 |
|
$handlerClass = $input->getArgument('handlerClass'); |
62
|
5 |
|
$workload = $input->getArgument('workload'); |
63
|
5 |
|
$cronExpression = $input->getOption('cron-expression'); |
64
|
5 |
|
$executionDateString = $input->getOption('execution-date'); |
65
|
5 |
|
$endDateString = $input->getOption('end-date'); |
66
|
|
|
|
67
|
5 |
|
if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) { |
68
|
|
|
$output->writeln(sprintf('Schedule task "%s" with workload "%s"', $handlerClass, $workload)); |
69
|
|
|
} |
70
|
|
|
|
71
|
5 |
|
$taskBuilder = $this->scheduler->createTask($handlerClass, $workload); |
72
|
|
|
|
73
|
5 |
|
if ($cronExpression !== null) { |
74
|
2 |
|
$endDate = null; |
75
|
2 |
|
if ($endDateString !== null) { |
76
|
1 |
|
$endDate = new \DateTime($endDateString); |
77
|
1 |
|
} |
78
|
|
|
|
79
|
2 |
|
$taskBuilder->cron($cronExpression, new \DateTime(), $endDate); |
80
|
2 |
|
} |
81
|
|
|
|
82
|
5 |
|
if ($executionDateString !== null) { |
83
|
1 |
|
$taskBuilder->executeAt(new \DateTime($executionDateString)); |
84
|
1 |
|
} |
85
|
|
|
|
86
|
5 |
|
$taskBuilder->schedule(); |
87
|
5 |
|
} |
88
|
|
|
} |
89
|
|
|
|