@@ -33,24 +33,22 @@ public function __construct(bool $isDryRun, Logger $logger)
3333 */
3434 public function run (Task $ task ): array
3535 {
36- $ results = [];
36+ // Start all processes asynchronously
37+ $ processes = [];
3738 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 ());
3940 }
4041
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 ();
4846 }
4947
50- return new Process ( $ shellCommand ) ;
48+ return $ results ;
5149 }
5250
53- private function doRun (Server $ server , string $ shellCommand , array $ envVars ): TaskCompleted
51+ private function startProcess (Server $ server , string $ shellCommand , array $ envVars ): PendingTask
5452 {
5553 if ($ server ->has (Property::project_dir)) {
5654 $ 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
6765
6866 $ this ->logger ->log (sprintf ('[<server>%s</>] Executing command: <command>%s</> ' , $ server , $ shellCommand ));
6967
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 ();
8970
90- return new TaskCompleted ( $ server , $ process -> getOutput (), $ process -> getExitCode ()) ;
71+ return $ pendingTask ;
9172 }
9273}
0 commit comments