|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the Commander project. |
|
4
|
|
|
* |
|
5
|
|
|
* @author Daniel Schröder <[email protected]> |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace GravityMedia\Commander\Console\Command; |
|
9
|
|
|
|
|
10
|
|
|
use GravityMedia\Commander\Commander; |
|
11
|
|
|
use GravityMedia\Commander\Entity\TaskEntity; |
|
12
|
|
|
use GravityMedia\Commander\TaskManager; |
|
13
|
|
|
use Symfony\Component\Console\Exception\RuntimeException; |
|
14
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
15
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
16
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
17
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* New command class. |
|
21
|
|
|
* |
|
22
|
|
|
* @package GravityMedia\Commander\Console\Command |
|
23
|
|
|
*/ |
|
24
|
|
|
class NewCommand extends Command |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* {@inheritdoc} |
|
28
|
|
|
*/ |
|
29
|
|
|
protected function configure() |
|
30
|
|
|
{ |
|
31
|
|
|
parent::configure(); |
|
32
|
|
|
|
|
33
|
|
|
$this |
|
34
|
|
|
->setName('task:new') |
|
35
|
|
|
->setDescription('New task') |
|
36
|
|
|
->addArgument( |
|
37
|
|
|
'script', |
|
38
|
|
|
InputArgument::REQUIRED, |
|
39
|
|
|
'The script to run with the task' |
|
40
|
|
|
) |
|
41
|
|
|
->addOption( |
|
42
|
|
|
'priority', |
|
43
|
|
|
null, |
|
44
|
|
|
InputOption::VALUE_REQUIRED, |
|
45
|
|
|
'The task priority', |
|
46
|
|
|
TaskEntity::DEFAULT_PRIORITY |
|
47
|
|
|
); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* {@inheritdoc} |
|
52
|
|
|
*/ |
|
53
|
|
|
protected function initialize(InputInterface $input, OutputInterface $output) |
|
54
|
|
|
{ |
|
55
|
|
|
parent::initialize($input, $output); |
|
56
|
|
|
|
|
57
|
|
|
if (null === $this->filterPriority($input)) { |
|
58
|
|
|
throw new RuntimeException('Value of option "priority" must be an integer'); |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* {@inheritdoc} |
|
64
|
|
|
*/ |
|
65
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
66
|
|
|
{ |
|
67
|
|
|
$config = $this->getCommanderConfig(); |
|
68
|
|
|
$commander = new Commander($config); |
|
69
|
|
|
if (!$commander->isSchemaValid()) { |
|
70
|
|
|
$commander->updateSchema(); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
$entityManager = $commander->getEntityManager(); |
|
74
|
|
|
$taskManager = new TaskManager($entityManager); |
|
75
|
|
|
|
|
76
|
|
|
$script = $input->getArgument('script'); |
|
77
|
|
|
$task = $taskManager->findTask(['script' => $script, 'pid' => null]); |
|
78
|
|
|
|
|
79
|
|
|
$priority = $this->filterPriority($input); |
|
80
|
|
|
if (null === $task) { |
|
81
|
|
|
$taskManager->newTask($script, $priority); |
|
82
|
|
|
return; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
if ($priority !== $task->getEntity()->getPriority()) { |
|
86
|
|
|
$task->updatePriority($priority); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Filter priority value. |
|
92
|
|
|
* |
|
93
|
|
|
* @param InputInterface $input |
|
94
|
|
|
* |
|
95
|
|
|
* @return null|int |
|
96
|
|
|
*/ |
|
97
|
|
|
protected function filterPriority(InputInterface $input) |
|
98
|
|
|
{ |
|
99
|
|
|
$priority = filter_var($input->getOption('priority'), FILTER_VALIDATE_INT); |
|
100
|
|
|
if (false === $priority) { |
|
101
|
|
|
return null; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
return $priority; |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|