0

I have a job class called NotifyUsersJob this class, of course, implements shouldQueue interface which is the default queue class that Laravel gives me. In the handle method, I have this logic.

Notification::send($this->users, new NotifyUser($this->message));

In NotifyUser notification class I send the notification throw WebPushChannel. My question is about should I implement shouldQueue in NotifyUser class also or not, and if I should do that why? By the way, everything is working fine both are working well, but I would like to know the right way of doing this.

Mustafa Omar
  • 399
  • 4
  • 15

1 Answers1

1

It depends on what you are trying to achieve. If you want to run the notification in background then yes. Using should queue will create a job that gets processed in the background such that you do not have to wait till it gets processed.

But if you think your application might break down at some point then you will not be able to get the error then and there. you will have to extract from failed job table if using database for queue. So my catch would be.

  1. If you think your notification can take some time to process use shouldqueue such that it runs in the background and your application dosent lag behind
  2. If you think you need to get errors instantly if the notification did not work then dont use shouldQueue
Deepesh Thapa
  • 1,721
  • 3
  • 19
  • 29
  • Thank you for your answer, I got the idea, Actually, I'm sending thousands of thousands of push notifications, so it will take so many time, so I think I should implement shouldQueue to protect the server from crashing because without shouldQueue the server will execute all notifications one after one very quickly which can make the server crashes, but with shouldQueue the server will execute them quietly and that what I need, Have I got that right? – Mustafa Omar Jan 21 '20 at 20:15
  • 1
    Yes Should queue will run in background. So if you have tons of notification being sent at once you should always use shouldqueue. Also specify tries on your schedule run command . Make it 3 or 4 tries so that if a notification breaks it will continue to process next one ASAP. I would recommend using Redis for storing your queued job. – Deepesh Thapa Jan 21 '20 at 20:35