From eade725d6c3d81f55857aad0f96ffc3e15a49087 Mon Sep 17 00:00:00 2001 From: arefeh <69645908+arefeh-rj@users.noreply.github.com> Date: Mon, 5 Oct 2020 21:24:50 +0330 Subject: [PATCH 1/4] Developer (#14) * Update Executor.php * add timeOut * clean code * fix namespace * revert commit * fix style * fix style again * revert * update Executor.php * add blank lines * fix style Co-authored-by: Arefe Rajabian --- src/Classes/Executor.php | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/Classes/Executor.php b/src/Classes/Executor.php index dc2088a..556e873 100644 --- a/src/Classes/Executor.php +++ b/src/Classes/Executor.php @@ -62,17 +62,18 @@ abstract public function run(): self; * should be executed. * * @param string $command + * @param float $timeOut * @param bool $isInteractive * @return $this * @throws ExecutorException */ - public function runArtisan(string $command, bool $isInteractive = false): self + public function runArtisan(string $command, bool $isInteractive = false, float $timeOut = 60): self { $this->validateCommand($command, $isInteractive); $command = 'php artisan '.$command; - $this->runCommand($command, $isInteractive); + $this->runCommand($command, $isInteractive, $timeOut); return $this; } @@ -82,15 +83,16 @@ public function runArtisan(string $command, bool $isInteractive = false): self * items that should be executed. * * @param string $command + * @param float $timeOut * @param bool $isInteractive * @return $this * @throws ExecutorException */ - public function runExternal(string $command, bool $isInteractive = false): self + public function runExternal(string $command, bool $isInteractive = false, float $timeOut = 60): self { $this->validateCommand($command, $isInteractive); - $this->runCommand($command, $isInteractive); + $this->runCommand($command, $isInteractive, $timeOut); return $this; } @@ -135,9 +137,10 @@ public function ping(string $url, array $headers = []): self * Handle the running of a console command. * * @param string $commandToRun - * @param bool $isInteractive + * @param float $timeOut + * @param bool $isInteractive */ - private function runCommand(string $commandToRun, bool $isInteractive = false): void + private function runCommand(string $commandToRun, bool $isInteractive = false, float $timeOut = 60): void { if ($isInteractive) { $this->runInteractiveCommand($commandToRun); @@ -147,6 +150,8 @@ private function runCommand(string $commandToRun, bool $isInteractive = false): $process = new Process(explode(' ', $commandToRun)); + $process->setTimeout($timeOut); + $process->setWorkingDirectory(base_path()); $process->run(function ($type, $buffer) { From 489e2b3dbedcb03b09ff2d9dea1b4c0fbba260fc Mon Sep 17 00:00:00 2001 From: Ashley Allen Date: Mon, 5 Oct 2020 19:19:41 +0100 Subject: [PATCH 2/4] Changed the timeOut to be an integer rather than a float. --- src/Classes/Executor.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/Classes/Executor.php b/src/Classes/Executor.php index 556e873..ecb8c21 100644 --- a/src/Classes/Executor.php +++ b/src/Classes/Executor.php @@ -62,12 +62,12 @@ abstract public function run(): self; * should be executed. * * @param string $command - * @param float $timeOut + * @param int $timeOut * @param bool $isInteractive * @return $this * @throws ExecutorException */ - public function runArtisan(string $command, bool $isInteractive = false, float $timeOut = 60): self + public function runArtisan(string $command, bool $isInteractive = false, int $timeOut = 60): self { $this->validateCommand($command, $isInteractive); @@ -83,12 +83,12 @@ public function runArtisan(string $command, bool $isInteractive = false, float $ * items that should be executed. * * @param string $command - * @param float $timeOut + * @param int $timeOut * @param bool $isInteractive * @return $this * @throws ExecutorException */ - public function runExternal(string $command, bool $isInteractive = false, float $timeOut = 60): self + public function runExternal(string $command, bool $isInteractive = false, int $timeOut = 60): self { $this->validateCommand($command, $isInteractive); @@ -136,11 +136,11 @@ public function ping(string $url, array $headers = []): self /** * Handle the running of a console command. * - * @param string $commandToRun - * @param float $timeOut + * @param string $commandToRun + * @param int $timeOut * @param bool $isInteractive */ - private function runCommand(string $commandToRun, bool $isInteractive = false, float $timeOut = 60): void + private function runCommand(string $commandToRun, bool $isInteractive = false, int $timeOut = 60): void { if ($isInteractive) { $this->runInteractiveCommand($commandToRun); @@ -168,7 +168,7 @@ private function runCommand(string $commandToRun, bool $isInteractive = false, f /** * Handle the running of an interactive console command. * - * @param string $commandToRun + * @param string $commandToRun */ private function runInteractiveCommand(string $commandToRun): void { From d255d06e2a465e595c4b15461da24d37fd6ff5ad Mon Sep 17 00:00:00 2001 From: Ashley Allen Date: Mon, 5 Oct 2020 22:08:48 +0100 Subject: [PATCH 3/4] Updated README. --- README.md | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index fbab2d3..ed595db 100755 --- a/README.md +++ b/README.md @@ -103,10 +103,13 @@ class AppUpdate extends Executor } ``` -Note: In some cases, you may want to run a command that requires your input. For example, you might have a command that +In some cases, you may want to run a command that requires your input. For example, you might have a command that creates a new user in the database and need you to input some details. In this case, you can pass ``` true ``` as the second parameter to the ``` ->runArtisan() ``` method to specify that it is an interactive command. +To determine the process timeout for the command, you can also pass a time in seconds as the third parameter to the +``` ->runArtisan() ``` method. + #### Adding a Command To run a command (that can't be run with Artisan) via your Executor class, you can add the ``` runExternal() ``` method to your Executor's ``` run() ``` method. For example, the code below shows how you could set the Executor to run the built-in Composer ``` composer install ``` @@ -128,10 +131,14 @@ class AppUpdate extends Executor } ``` -Note: In some cases, you may want to run a command that requires your input. For example, you might have a command that +In some cases, you may want to run a command that requires your input. For example, you might have a command that creates a new user in the database and need you to input some details. In this case, you can pass ``` true ``` as the second parameter to the ``` ->runExternal() ``` method to specify that it is an interactive command. +To determine the process timeout for the command, you can also pass a time in seconds as the third parameter to the +``` ->runExternal() ``` method. + + #### Adding a Closure Sometimes you might want to run some code that doesn't necessarily fit into an existing command. In this case, you can add a closure to your Executor instead. The example below shows how to pass a simple closure to your Executor class: From c1dd06a2afb59d2e179e9c449f34e920db132d89 Mon Sep 17 00:00:00 2001 From: Ashley Allen Date: Mon, 5 Oct 2020 22:17:52 +0100 Subject: [PATCH 4/4] Updated CHANGELOG. --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3077366..346fc2e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ # Changelog +**v2.1.0 (released 2020-10-05):** +- Added a timeOut option for the Artisan and external commands. [#14](https://github.com/ash-jc-allen/laravel-executor/pull/14) + **v2.0.0 (released 2020-09-16):** - Added the functionality to run interactive commands. [#9](https://github.com/ash-jc-allen/laravel-executor/pull/9) [#10](https://github.com/ash-jc-allen/laravel-executor/pull/10) - Added support for Laravel 8 and Guzzle 7. [#12](https://github.com/ash-jc-allen/laravel-executor/pull/12)