简体   繁体   中英

PHP Laravel Queue's Events and jobs

I have a question

I have a simple system where a User creates a Booking After a user has created a booking, an email and a SMS will be sent to the user.

I have though of multiple ways to approach this

  1. After the booking was made, send the email and the SMS(blocking)
  2. After the booking was made, Fire an event that is queueable and has two listeners. One sends the email. The second one sends the SMS.
  3. After the booking was made, Fire and event, that will queue 2 jobs, one that sends the email, the second one sends the sms.

Option 1 is a no-go since it is blocking and doesn't have a retry to my Transactional Email provider or SMS provider.

Option 2 is do-able

Option 3 is do-able

In the future I want to be able to send the same email/sms without the BookingConfirmation event, so I will eventually need jobs that can be queue'd without the event.

Or is the event part just overhead and can I queue the jobs directly from the Controller instead of firing the event?

I am basically asking if I should use the Events at all or just use Jobs?

Thanks!

Events are useful because you can fire one event and have multiple action independently.

That means if in the future you want to have a third action, if you use events you can just add another event callback, meanwhile if you skip the events and directly put the jobs into the queue, you have to modify the controller to put the third action.

Convenience lies in the fact you don't have to alter any method in order to add new functionality.

In the end of the day, it's up to the developer. Events provide better code between each action. But performance wise, I think it is the same.

I would put all the tasks as Jobs in a queue, you then fire events (last line of job) when the Job is done, so you can do any follow up actions or things you want to do to inform the use.

So then you can have a job for each task and keep them simple, and also less exceptions or errors to track on each one.

The system I am using at the moment for example uses the event to trigger the next job, but other tasks I fire multiple jobs at once.

Eg.
user clicks booked.
dispatch job email
job email is queued
Job email is processed
Job email sends event
Event picked up by listener
Listener dispatch Job SMS
job SMS is queued
Job SMS is processed
Job SMS sends event
Event picked up by listener
anything else you want to do...

might sound a lot of steps but all are short and then simple to follow and update.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM