1
|
|
|
<?php |
2
|
|
|
/* (c) Anton Medvedev <[email protected]> |
3
|
|
|
* |
4
|
|
|
* For the full copyright and license information, please view the LICENSE |
5
|
|
|
* file that was distributed with this source code. |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Deployer\Executor; |
9
|
|
|
|
10
|
|
|
use Deployer\Exception\Exception; |
11
|
|
|
use Deployer\Exception\RunException; |
12
|
|
|
use Deployer\Host\Host; |
13
|
|
|
use Deployer\Task\Task; |
14
|
|
|
use Symfony\Component\Console\Input\Input; |
15
|
|
|
use Symfony\Component\Console\Output\Output; |
16
|
|
|
use Throwable; |
17
|
|
|
|
18
|
|
|
class Messenger |
19
|
|
|
{ |
20
|
|
|
private $input; |
21
|
|
|
private $output; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var int|double |
25
|
|
|
*/ |
26
|
|
|
private $startTime; |
27
|
|
|
|
28
|
|
|
public function __construct(Input $input, Output $output) |
29
|
|
|
{ |
30
|
|
|
$this->input = $input; |
31
|
|
|
$this->output = $output; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function startTask(Task $task) |
35
|
|
|
{ |
36
|
|
|
$this->startTime = round(microtime(true) * 1000); |
37
|
|
|
if (!$task->isShallow()) { |
38
|
|
|
$this->output->writeln("<fg=cyan;options=bold>task</> {$task->getName()}"); |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/* |
43
|
|
|
* Print task was ok. |
44
|
|
|
*/ |
45
|
|
|
public function endTask(Task $task) |
46
|
|
|
{ |
47
|
|
|
if ($task->isShallow()) { |
48
|
|
|
return; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$endTime = round(microtime(true) * 1000); |
52
|
|
|
$millis = $endTime - $this->startTime; |
53
|
|
|
$seconds = floor($millis / 1000); |
54
|
|
|
$millis = $millis - $seconds * 1000; |
55
|
|
|
$taskTime = ($seconds > 0 ? "{$seconds}s " : "") . "{$millis}ms"; |
56
|
|
|
|
57
|
|
|
if ($this->output->isVeryVerbose()) { |
58
|
|
|
$this->output->writeln("<fg=yellow;options=bold>done</> {$task->getName()} $taskTime"); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
if (!empty($this->input->getOption('profile'))) { |
62
|
|
|
$line = sprintf("%s\t%s\n", $task->getName(), $taskTime); |
63
|
|
|
file_put_contents($this->input->getOption('profile'), $line, FILE_APPEND); |
64
|
|
|
|
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function endOnHost(Host $host) |
69
|
|
|
{ |
70
|
|
|
if ($this->output->isVeryVerbose()) { |
71
|
|
|
$this->output->writeln("[{$host->tag()}] <info>ok</info>"); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function printException(Throwable $exception, Host $host) |
76
|
|
|
{ |
77
|
|
|
if ($exception instanceof RunException) { |
78
|
|
|
|
79
|
|
|
$message = ""; |
80
|
|
|
$message .= "[{$host->tag()}] <fg=white;bg=red> error </> <comment>in {$exception->getFilename()} on line {$exception->getLineNumber()}:</>\n"; |
81
|
|
|
if ($this->output->getVerbosity() === Output::VERBOSITY_NORMAL) { |
82
|
|
|
$message .= "[{$host->tag()}] <fg=green;options=bold>run</> {$exception->getCommand()}\n"; |
83
|
|
View Code Duplication |
foreach (explode("\n", $exception->getErrorOutput()) as $line) { |
84
|
|
|
$line = trim($line); |
85
|
|
|
if ($line !== "") { |
86
|
|
|
$message .= "[{$host->tag()}] <fg=red>err</> $line\n"; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
View Code Duplication |
foreach (explode("\n", $exception->getOutput()) as $line) { |
90
|
|
|
$line = trim($line); |
91
|
|
|
if ($line !== "") { |
92
|
|
|
$message .= "[{$host->tag()}] $line\n"; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
$message .= "[{$host->tag()}] <fg=red>exit code</> {$exception->getExitCode()} ({$exception->getExitCodeText()})\n"; |
97
|
|
|
$this->output->write($message); |
98
|
|
|
|
99
|
|
|
} else { |
100
|
|
|
|
101
|
|
|
$message = ""; |
102
|
|
|
$class = get_class($exception); |
103
|
|
|
$file = basename($exception->getFile()); |
104
|
|
|
$line = $exception->getLine(); |
105
|
|
|
if ($exception instanceof Exception) { |
106
|
|
|
$file = $exception->getFilename(); |
107
|
|
|
$line = $exception->getLineNumber(); |
108
|
|
|
} |
109
|
|
|
$message .= "[{$host->tag()}] <fg=white;bg=red> $class </> <comment>in $file on line $line:</>\n"; |
110
|
|
|
$message .= "[{$host->tag()}]\n"; |
111
|
|
View Code Duplication |
foreach (explode("\n", $exception->getMessage()) as $line) { |
112
|
|
|
$line = trim($line); |
113
|
|
|
if ($line !== "") { |
114
|
|
|
$message .= "[{$host->tag()}] <comment>$line</comment>\n"; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
$message .= "[{$host->tag()}]\n"; |
118
|
|
|
$this->output->write($message); |
119
|
|
|
|
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
if ($exception->getPrevious()) { |
123
|
|
|
$this->printException($exception->getPrevious(), $host); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|