Scheduling tasks in Laravel

Asked

Viewed 882 times

1

I am working on a medium-sized project, is one of the functions is to send SMS to system customers. At the Laravel we have how to use the new cron to schedule a task to be run every 10 minutes or every week and so on!

My problem!

I need to send an SMS to the client as soon as he registers and within my Dashboard Admin can choose how long after that SMS will be sent

That is, if the administrator set that the SMS will be sent one hour after the registration! Every time a customer registers I have to perform this task an hour later!

If a customer registers 12:00 I send 13:00 If another sign up 13:30 I send 14:30

How to make this type of scheduling in the Standard?

2 answers

1

What can you do,

Create a Scheduler run every minute (or another longer interval), calling a method of SMSQueues (for example).

This method would basically be a Select that would search based on the time of Schedule execution, the sms that should be sent, and if return some information send to the recipients.

Already at the time of registration, the client will register normally, but in the method of registration you will also insert in the table of sms_queues (following the same logic of the class above) the time the SMS must be sent, this time will be based on the current time sum + the time set by the administrator.

I think this will solve your problem.

0


The answer of Bulfaitelo is a possible solution and another would be to delay the processing of a Job. It’s a solution that requires little system intervention that you might have developed, as long as you’re already using Queues.

According to the documentation (https://laravel.com/docs/queues#delayed-dispatching), you can define how long after the system should run a Job. So, imagining that you have a Job called ProcessNewRegisterSmsNotification you can do the following immediately after the method that registers the user:

ProcessNewRegisterSmsNotification::dispatch($user)->delay(now()->addHour());

Where has now()->addHour() you can replace with the value that the administrator set in backoffice.

  • Thank you José! I believe this is the best solution, I did a little test here and it worked! I’ll put it into production to see if there’s any problem with high volumes!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.