1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace JobQueue\Application\Console; |
4
|
|
|
|
5
|
|
|
use JobQueue\Application\Utils\CommandTrait; |
6
|
|
|
use JobQueue\Domain\Task\Profile; |
7
|
|
|
use JobQueue\Domain\Task\Task; |
8
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
9
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
10
|
|
|
use Symfony\Component\Console\Input\InputOption; |
11
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
12
|
|
|
|
13
|
|
|
final class AddTask extends ManagerCommand |
14
|
|
|
{ |
15
|
|
|
use CommandTrait; |
16
|
|
|
|
17
|
|
|
public function configure() |
18
|
|
|
{ |
19
|
|
|
$this |
20
|
|
|
->setName('add') |
21
|
|
|
->setDescription('Add a new task to the queue') |
22
|
|
|
->addArgument('profile', InputArgument::REQUIRED, 'Profile name') |
23
|
|
|
->addArgument('job', InputArgument::REQUIRED, 'Job class name') |
24
|
|
|
->addArgument('parameters', InputArgument::IS_ARRAY, 'List of parameters (key:value)', []) |
25
|
|
|
->addOption('tags', 't', InputOption::VALUE_IS_ARRAY|InputOption::VALUE_OPTIONAL, 'Add one or multiple (array) tags') |
26
|
|
|
; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* |
31
|
|
|
* @param InputInterface $input |
32
|
|
|
* @param OutputInterface $output |
33
|
|
|
* @return int |
34
|
|
|
*/ |
35
|
1 |
|
protected function execute(InputInterface $input, OutputInterface $output): int |
36
|
|
|
{ |
37
|
1 |
|
$jobName = $input->getArgument('job'); |
38
|
|
|
|
39
|
1 |
|
$parameters = []; |
40
|
1 |
|
foreach ($input->getArgument('parameters') as $parameter) { |
41
|
1 |
|
list($name, $value) = explode(':', $parameter); |
42
|
1 |
|
$parameters[trim($name)] = trim($value); |
43
|
|
|
} |
44
|
|
|
|
45
|
1 |
|
$tags = []; |
46
|
1 |
|
foreach ($input->getOption('tags') as $tag) { |
47
|
1 |
|
$tags[] = trim($tag); |
48
|
|
|
} |
49
|
|
|
|
50
|
1 |
|
$task = new Task( |
51
|
1 |
|
new Profile($input->getArgument('profile')), |
52
|
1 |
|
new $jobName, |
53
|
1 |
|
$parameters, $tags |
54
|
|
|
); |
55
|
|
|
|
56
|
1 |
|
$this->queue->add($task); |
57
|
|
|
|
58
|
1 |
|
$this->formatTaskBlock($task, $output); |
59
|
|
|
|
60
|
1 |
|
return 0; |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|