From 87d8094035fbff1f4e9bf0f1551de7845d19f011 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateus=20Guimar=C3=A3es?= Date: Sun, 29 Oct 2023 23:10:23 -0300 Subject: [PATCH 1/7] Update events.md --- events.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/events.md b/events.md index 00a9c6f028c..5ad18826bbd 100644 --- a/events.md +++ b/events.md @@ -414,6 +414,24 @@ If your queue connection's `after_commit` configuration option is set to `false` public $afterCommit = true; } +Alternatively, you might also implement the `ShouldHandleEventsAfterCommit` interface: + + **Note** > To learn more about working around these issues, please review the documentation regarding [queued jobs and database transactions](/docs/{{version}}/queues#jobs-and-database-transactions). From a05ce803df9767f1503e51165036ad3bc068b393 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateus=20Guimar=C3=A3es?= Date: Sun, 29 Oct 2023 23:18:59 -0300 Subject: [PATCH 2/7] ShouldQueueAfterCommit docs --- queues.md | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/queues.md b/queues.md index debaa41b747..d2f561536d6 100644 --- a/queues.md +++ b/queues.md @@ -816,6 +816,42 @@ Likewise, if the `after_commit` configuration option is set to `true`, you may i ProcessPodcast::dispatch($podcast)->beforeCommit(); + +#### Specifying Commit Dispatch Behavior On The Job Class + +Alternatively, if you want to specify the commit dispatch behavior inside the job class, you can do so by implementing the `ShouldQueueAfterCommit` interface instead of `ShouldQueue`: + ### Job Chaining From 31a9b452c6035aefae9f9e8b47653d24437a798b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateus=20Guimar=C3=A3es?= Date: Sun, 29 Oct 2023 23:26:57 -0300 Subject: [PATCH 3/7] ShouldDispatchAfterCommit docs --- events.md | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/events.md b/events.md index 5ad18826bbd..f8cc2c08b58 100644 --- a/events.md +++ b/events.md @@ -12,6 +12,7 @@ - [Queued Event Listeners & Database Transactions](#queued-event-listeners-and-database-transactions) - [Handling Failed Jobs](#handling-failed-jobs) - [Dispatching Events](#dispatching-events) + - [Interacting with Database Transactions](#interacting-with-database-transactions) - [Event Subscribers](#event-subscribers) - [Writing Event Subscribers](#writing-event-subscribers) - [Registering Event Subscribers](#registering-event-subscribers) @@ -550,6 +551,44 @@ To dispatch an event, you may call the static `dispatch` method on the event. Th > **Note** > When testing, it can be helpful to assert that certain events were dispatched without actually triggering their listeners. Laravel's [built-in testing helpers](#testing) make it a cinch. + +### Interacting with Database Transactions + +Whenever you dispatch events within a database transaction, you might want to instruct the framework to only dispatch the events once the transaction is commited. To do so, you can implement the `ShouldDispatchAfterCommit` interface on the Event class: + + ## Event Subscribers From f652ceacfb1851ac40cc5129f4b936226c4c15d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateus=20Guimar=C3=A3es?= Date: Sun, 29 Oct 2023 23:31:17 -0300 Subject: [PATCH 4/7] fix --- events.md | 39 +++++++++++++++------------------------ 1 file changed, 15 insertions(+), 24 deletions(-) diff --git a/events.md b/events.md index f8cc2c08b58..baad28a97f0 100644 --- a/events.md +++ b/events.md @@ -554,40 +554,31 @@ To dispatch an event, you may call the static `dispatch` method on the event. Th ### Interacting with Database Transactions -Whenever you dispatch events within a database transaction, you might want to instruct the framework to only dispatch the events once the transaction is commited. To do so, you can implement the `ShouldDispatchAfterCommit` interface on the Event class: +Whenever you're dealing with database transactions, you might want the events to only be dispatched once the transaction is committed. To do so, you can implement the `ShouldDispatchAfterCommit` interface on the Event class: Each database transaction's events are isolated. If a transaction fails, the events will be discarded and it will not affect the parent transaction. ## Event Subscribers From 14353d1320cbb3fc205af17bd342210aa83bea91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateus=20Guimar=C3=A3es?= Date: Mon, 30 Oct 2023 11:16:07 -0300 Subject: [PATCH 5/7] Remove $afterCommit from the ShouldHandleEventsAfterCommit example --- events.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/events.md b/events.md index baad28a97f0..0d330f4ce04 100644 --- a/events.md +++ b/events.md @@ -428,8 +428,6 @@ Alternatively, you might also implement the `ShouldHandleEventsAfterCommit` inte class SendShipmentNotification implements ShouldQueue, ShouldHandleEventsAfterCommit { use InteractsWithQueue; - - public $afterCommit = true; } From df512d79b264685fbfc34d400be518b320b55d6b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateus=20Guimar=C3=A3es?= Date: Mon, 30 Oct 2023 11:16:37 -0300 Subject: [PATCH 6/7] Change verbiage --- events.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/events.md b/events.md index 0d330f4ce04..92c025b5827 100644 --- a/events.md +++ b/events.md @@ -576,7 +576,7 @@ Whenever you're dealing with database transactions, you might want the events to ) {} } ->Each database transaction's events are isolated. If a transaction fails, the events will be discarded and it will not affect the parent transaction. +>Each database transaction's events are isolated. If a transaction fails, its events will be discarded and it will not affect the parent transaction. ## Event Subscribers From b4e2c7c82a3690e17d7b280778f9e7fca9345d21 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateus=20Guimar=C3=A3es?= Date: Mon, 30 Oct 2023 20:09:16 -0300 Subject: [PATCH 7/7] Apply suggestions from code review Co-authored-by: Francis Lavoie --- events.md | 4 ++-- queues.md | 9 +++++---- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/events.md b/events.md index 92c025b5827..cc6d5b2c0c3 100644 --- a/events.md +++ b/events.md @@ -569,8 +569,8 @@ Whenever you're dealing with database transactions, you might want the events to use Dispatchable, InteractsWithSockets, SerializesModels; /** - * Create a new event instance. - */ + * Create a new event instance. + */ public function __construct( public Order $order, ) {} diff --git a/queues.md b/queues.md index d2f561536d6..2fbc96c17bd 100644 --- a/queues.md +++ b/queues.md @@ -820,6 +820,7 @@ Likewise, if the `after_commit` configuration option is set to `true`, you may i #### Specifying Commit Dispatch Behavior On The Job Class Alternatively, if you want to specify the commit dispatch behavior inside the job class, you can do so by implementing the `ShouldQueueAfterCommit` interface instead of `ShouldQueue`: +