-
Notifications
You must be signed in to change notification settings - Fork 100
Expand file tree
/
Copy pathPendingTask.php
More file actions
82 lines (68 loc) · 2.51 KB
/
Copy pathPendingTask.php
File metadata and controls
82 lines (68 loc) · 2.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<?php
/*
* This file is part of the EasyDeploy project.
*
* (c) Javier Eguiluz <javier.eguiluz@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace EasyCorp\Bundle\EasyDeployBundle\Task;
use EasyCorp\Bundle\EasyDeployBundle\Helper\Str;
use EasyCorp\Bundle\EasyDeployBundle\Logger;
use EasyCorp\Bundle\EasyDeployBundle\Server\Server;
use Symfony\Component\Process\Process;
use Symfony\Component\Process\Exception\ProcessFailedException;
class PendingTask {
private $server;
private $dryRun;
private $process;
private $logger;
public function __construct (Server $server, string $shellCommand, Logger $logger, bool $dryRun = false)
{
$this->server = $server;
$this->dryRun = $dryRun;
$this->logger = $logger;
if ($this->dryRun) {
return;
}
if ($server->isLocalHost()) {
$this->process = $this->createProcess($shellCommand);
} else {
$this->process = $this->createProcess(sprintf('%s %s', $server->getSshConnectionString(), escapeshellarg($shellCommand)));
}
$this->process->setTimeout(null);
}
private function createProcess(string $shellCommand): Process
{
if (method_exists(Process::class, 'fromShellCommandline')) {
return Process::fromShellCommandline($shellCommand);
}
return new Process($shellCommand);
}
public function start ()
{
if ($this->dryRun) {
return;
}
$this->process->start(function ($type, $buffer) {
if (Process::ERR === $type) {
$this->logger->log(Str::prefix(rtrim($buffer, PHP_EOL), sprintf('| [<server>%s</>] <stream>err ::</> ', $this->server)));
} else {
$this->logger->log(Str::prefix(rtrim($buffer, PHP_EOL), sprintf('| [<server>%s</>] <stream>out ::</> ', $this->server)));
}
});
}
public function getCompletionResult ()
{
if ($this->dryRun) {
return new TaskCompleted($this->server, '', 0);
}
// Make sure we ran without errors
// As in https://github.com/symfony/symfony/blob/4.4/src/Symfony/Component/Process/Process.php#L266
if (0 !== $this->process->wait()) {
throw new ProcessFailedException($this->process);
}
return new TaskCompleted($this->server, $this->process->getOutput(), $this->process->getExitCode());
}
}