@@ -33,24 +33,22 @@ public function __construct(bool $isDryRun, Logger $logger)
33
33
*/
34
34
public function run (Task $ task ): array
35
35
{
36
- $ results = [];
36
+ // Start all processes asynchronously
37
+ $ processes = [];
37
38
foreach ($ task ->getServers () as $ server ) {
38
- $ results [] = $ this ->doRun ($ server , $ server ->resolveProperties ($ task ->getShellCommand ()), $ task ->getEnvVars ());
39
+ $ processes [] = $ this ->startProcess ($ server , $ server ->resolveProperties ($ task ->getShellCommand ()), $ task ->getEnvVars ());
39
40
}
40
41
41
- return $ results ;
42
- }
43
-
44
- private function createProcess (string $ shellCommand ): Process
45
- {
46
- if (method_exists (Process::class, 'fromShellCommandline ' )) {
47
- return Process::fromShellCommandline ($ shellCommand );
42
+ // Collect all their results
43
+ $ results = [];
44
+ foreach ($ processes as $ process ) {
45
+ $ results [] = $ process ->getCompletionResult ();
48
46
}
49
47
50
- return new Process ( $ shellCommand ) ;
48
+ return $ results ;
51
49
}
52
50
53
- private function doRun (Server $ server , string $ shellCommand , array $ envVars ): TaskCompleted
51
+ private function startProcess (Server $ server , string $ shellCommand , array $ envVars ): PendingTask
54
52
{
55
53
if ($ server ->has (Property::project_dir)) {
56
54
$ shellCommand = sprintf ('cd %s && %s ' , $ server ->get (Property::project_dir), $ shellCommand );
@@ -67,26 +65,9 @@ private function doRun(Server $server, string $shellCommand, array $envVars): Ta
67
65
68
66
$ this ->logger ->log (sprintf ('[<server>%s</>] Executing command: <command>%s</> ' , $ server , $ shellCommand ));
69
67
70
- if ($ this ->isDryRun ) {
71
- return new TaskCompleted ($ server , '' , 0 );
72
- }
73
-
74
- if ($ server ->isLocalHost ()) {
75
- $ process = $ this ->createProcess ($ shellCommand );
76
- } else {
77
- $ process = $ this ->createProcess (sprintf ('%s %s ' , $ server ->getSshConnectionString (), escapeshellarg ($ shellCommand )));
78
- }
79
-
80
- $ process ->setTimeout (null );
81
-
82
- $ process = $ process ->mustRun (function ($ type , $ buffer ) {
83
- if (Process::ERR === $ type ) {
84
- $ this ->logger ->log (Str::prefix (rtrim ($ buffer , PHP_EOL ), '| <stream>err ::</> ' ));
85
- } else {
86
- $ this ->logger ->log (Str::prefix (rtrim ($ buffer , PHP_EOL ), '| <stream>out ::</> ' ));
87
- }
88
- });
68
+ $ pendingTask = new PendingTask ($ server , $ shellCommand , $ this ->logger , $ this ->isDryRun );
69
+ $ pendingTask ->start ();
89
70
90
- return new TaskCompleted ( $ server , $ process -> getOutput (), $ process -> getExitCode ()) ;
71
+ return $ pendingTask ;
91
72
}
92
73
}
0 commit comments