Skip to content

Commit

Permalink
Merge pull request #1 from mhfereydouni/handle_event_consumes_which_e…
Browse files Browse the repository at this point in the history
…ncounter_an_error

add error handling protocol for event consumer
  • Loading branch information
mhfereydouni authored May 15, 2023
2 parents f99eadc + ab9daed commit de1fddd
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 18 deletions.
16 changes: 0 additions & 16 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,3 @@
# Changelog

All notable changes to `:package_name` will be documented in this file.

## Add vhost option & read configs from env - 2023-04-11

- added vhost option
- configs to target env

## Laravel 10 & PHP 8.1 support - 2023-03-12

drop support for laravel < 10
drop support for PHP < 8.1
cleaner syntax for consumer configuration

## Changed config and added document for package - 2023-03-06

- changed config to be more intuitive
- document usage of package
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,18 @@ return [
// 'map_into' => '\App\Events\MapIntoEvent', // if you want to use the same event then remove this line
// ],
],

/** -----------------------------------------------
* options: 'sync', 'kind-sync', 'job'
* sync: event are fired when they are consumed and error will stop the consumer
* kind-sync: event are fired when they are consumed and error will not stop the consumer instead a log is stored
* job: events are fired in a queue via laravel jobs (Note: you should make sure there is a queue worker for queue)
*/
'event-consumer-mode' => 'sync',

'log-channel' => 'daily',

'queue-name' => 'default',
];
```

Expand Down Expand Up @@ -84,7 +96,7 @@ property to your event:
In the `rabbitmq.php` config file you should list all the events you want to consume.

```php
'consumers' => [
'event-consumers' => [
// [
// 'event' => '\App\Events\MyEvent',
// 'routing_key' => 'my_routing_key', // if this event does not use routing key then remove this line
Expand Down
12 changes: 12 additions & 0 deletions config/rabbitmq.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,16 @@
// 'map_into' => '\App\Events\MapIntoEvent', // if you want to use the same event then remove this line
// ],
],

/** -----------------------------------------------
* options: 'sync', 'kind-sync', 'job'
* sync: event are fired when they are consumed and error will stop the consumer
* kind-sync: event are fired when they are consumed and error will not stop the consumer instead a log is stored
* job: events are fired in a queue via laravel jobs (Note: you should make sure there is a queue worker for queue)
*/
'event-consumer-mode' => 'sync',

'log-channel' => 'daily',

'queue-name' => 'default',
];
26 changes: 25 additions & 1 deletion src/Commands/ConsumeEventMessages.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

use Illuminate\Console\Command;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Str;
use MHFereydouni\RabbitMQ\RabbitMQ;
use MHFereydouni\RabbitMQ\Support\EventJobWrapper;

class ConsumeEventMessages extends Command
{
Expand Down Expand Up @@ -58,6 +60,28 @@ public function fireEvent(array $payload, string $routingKey)
&& Str::is($event['routing_key'], $routingKey);
})['map_into_event'];

event(resolve($event, $payload));
$mode = config('rabbitmq.event-consumer-mode', 'sync');

if ($mode === 'sync') {
event(resolve($event, $payload));
} elseif ($mode === 'kind-sync') {
try {
event(resolve($event, $payload));
} catch (\Exception $exception) {
Log::channel(config('rabbitmq.log-channel'))
->error(
'Could not dispatch the event consumed from rabbitmq!',
[
'consumed_event' => $payload['event.name'],
'routing_key' => $routingKey,
'was_going_to_map_into' => $event,
'error_message' => $exception->getMessage(),
'error_trace' => $exception->getTraceAsString(),
]
);
}
} elseif ($mode === 'job') {
EventJobWrapper::dispatch($event, $payload);
}
}
}
32 changes: 32 additions & 0 deletions src/Support/EventJobWrapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace MHFereydouni\RabbitMQ\Support;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class EventJobWrapper implements ShouldQueue
{
use Dispatchable;
use InteractsWithQueue;
use Queueable;
use SerializesModels;

public function __construct(
public string $event,
public array $payload
) {}

public function handle(): void
{
event(resolve($this->event, $this->payload));
}

public function viaQueue(): string
{
return config('rabbitmq.queue-name', 'default');
}
}

0 comments on commit de1fddd

Please sign in to comment.