66use ApacheBorys \Retry \Entity \Config ;
77use ApacheBorys \Retry \Entity \Message ;
88use ApacheBorys \Retry \Interfaces \Executor ;
9+ use Psr \Log \LoggerInterface ;
910
1011class CommandExecutor implements Executor
1112{
@@ -16,19 +17,26 @@ class CommandExecutor implements Executor
1617 private array $ environmentVars ;
1718 private ?string $ cwd ;
1819 private ?array $ envVariablesSnapshot = null ;
20+ private ?LoggerInterface $ logger ;
1921
2022 /**
2123 * @param string $commandAddress First operand after php
2224 * @param array<string, string> $arguments Array of arguments. Key - argument name, value - regular expression for error message
2325 * @param string|null $cwd The initial working dir for the command
2426 * @param array<String, String> $environmentVars Set of env variables to execute command. It will be set before execution and rolled back after
2527 */
26- public function __construct (string $ commandAddress , array $ arguments = [], ?string $ cwd = null , array $ environmentVars = [])
27- {
28+ public function __construct (
29+ string $ commandAddress ,
30+ array $ arguments = [],
31+ ?string $ cwd = null ,
32+ array $ environmentVars = [],
33+ ?LoggerInterface $ logger = null
34+ ) {
2835 $ this ->commandAddress = $ commandAddress ;
2936 $ this ->arguments = $ arguments ;
3037 $ this ->cwd = $ cwd ;
3138 $ this ->environmentVars = $ environmentVars ;
39+ $ this ->logger = $ logger ;
3240 }
3341
3442 public function handle (Message $ message ): bool
@@ -66,17 +74,25 @@ public function handle(Message $message): bool
6674 sleep (1 );
6775 }
6876
69- $ returnValue = proc_close ($ process );
77+ proc_close ($ process );
7078 } catch (\Throwable $ e ) {
7179 $ this ->rollbackEnvironmentVariables ();
80+ if ($ this ->logger ) {
81+ $ this ->logger ->debug (
82+ sprintf (
83+ 'Command executor in Retry library catch exception during execution of command. Error: %s ' ,
84+ $ e ->getMessage ()
85+ )
86+ );
87+ }
7288
73- throw $ e ;
89+ return false ;
7490 }
7591
7692 $ this ->rollbackEnvironmentVariables ();
7793 putenv (self ::ALIAS_FOR_CORRELATION_ID );
7894
79- return $ returnValue !== - 1 ;
95+ return true ;
8096 }
8197
8298 public function compilePayload (\Throwable $ exception , Config $ config ): array
0 commit comments