Skip to content

Commit

Permalink
Merge pull request #8 from vuongxuongminh/2.0
Browse files Browse the repository at this point in the history
2.0
  • Loading branch information
vuongxuongminh authored May 14, 2020
2 parents 849ee33 + 384c422 commit e6cfd0b
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 58 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
language: php

php:
- 7.1
- 7.2
- 7.3
- 7.4

install:
- composer self-update
Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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`.
Expand Down
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
23 changes: 23 additions & 0 deletions src/Async.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
30 changes: 0 additions & 30 deletions src/Process/SynchronousProcess.php

This file was deleted.

90 changes: 66 additions & 24 deletions tests/JobTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
],
],
];
}
}

0 comments on commit e6cfd0b

Please sign in to comment.