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\Commander; |
9
|
|
|
|
10
|
|
|
use Monolog\Logger; |
11
|
|
|
use Psr\Log\LoggerInterface; |
12
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
13
|
|
|
use Symfony\Component\Process\Process; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Task runner class. |
17
|
|
|
* |
18
|
|
|
* @package GravityMedia\Commander\Commander |
19
|
|
|
*/ |
20
|
|
|
class TaskRunner |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* The task manager. |
24
|
|
|
* |
25
|
|
|
* @var TaskManager |
26
|
|
|
*/ |
27
|
|
|
protected $taskManager; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* The output. |
31
|
|
|
* |
32
|
|
|
* @var OutputInterface |
33
|
|
|
*/ |
34
|
|
|
protected $output; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* The logger. |
38
|
|
|
* |
39
|
|
|
* @var LoggerInterface |
40
|
|
|
*/ |
41
|
|
|
protected $logger; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Create task runner object. |
45
|
|
|
* |
46
|
|
|
* @param TaskManager $taskManager |
47
|
|
|
* @param OutputInterface $output |
48
|
|
|
* @param LoggerInterface $logger |
49
|
|
|
*/ |
50
|
2 |
|
public function __construct(TaskManager $taskManager, OutputInterface $output, LoggerInterface $logger) |
51
|
|
|
{ |
52
|
2 |
|
$this->taskManager = $taskManager; |
53
|
2 |
|
$this->output = $output; |
54
|
2 |
|
$this->logger = $logger; |
55
|
2 |
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Gets called when the object is used as a callback. |
59
|
|
|
* |
60
|
|
|
* @param string $type |
61
|
|
|
* @param string $buffer |
62
|
|
|
* |
63
|
|
|
* @return void |
64
|
|
|
*/ |
65
|
1 |
|
public function __invoke($type, $buffer) |
66
|
|
|
{ |
67
|
1 |
|
$this->logMessage($type, $buffer); |
68
|
1 |
|
$this->renderOutput($type, $buffer); |
69
|
1 |
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Log message. |
73
|
|
|
* |
74
|
|
|
* @param string $type |
75
|
|
|
* @param string $message |
76
|
|
|
* |
77
|
|
|
* @return void |
78
|
|
|
*/ |
79
|
1 |
|
protected function logMessage($type, $message) |
80
|
|
|
{ |
81
|
1 |
|
$logLevel = Logger::INFO; |
82
|
1 |
|
if (Process::ERR === $type) { |
83
|
1 |
|
$logLevel = Logger::ERROR; |
84
|
1 |
|
} |
85
|
|
|
|
86
|
1 |
|
$this->logger->log($logLevel, $message); |
87
|
1 |
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Render output. |
91
|
|
|
* |
92
|
|
|
* @param string $type |
93
|
|
|
* @param string $message |
94
|
|
|
* |
95
|
|
|
* @return void |
96
|
|
|
*/ |
97
|
1 |
|
protected function renderOutput($type, $message) |
98
|
|
|
{ |
99
|
1 |
|
if ($this->output->isQuiet()) { |
100
|
|
|
return; |
101
|
|
|
} |
102
|
|
|
|
103
|
1 |
|
if (Process::ERR === $type) { |
104
|
1 |
|
$message = sprintf('<error>%s</error>', $message); |
105
|
1 |
|
} |
106
|
|
|
|
107
|
1 |
|
$this->output->writeln($message); |
108
|
1 |
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Run all tasks from the task manager. |
112
|
|
|
* |
113
|
|
|
* @param int $timeout |
114
|
|
|
* |
115
|
|
|
* @return void |
116
|
|
|
*/ |
117
|
2 |
|
public function runAll($timeout) |
118
|
|
|
{ |
119
|
2 |
|
while (null !== $task = $this->taskManager->findNextTask()) { |
120
|
2 |
|
$this->run($task, $timeout); |
121
|
1 |
|
} |
122
|
2 |
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Run single task. |
126
|
|
|
* |
127
|
|
|
* @param Task $task |
128
|
|
|
* @param int $timeout |
129
|
|
|
* |
130
|
|
|
* @return void |
131
|
|
|
*/ |
132
|
2 |
|
public function run($task, $timeout) |
133
|
|
|
{ |
134
|
2 |
|
$process = new Process($task->getEntity()->getCommandline()); |
135
|
2 |
|
$process->setTimeout($timeout); |
136
|
2 |
|
$process->start(); |
137
|
|
|
|
138
|
2 |
|
$task->begin($process->getPid()); |
139
|
|
|
|
140
|
2 |
|
$process->wait($this); |
141
|
|
|
|
142
|
2 |
|
$task->finish($process->getExitCode()); |
143
|
2 |
|
} |
144
|
|
|
} |
145
|
|
|
|