Run a task at a given time

Asked

Viewed 372 times

1

I’m developing a system of deadlines and alerts. The user will register a deadline, date and time of the deadline and define if he wants to receive the alert 1, 2 or 3 hours in advance.

How could I perform a function that keeps checking and it’s time to send the email to the user? I didn’t want to make a CRON to run every minute and check if it’s time to send the email.

Is there any way to create an automatic CRON for it to run only when sending the email to the user.

  • 1

    Running only on time involves checking whether it’s already time or not (you can’t get away from it, whether it’s cron or not)... A cron would be the best solution in this case, no problem if it is minute to minute. https://laravel.com/docs/5.6/scheduling

  • 2

    Anyway you should check from time to time. Otherwise how will you know it’s already time ? Use cron

  • Leave a Schedule running every minute and check the database to see if it’s time to send an email, it wouldn’t weigh the server?

  • You can use the Command at (Linux) for the script to be executed at a given time. If I’m not mistaken, you can run Command using shell_exec and it will run only once at the given time. However, I never used it via PHP, only via CLI.

  • @Gabrielheming this command and as this link itself indicates, this is similar to cron... And no doubt you will have to check from time to time if it is time to run the program. And it is also obsolete as it says on this link.

  • @Miguel I think we read different things, I researched here, here and here and all the information you have reported now is for windows ([...]but is now deprecated in favor of schtasks.exe[...]). I haven’t located anywhere that’s obsolete (Unix-Like systems) and even if it is processed minute/minute, just like cron, there will be no overhead of running the PHP script, that is, it is less expensive.

  • Yes @Gabrielheming, you’re right it was my distraction, I saw windows and assumed it was windows. Of any the check is done, it has to be

  • Yes @Miguel, but no overhead, because the validation PHP code (if it’s time to execute) will be executed only once. In other words, it will cost less to the server. I am far from my linux server to make an example, when I arrive in case I will (if no one has answered yet). Here is more information about at and variants: https://unix.stackexchange.com/questions/97882/why-do-we-need-the-at-command-in-linux

  • Yes, exact @Gabrielheming, the script is not even called if it’s not time.

Show 4 more comments

1 answer

0


I believe that running the CRON every minute (or the minimum multiple of minutes your scheduling allows) is not going to be a problem, since the job will only check and only perform the task actually if you need to. Optimize well this task of checking and returning fast if there is no fault that everything is at home.

Do not forget to check whether you will want to have overlap or not. Overlap is when a job takes too long to perform and the next minute arrives by firing a next job in parallel. You may or may not want this.

Remember to always go the simple way it works (each project has its context), leave to do super optimizations only if you have verified performance issues.

  • I use Queues on the Laravel and see that, after performing a task, it deletes the record from the job table. For the consultation not take long, I will have to do the same on the schedule.

Browser other questions tagged

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