You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
There is a different log entry when there's an exception. This is pretty straightforward when you're not queueing e-mails. It's not when you're queueing them. Ideally, we want to keep the responsibility of completed jobs with the job and e-mail, as failed does now.
The problem
Queueing an e-mail means it gets wrapped in a SendQueuedMailable job. This results in two different problems for us.
We do not have access to the handle method to log a successfully sent e-mail.
We do not have access to the job payload, so we cannot fetch the user id from it.
We do not prefer to naively mark the e-mail as sent when it's queued and mark it as failed when an exception occurs.
Workarounds
You could argue that sending e-mails should be logged through an event listener. But hooking into MessageSent means we do not have access to the job (and the payload).
The second event we can look at is the JobProcessed event. An example of what we tried is below:
This works and solves most of our requirements. It allows us to keep the log entries (or other actions) close to the actual job, and although I didn't include it in the workaround, we can inject the payload into the mailable.
Unfortunately, there is a problem with this workaround. We would need the payload in the failed method too. Without it, we don't know which user triggered it. The alternative is to drop the failed method and listen for JobFailed events, essentially using the same logic as completed jobs. We do not want to do this as it moves us away from Laravel's core.
TL;DR
We would like queued mailables to access the job's payload (InteractsWithQueue?).
We would like to introduce a completed method on all jobs, including queued mailables.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Use case
We want to keep an audit log of who sends an e-mail or starts a job. A log entry would look like this:
There is a different log entry when there's an exception. This is pretty straightforward when you're not queueing e-mails. It's not when you're queueing them. Ideally, we want to keep the responsibility of completed jobs with the job and e-mail, as
failed
does now.The problem
Queueing an e-mail means it gets wrapped in a
SendQueuedMailable
job. This results in two different problems for us.handle
method to log a successfully sent e-mail.Note: we're injecting the user id into the payload with
Queue::createPayloadUsing
; see: Injecting Additional Data into Laravel Queued Jobs.We do not prefer to naively mark the e-mail as sent when it's queued and mark it as failed when an exception occurs.
Workarounds
You could argue that sending e-mails should be logged through an event listener. But hooking into
MessageSent
means we do not have access to the job (and the payload).The second event we can look at is the
JobProcessed
event. An example of what we tried is below:This works and solves most of our requirements. It allows us to keep the log entries (or other actions) close to the actual job, and although I didn't include it in the workaround, we can inject the payload into the mailable.
Unfortunately, there is a problem with this workaround. We would need the payload in the
failed
method too. Without it, we don't know which user triggered it. The alternative is to drop thefailed
method and listen forJobFailed
events, essentially using the same logic ascompleted
jobs. We do not want to do this as it moves us away from Laravel's core.TL;DR
InteractsWithQueue
?).completed
method on all jobs, including queued mailables.Beta Was this translation helpful? Give feedback.
All reactions