From d20d42745a1529319d60ddc1c8cd31a4ae8a360d Mon Sep 17 00:00:00 2001 From: karam mustafa Date: Fri, 21 Jan 2022 21:06:28 +0200 Subject: [PATCH 1/8] Add new option to throw exception if any task failed --- src/Classes/MultiProcess.php | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/src/Classes/MultiProcess.php b/src/Classes/MultiProcess.php index 0c12e02..a346c3a 100644 --- a/src/Classes/MultiProcess.php +++ b/src/Classes/MultiProcess.php @@ -4,6 +4,9 @@ namespace SOS\MultiProcess\Classes; +use Couchbase\Exception; +use Symfony\Component\Process\Exception\ProcessFailedException; +use Symfony\Component\Process\PhpProcess; use Symfony\Component\Process\Process; /** @@ -32,6 +35,7 @@ class MultiProcess 'workingDirectory' => null, 'enableOutput' => true, 'processTime' => 3, + 'throwIfTaskNotSuccess' => false, ]; /** * @@ -165,6 +169,31 @@ public function displayOutputMessage($type, $buffer) } } + /** + * this function will execute run function in symfony component + * the function will check if we must display the error or the + * response status from the executed functions from the symfony classes. + * + * @return \SOS\MultiProcess\Classes\MultiProcess + * @throws \Exception + * @author karam mustafa + */ + public function runPHP() + { + + while ($task = $this->checkIfCanProcess()) { + + $process = new PhpProcess(<< + EOF + ); + + // Add the process to the processing property + $this->processing[] = $process; + } + dd($process->run()); + return $this; + } /** * this function will execute run function in symfony component @@ -239,6 +268,11 @@ private function process($callback) // this callback could be a start or run function in symfony component // or might be any callback that accept Process parameter as a dependency. $callback($process); + + if (!$process->isSuccessful() && $this->getOptions('throwIfTaskNotSuccess')) { + throw new ProcessFailedException($process); + } + } } From 420fd8f03adab32ed8c70b1ad73d6c5bc976540a Mon Sep 17 00:00:00 2001 From: karam mustafa Date: Fri, 21 Jan 2022 22:07:57 +0200 Subject: [PATCH 2/8] Add run a php code feature. --- src/Classes/MultiProcess.php | 21 ++++++++++----------- src/Facades/MultiProcessFacade.php | 1 + 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/Classes/MultiProcess.php b/src/Classes/MultiProcess.php index a346c3a..189438e 100644 --- a/src/Classes/MultiProcess.php +++ b/src/Classes/MultiProcess.php @@ -4,7 +4,6 @@ namespace SOS\MultiProcess\Classes; -use Couchbase\Exception; use Symfony\Component\Process\Exception\ProcessFailedException; use Symfony\Component\Process\PhpProcess; use Symfony\Component\Process\Process; @@ -170,9 +169,7 @@ public function displayOutputMessage($type, $buffer) } /** - * this function will execute run function in symfony component - * the function will check if we must display the error or the - * response status from the executed functions from the symfony classes. + * run the php codes from tasks, and each task must be a callback function. * * @return \SOS\MultiProcess\Classes\MultiProcess * @throws \Exception @@ -180,18 +177,20 @@ public function displayOutputMessage($type, $buffer) */ public function runPHP() { - while ($task = $this->checkIfCanProcess()) { - - $process = new PhpProcess(<< - EOF - ); + $process = new PhpProcess(""); // Add the process to the processing property $this->processing[] = $process; + + // run this task + $process->run(); + + if (!$process->isSuccessful() && $this->getOptions('throwIfTaskNotSuccess')) { + throw new ProcessFailedException($process); + } + } - dd($process->run()); return $this; } diff --git a/src/Facades/MultiProcessFacade.php b/src/Facades/MultiProcessFacade.php index 63f1185..97a05b6 100644 --- a/src/Facades/MultiProcessFacade.php +++ b/src/Facades/MultiProcessFacade.php @@ -11,6 +11,7 @@ * @method MultiProcess setTasks( ...$args) * @method MultiProcess start($callback) * @method MultiProcess run($callback) + * @method MultiProcess runPHP($callback) * @method MultiProcess setOptions($options) */ class MultiProcessFacade extends Facade From 69f2ad3f988a5afea8febbd2cc6f1e675f7c5877 Mon Sep 17 00:00:00 2001 From: karam mustafa Date: Fri, 21 Jan 2022 22:13:18 +0200 Subject: [PATCH 3/8] update doc --- README.md | 1 + docs/commands.md | 16 +++++++++------- docs/php.md | 41 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 7 deletions(-) create mode 100644 docs/php.md diff --git a/README.md b/README.md index 152ca4f..353a0d0 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,7 @@ php artisan multi-process:install Features ----------- - [Run process from commands](https://github.com/syrian-open-source/laravel-multi-process/blob/main/docs/commands.md) +- [Run php codes](https://github.com/syrian-open-source/laravel-multi-process/blob/main/docs/php.md) Changelog diff --git a/docs/commands.md b/docs/commands.md index b4261a4..5ac23d7 100644 --- a/docs/commands.md +++ b/docs/commands.md @@ -3,26 +3,26 @@ Usage * Define multiple process and execute them by start function. ```shell - $proceses = \SOS\MultiProcess\Facades\MultiProcessFacade::setTasks( + $process = \SOS\MultiProcess\Facades\MultiProcessFacade::setTasks( "php artisan make:model modelName", "php artisan make:model ControllerName", // and you can define unlimited commands ); - $proceses->start(); + $process->start(); ``` * Define multiple process and execute them by run function. ```shell - $proceses = \SOS\MultiProcess\Facades\MultiProcessFacade::setTasks( + $process = \SOS\MultiProcess\Facades\MultiProcessFacade::setTasks( "php artisan make:model modelName", "php artisan make:model ControllerName", // and you can define unlimited commands ); // run function will allows you to get the output from the execution process. - $proceses->run(); + $process->run(); ``` * Add options. @@ -32,7 +32,7 @@ Usage // default options are in multi_process.php file. // you can change them from the file // or you can basicly added them from the setter function. - $proceses = \SOS\MultiProcess\Facades\MultiProcessFacade::setTasks( + $process = \SOS\MultiProcess\Facades\MultiProcessFacade::setTasks( "php artisan make:model modelName", "php artisan make:model ControllerName", // and you can define unlimited commands @@ -41,10 +41,12 @@ Usage 'ideTimeOut' => 60, 'enableOutput' => true, 'processTime' => 3, + + // thorw exceprtion if any task was failed. + 'throwIfTaskNotSuccess' => false, ); // run or start your tasks. - $proceses->start(); + $process->start(); ``` - diff --git a/docs/php.md b/docs/php.md new file mode 100644 index 0000000..bbb9fec --- /dev/null +++ b/docs/php.md @@ -0,0 +1,41 @@ +Usage +-------- +* Define multiple process and execute them by start function, +each task must be a callback function as you can see in the below example. + +```shell + $process = \SOS\MultiProcess\Facades\MultiProcessFacade::setTasks( + function () { + return \Illuminate\Support\Facades\DB::statement('delete from users where id = 5'); + }, function () { + return \Illuminate\Support\Facades\DB::statement('delete from users where id = 6'); + } + ); + $process->runPHP(); +``` +* Add options. + +```shell + + // default options are in multi_process.php file. + // you can change them from the file + // or you can basicly added them from the setter function. + $process = \SOS\MultiProcess\Facades\MultiProcessFacade::setTasks( + "php artisan make:model modelName", + "php artisan make:model ControllerName", + // and you can define unlimited commands + )->setOptions([ + 'timeOut' => 60, + 'ideTimeOut' => 60, + 'enableOutput' => true, + 'processTime' => 3, + + // thorw exceprtion if any task was failed. + 'throwIfTaskNotSuccess' => false, + ); + + // run or start your tasks. + $process->start(); + +``` + From 9dd2d599bf3f6b867019b8936c3230f724b4799a Mon Sep 17 00:00:00 2001 From: karam mustafa Date: Fri, 21 Jan 2022 22:14:46 +0200 Subject: [PATCH 4/8] update test --- tests/Feature/PHPTest.php | 40 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 tests/Feature/PHPTest.php diff --git a/tests/Feature/PHPTest.php b/tests/Feature/PHPTest.php new file mode 100644 index 0000000..52da6dd --- /dev/null +++ b/tests/Feature/PHPTest.php @@ -0,0 +1,40 @@ +setTasks( + function () { + echo 'process 1'; + }, + function () { + echo 'process 1'; + }, + function () { + echo 'process 1'; + } + ); + + foreach ($processor->start()->getTasks() as $task) { + $this->assertEquals($task['state'], $completedState); + } + + } +} From 87c9a2a31e4dd5c4d0374413dc86a4dd2d7649d3 Mon Sep 17 00:00:00 2001 From: karam mustafa Date: Fri, 21 Jan 2022 22:20:52 +0200 Subject: [PATCH 5/8] update test --- tests/Feature/PHPTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Feature/PHPTest.php b/tests/Feature/PHPTest.php index 52da6dd..b6d9fdf 100644 --- a/tests/Feature/PHPTest.php +++ b/tests/Feature/PHPTest.php @@ -6,11 +6,11 @@ use SOS\MultiProcess\Classes\MultiProcess; use SOS\MultiProcess\Tests\BaseTest; -class CommandsTest extends BaseTest +class PHPTest extends BaseTest { /** - * test if the commands are running successfully. + * test if the php codes are running successfully. * * @return void * @throws \Exception From dec60c47ef7d62b36bd70d454c039a199183b72b Mon Sep 17 00:00:00 2001 From: karam mustafa Date: Fri, 21 Jan 2022 22:37:54 +0200 Subject: [PATCH 6/8] refactor run function to be higher order --- src/Classes/MultiProcess.php | 71 ++++++++++++++++++++++++------------ 1 file changed, 47 insertions(+), 24 deletions(-) diff --git a/src/Classes/MultiProcess.php b/src/Classes/MultiProcess.php index 189438e..ec11c46 100644 --- a/src/Classes/MultiProcess.php +++ b/src/Classes/MultiProcess.php @@ -169,7 +169,7 @@ public function displayOutputMessage($type, $buffer) } /** - * run the php codes from tasks, and each task must be a callback function. + * run the php codes * * @return \SOS\MultiProcess\Classes\MultiProcess * @throws \Exception @@ -177,20 +177,11 @@ public function displayOutputMessage($type, $buffer) */ public function runPHP() { - while ($task = $this->checkIfCanProcess()) { - $process = new PhpProcess(""); - - // Add the process to the processing property - $this->processing[] = $process; - // run this task - $process->run(); + $this->phpProcess($this->higherOrderRun()); - if (!$process->isSuccessful() && $this->getOptions('throwIfTaskNotSuccess')) { - throw new ProcessFailedException($process); - } + $this->resolveNotRunningProcess($this->higherOrderRun()); - } return $this; } @@ -203,19 +194,9 @@ public function runPHP() */ public function run() { - $callback = function (Process $process) { - return $process->run(function ($type, $buffer) { + $this->process($this->higherOrderRun()); - // if we enable the output, then display this message depending on it type. - if ($this->getOptions('enableOutput')) { - $this->displayOutputMessage($type, $buffer); - } - }); - }; - - $this->process($callback); - - $this->resolveNotRunningProcess($callback); + $this->resolveNotRunningProcess($this->higherOrderRun()); return $this; } @@ -241,6 +222,29 @@ public function start() } + /** + * run the php codes from tasks, and each task must be a callback function. + * + * @param $callback + * + * @author karam mustafa + */ + private function phpProcess($callback) + { + while ($task = $this->checkIfCanProcess()) { + + $process = new PhpProcess("getCommandKey()]()} ?>"); + + // Add the process to the processing property + $this->processing[] = $process; + + $callback($process); + + if (!$process->isSuccessful() && $this->getOptions('throwIfTaskNotSuccess')) { + throw new ProcessFailedException($process); + } + } + } /** * this function will set the require config to a symfony process component. @@ -371,4 +375,23 @@ private function checkIfCanProcess() : false; } + /** + * return a callback that execute run function inside process component. + * + * @return \Closure + * @author karam mustafa + */ + private function higherOrderRun() + { + return function (Process $process) { + return $process->run(function ($type, $buffer) { + + // if we enable the output, then display this message depending on it type. + if ($this->getOptions('enableOutput')) { + $this->displayOutputMessage($type, $buffer); + } + }); + }; + } + } From 46a3c79bee5e63dc3e9f91e0b6b335449f6dc8b7 Mon Sep 17 00:00:00 2001 From: karam mustafa Date: Fri, 21 Jan 2022 22:38:08 +0200 Subject: [PATCH 7/8] update test --- tests/Feature/PHPTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/Feature/PHPTest.php b/tests/Feature/PHPTest.php index b6d9fdf..64ce9ae 100644 --- a/tests/Feature/PHPTest.php +++ b/tests/Feature/PHPTest.php @@ -25,14 +25,14 @@ function () { echo 'process 1'; }, function () { - echo 'process 1'; + echo 'process 2'; }, function () { - echo 'process 1'; + echo 'process 3'; } ); - foreach ($processor->start()->getTasks() as $task) { + foreach ($processor->runPHP()->getTasks() as $task) { $this->assertEquals($task['state'], $completedState); } From feae01dd816d5906063034e225e592f07025eda6 Mon Sep 17 00:00:00 2001 From: karam mustafa Date: Fri, 21 Jan 2022 22:41:19 +0200 Subject: [PATCH 8/8] update workflow --- .github/workflows/php-cs-fixer.yml | 23 +++++++++++++++++++++ .github/workflows/php.yml | 10 ++------- .github/workflows/tests.yml | 10 +++++---- .github/workflows/update-changelog.yml | 28 ++++++++++++++++++++++++++ 4 files changed, 59 insertions(+), 12 deletions(-) create mode 100644 .github/workflows/php-cs-fixer.yml create mode 100644 .github/workflows/update-changelog.yml diff --git a/.github/workflows/php-cs-fixer.yml b/.github/workflows/php-cs-fixer.yml new file mode 100644 index 0000000..4f13700 --- /dev/null +++ b/.github/workflows/php-cs-fixer.yml @@ -0,0 +1,23 @@ +name: Check & fix styling + +on: [push,pull_request] + +jobs: + php-cs-fixer: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v2 + with: + ref: ${{ github.head_ref }} + + - name: Run PHP Code Style Fixer + uses: docker://oskarstark/php-cs-fixer-ga + with: + args: --config=.php-cs-fixer.dist.php --allow-risky=yes + + - name: Commit changes + uses: stefanzweifel/git-auto-commit-action@v4 + with: + commit_message: Fix styling diff --git a/.github/workflows/php.yml b/.github/workflows/php.yml index cce756c..f80bcd5 100644 --- a/.github/workflows/php.yml +++ b/.github/workflows/php.yml @@ -2,9 +2,9 @@ name: PHP Composer on: push: - branches: [ main ] + branches: [main] pull_request: - branches: [ main ] + branches: [main] jobs: build: @@ -27,9 +27,3 @@ jobs: ${{ runner.os }}-php- - name: Install dependencies run: composer install --prefer-dist --no-progress - - # Add a test script to composer.json, for instance: "test": "vendor/bin/phpunit" - # Docs: https://getcomposer.org/doc/articles/scripts.md - - # - name: Run test suite - # run: composer run-script test diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 5ec3375..c4c6394 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -9,13 +9,15 @@ jobs: fail-fast: false matrix: os: [ubuntu-latest] - php: [8.1, 8.0] - laravel: [8.*] + php: [7.4, 8.0] + laravel: [8.*,7.*] dependency-version: [prefer-stable] include: - - testbench: ^6.23 - laravel: 8.* + - testbench: ^6.23 + laravel: 8.* + - testbench: 5.20 + laravel: 7.* name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.dependency-version }} - ${{ matrix.os }} diff --git a/.github/workflows/update-changelog.yml b/.github/workflows/update-changelog.yml new file mode 100644 index 0000000..fa56639 --- /dev/null +++ b/.github/workflows/update-changelog.yml @@ -0,0 +1,28 @@ +name: "Update Changelog" + +on: + release: + types: [released] + +jobs: + update: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v2 + with: + ref: main + + - name: Update Changelog + uses: stefanzweifel/changelog-updater-action@v1 + with: + latest-version: ${{ github.event.release.name }} + release-notes: ${{ github.event.release.body }} + + - name: Commit updated CHANGELOG + uses: stefanzweifel/git-auto-commit-action@v4 + with: + branch: main + commit_message: Update CHANGELOG + file_pattern: CHANGELOG.md