Skip to content

Commit

Permalink
Added support for the exception in the error callback
Browse files Browse the repository at this point in the history
  • Loading branch information
mnapoli committed Oct 21, 2013
1 parent 46d43f0 commit 52605e1
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 8 deletions.
23 changes: 23 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Change log

## 0.2.0

Added support for receiving the exception in the `error` callback:

```php
$error = function (Exception $e) {
echo "There was an error while completing the operation: " . $e->getMessage();
}

$workDispatcher->runBackground($task, 5, null, null, $error);
```

However the exception will not be the original exception that made the worker error. The reason for that
is that it is complex to serialize an exception and transmit it between the worker and the web request.

It becomes really useful though when using the `SimpleWorkDispatcher`, as you will get the original exception here.
It can help debugging a lot in a development environment.

## 0.1.0

First release.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ class BigComputationExecutor implements MyCLabs\Work\TaskExecutor\TaskExecutor
}
```

## Read more

Read more in [the docs](doc/).

## Contributing

You can run the tests with PHPUnit:
Expand Down
6 changes: 3 additions & 3 deletions doc/RabbitMQ.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,14 @@ This can be achieve using this library. Here is a schema of how it works interna
On you client side, you can use `runBackground` like so:

```php
$completed = function() {
$completed = function () {
echo "The operation has completed.";
}
$timeout = function() {
$timeout = function () {
echo "The operation is being applied. You will be notified when it has completed.";
// to notify the user, see below
}
$error = function() {
$error = function (Exception $e) {
echo "There was an error while completing the operation!";
}

Expand Down
3 changes: 2 additions & 1 deletion src/MyCLabs/Work/Dispatcher/RabbitMQWorkDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ private function waitForTask(
// If the first message of the queue is a "errored" message from the worker
if ($message->body == 'errored') {
if ($errored !== null) {
call_user_func($errored);
$e = new \RuntimeException("An error occured in the background task");
call_user_func($errored, $e);
}
// Delete the temporary exchange
$this->channel->exchange_delete($exchange);
Expand Down
2 changes: 1 addition & 1 deletion src/MyCLabs/Work/Dispatcher/SimpleWorkDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function runBackground(
$this->worker->executeTask($task);
} catch (\Exception $e) {
if ($errored) {
call_user_func($errored);
call_user_func($errored, $e);
}
return;
}
Expand Down
2 changes: 1 addition & 1 deletion src/MyCLabs/Work/Dispatcher/WorkDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ abstract class WorkDispatcher
* @param int $wait Number of seconds to wait for the task to complete. If 0, doesn't wait.
* @param callable $completed Called (if $wait > 0) when the task has completed.
* @param callable $timedout Called (if $wait > 0) if we hit the timeout while waiting.
* @param callable $errored Called (if $wait > 0) if the task errors.
* @param callable $errored Called (if $wait > 0) if the task errors. Takes 1 parameter which is the exception.
*
* @return void No results
*/
Expand Down
3 changes: 2 additions & 1 deletion tests/FunctionalTest/MyCLabs/Work/RabbitMQ/RabbitMQTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,8 @@ public function testRunBackgroundWaitError()
$mock->expects($this->never())
->method('timeout');
$mock->expects($this->once())
->method('errored');
->method('errored')
->with($this->isInstanceOf('Exception'));

$workDispatcher->runBackground(new FakeTask(), 1, [$mock, 'completed'], [$mock, 'timeout'], [$mock, 'errored']);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ public function testCallbackError()
$mock->expects($this->never())
->method('timeout');
$mock->expects($this->once())
->method('errored');
->method('errored')
->with($this->isInstanceOf('Exception'));

$dispatcher->runBackground($task, 1, [$mock, 'completed'], [$mock, 'timeout'], [$mock, 'errored']);
}
Expand Down

0 comments on commit 52605e1

Please sign in to comment.