1313 */
1414
1515use B13 \ContentSync \Domain \Model \Configuration ;
16+ use B13 \ContentSync \Event \BeforeProcessRunnerExecutesCommandsEvent ;
1617use B13 \ContentSync \Exception ;
18+ use Psr \EventDispatcher \EventDispatcherInterface ;
1719use Psr \Log \LoggerInterface ;
1820use Symfony \Component \Process \Process ;
1921
2022final readonly class ProcessRunner
2123{
2224 public function __construct (
2325 private DatabaseParameterBuilder $ databaseParameterBuilder ,
24- private LoggerInterface $ logger
26+ private LoggerInterface $ logger ,
27+ private EventDispatcherInterface $ eventDispatcher
2528 ) {}
2629
2730 public function localToRemote (Configuration $ configuration ): void
@@ -42,8 +45,11 @@ public function localToRemote(Configuration $configuration): void
4245 $ file = rtrim ($ file , '/ ' );
4346 $ commands [] = 'rsync -a --delete --omit-dir-times --no-owner --no-group ' . $ localNode ->getBasePath () . $ file . '/ ' . $ remoteNode ->getConnection () . ': ' . $ remoteNode ->getBasePath () . $ file ;
4447 }
48+ $ beforeProcessRunnnerExecuteCommands = new BeforeProcessRunnerExecutesCommandsEvent ($ configuration , $ commands );
49+ $ this ->eventDispatcher ->dispatch ($ beforeProcessRunnnerExecuteCommands );
50+ $ commands = $ beforeProcessRunnnerExecuteCommands ->commands ;
4551 foreach ($ commands as $ command ) {
46- $ this ->exec ($ command );
52+ $ this ->exec ($ command, $ beforeProcessRunnnerExecuteCommands -> timeoutPerProcess );
4753 }
4854 }
4955
@@ -65,15 +71,18 @@ public function remoteToLocal(Configuration $configuration): void
6571 $ file = rtrim ($ file , '/ ' );
6672 $ commands [] = 'rsync -a --delete --omit-dir-times --no-owner --no-group ' . $ remoteNode ->getConnection () . ': ' . $ remoteNode ->getBasePath () . $ file . '/ ' . $ localNode ->getBasePath () . $ file ;
6773 }
74+ $ beforeProcessRunnnerExecuteCommands = new BeforeProcessRunnerExecutesCommandsEvent ($ configuration , $ commands );
75+ $ this ->eventDispatcher ->dispatch ($ beforeProcessRunnnerExecuteCommands );
76+ $ commands = $ beforeProcessRunnnerExecuteCommands ->commands ;
6877 foreach ($ commands as $ command ) {
69- $ this ->exec ($ command );
78+ $ this ->exec ($ command, $ beforeProcessRunnnerExecuteCommands -> timeoutPerProcess );
7079 }
7180 }
7281
73- private function exec (string $ cmd ): void
82+ private function exec (string $ cmd, int $ timeout ): void
7483 {
7584 $ this ->logger ->debug ($ cmd );
76- $ process = Process::fromShellCommandline ($ cmd );
85+ $ process = Process::fromShellCommandline (command: $ cmd, timeout: $ timeout );
7786 $ process ->run ();
7887 if (!$ process ->isSuccessful ()) {
7988 throw new Exception ('cannot exec command ' . $ cmd . ' with error ' . $ process ->getErrorOutput (), 1600757440 );
0 commit comments