diff --git a/.travis.yml b/.travis.yml index d54cc35..b93d571 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,9 +1,9 @@ language: php php: - - 7.1 - 7.2 - 7.3 + - 7.4 install: - composer self-update diff --git a/CHANGELOG.md b/CHANGELOG.md index 1ee2ab1..5f58578 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Changelog +## 2.0.0 + +- Add `batchRun` method. +- Remove class `VXM\Async\Process\SynchronousProcess`. + ## 1.1.1 - Upgrade `spatie/async` package to `^1.4.0`. diff --git a/README.md b/README.md index e527cdb..6f2acf5 100644 --- a/README.md +++ b/README.md @@ -91,6 +91,15 @@ Async::run('Your\AsyncJobs\Class@jobB'); Async::run('Your\AsyncJobs\Class@jobC'); Async::run('Your\AsyncJobs\Class@jobD'); +// Another way: + +Async::batchRun( + 'Your\AsyncJobs\Class@jobA', + 'Your\AsyncJobs\Class@jobB', + 'Your\AsyncJobs\Class@jobC', + 'Your\AsyncJobs\Class@jobD' +); + $results = Async::wait(); // result return from jobs above ``` @@ -122,6 +131,12 @@ Async::run('AsyncJobClass@handleMethod', [ 'timeout' => 'AsyncJobEventListener@handleTimeout', 'error' => 'AsyncJobEventListener@handleError' ]); + +Async::batchRun( + ['AsyncJobClassA@handleMethod', ['success' => 'AsyncJobEventListenerA@handleSuccess']], + ['AsyncJobClassB@handleMethod', ['success' => 'AsyncJobEventListenerB@handleSuccess']], + ['AsyncJobClassC@handleMethod', ['success' => 'AsyncJobEventListenerC@handleSuccess']] +); ``` ## Working with complex job @@ -205,6 +220,15 @@ use App\AsyncJobs\MyJob; $model = App\MyModel::find(1); Async::run(new MyJob($model)); + +// or batch run with multiple models: + +$model2 = App\MyModel::find(2); + +Async::batchRun( + new MyJob($model), + new MyJob($model2) +); ``` ## Compare with queue diff --git a/composer.json b/composer.json index ee647b6..c35412c 100644 --- a/composer.json +++ b/composer.json @@ -14,14 +14,13 @@ } ], "require": { - "php": "^7.1", + "php": "^7.2", "illuminate/support": "^5.8 || ^6.0", "spatie/async": "^1.4.0" }, "require-dev": { "orchestra/testbench": "^3.8 || ^4.0", - "phpunit/phpunit": "~7.5 || ^8.0", - "scrutinizer/ocular": "^1.5" + "scrutinizer/ocular": "^1.7" }, "autoload": { "psr-4": { diff --git a/src/Async.php b/src/Async.php index 4bb5051..e1f98a7 100644 --- a/src/Async.php +++ b/src/Async.php @@ -67,6 +67,29 @@ public function run($job, array $events = []): self return $this; } + /** + * Batch execute async jobs. + * + * @param array $jobs + * @return static + * @see run() + * @since 2.0.0 + */ + public function batchRun(...$jobs): self + { + foreach ($jobs as $job) { + if (is_array($job)) { + [$job, $events] = $job; + } else { + $events = []; + } + + $this->run($job, $events); + } + + return $this; + } + /** * Wait until all async jobs done and return job results. * diff --git a/src/Process/SynchronousProcess.php b/src/Process/SynchronousProcess.php deleted file mode 100644 index 7022505..0000000 --- a/src/Process/SynchronousProcess.php +++ /dev/null @@ -1,30 +0,0 @@ - - * @since 1.0.0 - * @deprecated https://github.com/spatie/async/pull/73 had solved. This class will be remove on version 2. - */ -class SynchronousProcess extends BaseSynchronousProcess -{ - /** - * Hotfix: https://github.com/spatie/async/pull/73. - * - * @return Throwable - */ - protected function resolveErrorOutput(): Throwable - { - return $this->getErrorOutput(); - } -} diff --git a/tests/JobTest.php b/tests/JobTest.php index 9591356..fa0bf75 100644 --- a/tests/JobTest.php +++ b/tests/JobTest.php @@ -16,41 +16,83 @@ */ class JobTest extends TestCase { - public function testHandleSuccess() + /** + * @dataProvider successJobProvider + */ + public function testHandleSuccess($handler, array $events) { - Async::run(TestClass::class, [ - 'success' => 'VXM\Async\Tests\EventTestClass@success', - ]); + Async::run($handler, $events); - Async::run(new TestClass, [ - 'success' => 'VXM\Async\Tests\EventTestClass@success', - ]); + $this->assertStringContainsString('ok!', current(Async::wait())); + } - Async::run(function () { - return 'ok!'; - }, [ - 'success' => 'VXM\Async\Tests\EventTestClass@success', - ]); + public function testBatchHandleSuccess() + { + Async::batchRun(...$this->successJobProvider()); foreach (Async::wait() as $result) { $this->assertStringContainsString('ok!', $result); } } - public function testHandleError() + /** + * @dataProvider errorJobProvider + */ + public function testHandleError($handler, array $events) { - Async::run(TestClass::class.'@handleException', [ - 'error' => 'VXM\Async\Tests\EventTestClass@catch', - ]); + Async::run($handler, $events); + $this->assertEmpty(Async::wait()); + } - Async::run(function () { - throw new TestException('ok!'); - }, [ - 'error' => 'VXM\Async\Tests\EventTestClass@catch', - ]); + public function testBatchHandleError() + { + Async::batchRun(...$this->errorJobProvider()); + $this->assertEmpty(Async::wait()); + } - foreach (Async::wait() as $result) { - $this->assertNull($result); - } + public function successJobProvider(): array + { + return [ + [ + TestClass::class, + [ + 'success' => 'VXM\Async\Tests\EventTestClass@success', + ], + ], + [ + new TestClass, + [ + 'success' => 'VXM\Async\Tests\EventTestClass@success', + ], + ], + [ + function () { + return 'ok!'; + }, + [ + 'success' => 'VXM\Async\Tests\EventTestClass@success', + ], + ], + ]; + } + + public function errorJobProvider(): array + { + return [ + [ + TestClass::class.'@handleException', + [ + 'error' => 'VXM\Async\Tests\EventTestClass@catch', + ], + ], + [ + function () { + throw new TestException('ok!'); + }, + [ + 'error' => 'VXM\Async\Tests\EventTestClass@catch', + ], + ], + ]; } }