1

I want to inform the seller, that the buyer is coming soon (about 2 hours before pickup time) via mail.

I would normally do it the hard way with CRON and a database table. Checking hourly if I find an order with pickup time minus 2 hours, only then sending the mail out.

Now, I would like to know if you would recommend using Queueing Jobs for sending Mails out.

With

$when = now()->addDays(10); //I would dynamically set the date

Mail::to($order->seller())
    ->later($when, new BuyerIsComing($order));

I can delay the delivery of a queued email message.

But how safe would this be? Especially, if someone is ordering something but is picking it up in let us exaggerate two months?

Is the Laravel queueing system rigid enough to behave correctly after long delays (i.e. 2 months)?

Edit

I'm using Redis for Queueing

Philipp Mochine
  • 4,351
  • 10
  • 36
  • 68

3 Answers3

2

You actually have nothing to worry about. Sending mail usually increases the response time of your application, so it's a good thing you want to delay the sending.

Queues are the way to go and it's pretty easy to setup in Laravel. Laravel supports a couple of them out of the box. I would advise you start with database and then try beanstalk etc.

Lastly and somehow more importantly, use a process manager like Supervisor to monitor and maintain your queue workers...

Take a look at https://laravel.com/docs/5.7/queues for more insight. Cheers.

Chukwuemeka Inya
  • 2,575
  • 1
  • 17
  • 23
  • Forgot to say that I'm using Redis. So I think I will use Database for sending mails just to be safe. – Philipp Mochine Sep 18 '18 at 12:06
  • 1
    Lol... Whether Database, Redis, Beanstalk etc...the main thing you should be worried about is making sure your queue workers don't fail. And you can ensure they don't through the use of a process monitor like Supervisor. Supervisor will restart your queue:work process if it fails. At some point, concerns like speed and scalability will become important to you. Do well to research further when the time comes. – Chukwuemeka Inya Sep 18 '18 at 12:41
  • I will do this. Okay good to know. Still unclear how redis is remembering all jobs if redis is failing, since I though it's only memory based not like a database – Philipp Mochine Sep 18 '18 at 17:11
  • 1
    Yeah, Redis is an in-memory data store but it also has the ability to persist data. You can read their documentation on data persistence for more info... https://redis.io/topics/persistence. You can stick with database in your development environment, but at production level, I would advise you go for Beanstalkd, RabbitMQ, SQS... These guys are specialized for queueing... – Chukwuemeka Inya Sep 18 '18 at 17:50
1

If by safe, you mean reliable, then it would be little different than sending an email immediately. If there's ever a possibility that your server "hiccups" and doesn't send an email, that possibility would be the same now as 10 minutes from now. Once the job is in the queue, it is persisted until completion (unless you use a memory-based driver, like Redis, which could get reset if the server reboots).

If you are using a database queue driver or remote, the log of queued jobs will remain even if the server is unavailable for a short period of time. Your queue will be honored even if the exact time stamp for when you want to send the job has expired. For instance if you schedule to send an email at 1:00pm but your server is down at that exact moment, when it comes back online it will still see the job because it is stored as incomplete and the time for the job is in the past, which will trigger the execution of the job at the next time your queue worker checks the job list.

Of course, this assumes that you have your queue worker set up to always check jobs and automatically restart, even after a server failure, but that's a different discussion with lots of solutions...such as those shown here.

eResourcesInc
  • 948
  • 1
  • 9
  • 17
  • Okay I see! Actually, I'm using Redis for Queueing. Forgot to tell it. So you would recommend to queue it with database? – Philipp Mochine Sep 18 '18 at 12:03
  • 1
    There are benefits to both but if reliability is your top priority you might consider the switch. Even with that said, I don’t think you will have any issue using Redis. You could also create your own manual database table that monitors each email you send and makes sure that each one eventually gets sent. – eResourcesInc Sep 18 '18 at 12:16
1

If you're using database driver with Laravel queues to process your email then you don't need to worry about anything.

Jobs are only removed from Jobs table if they are successfully completed otherwise their next attempt time is set which is few minutes in future and they are executed again (if your queue worker is online).

So its completely safe to use Laravel queues

Waleed
  • 1,097
  • 18
  • 45