|
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('Run pending tasks') |
|
49
|
|
|
->addArgument('handler', InputArgument::REQUIRED) |
|
50
|
|
|
->addArgument('workload', InputArgument::OPTIONAL) |
|
51
|
|
|
->addOption('cron-expression', 'c', InputOption::VALUE_REQUIRED) |
|
52
|
|
|
->addOption('end-date', null, InputOption::VALUE_REQUIRED); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* {@inheritdoc} |
|
57
|
|
|
*/ |
|
58
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
59
|
|
|
{ |
|
60
|
|
|
$handler = $input->getArgument('handler'); |
|
61
|
|
|
$workload = $input->getArgument('workload'); |
|
62
|
|
|
$cronExpression = $input->getOption('cron-expression'); |
|
63
|
|
|
$endDateString = $input->getOption('end-date'); |
|
64
|
|
|
|
|
65
|
|
|
if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) { |
|
66
|
|
|
$output->writeln(sprintf('Schedule task "%s" with workload "%s"', $handler, $workload)); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
$taskBuilder = $this->scheduler->createTask($input->getArgument('handler'), $input->getArgument('workload')); |
|
70
|
|
|
|
|
71
|
|
|
if ($cronExpression !== null) { |
|
72
|
|
|
$endDate = null; |
|
73
|
|
|
if ($endDateString !== null) { |
|
74
|
|
|
$endDate = new \DateTime($endDateString); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
$taskBuilder->cron($cronExpression, new \DateTime(), $endDate); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
$this->scheduler->addTask($taskBuilder->getTask()); |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|