How to convert a date into a cron expression?

Asked

Viewed 142 times

0

I am developing an application in PHP and Javascript to schedule some tasks.

The user will have the possibility to configure from a graphical interface the date of execution of the task (with the datapicker for example).

I would like to retrieve these data ( year, month, day, hour, minute and seconds ) and convert them into expressions cron.

Is there a technique or framework for that reason ?

  • 3

    you already have the full description of cron fields on the site, if you help http://answall.com/questions/124518/70 - The ideal would be a cron task that would consult a DB to see what to run, instead of you messing with cron by PHP

  • 1

    I describe this technique here: http://answall.com/a/105885/70

  • @Bacco I think your idea is correct... and how the cron would check my tables ?

  • Cron would not check your tables, cron would call an application of yours that would check and execute what was necessary. Example: cron calls a "php executa.php" every 5 min, and this PHP would check the DB and run everything it needed. PHP running through the shell is much more versatile than accessed per page, as you can use it without timeout, and as another user other than the one used on the page server.

  • @Bacco imagine that the user set a task for a date x, my application will store this data in my database. The question is : how to cron will need my BD ? and if it is to call a script to check, when will it be done ?

  • cron calls your script constantly. If you have nothing to do, your script ends. If you have something to do, your script does, and ends

  • @Bacco realized ... I will test, it seems logical and worthy of credit your idea !

  • The precision required will determine what you put in the Cron. If these are tasks that can only be done with round time, you can call PHP 1 in 1 hour only, for example, or a little less (15 in 15 min) in case when you restart the server or stop the service for some reason, it doesn’t take too long to catch up. Cron will always be calling PHP, and PHP simply checks and closes as long as there is no scheduled time.

  • @Bacco if I call my script every second, it would have a negative impact on the performance of my server ?

  • But do you have tasks that need this precision? In this case, every second is nonsense to use cron. Nor is it Cron, to tell the truth. The minimum is 1 minute. It is best to leave a PHP running in an infinite loop then, by shell (never by Apache etc), with a 1-second Sleep(). The Sleep is important not to be occupying CPU. What you have to see is that if something scheduled needs 1 second accuracy, it is a sign that maybe the application deserves to be rethought at some stage. ; Even an appointment schedule by SMS, for example, would be more than sufficient to specify 1 minute.

  • And why will you let him set it up with that level of detail? Is it something he has to do with that precision? Otherwise it would be enough to choose date and time without seconds, for example. I myself have an application that scheduling minutes is a dropdown with 0 / 5 / 10 ... only every 5 min, because it is for appointments. It’s not an alarm clock :) but of course you need to see the actual use of the application, as I don’t know the details, whatever I say is bullshit.

  • @Bacco imagine that the user configured : Receive alert on the following date : 2016-11-29 12:01:25. If a delay arises the user would never receive the alert even if it is 1 second. Therefore there is a need to check every moment.

  • So you haven’t read my post. You won’t compare =, will make comparison >=

  • @Bacco I’ll take a look and organize myself better, and as soon as I can I’ll be back !

  • If this is your reasoning, even looking at it every second, you will never be able to restart the server, but lose commitment. All scheduling assumes checking whether the date is greater or equal, and when executing, mark as executed. So even if the service goes off the air for half an hour, when it comes back it runs everything.

  • The idea is that you have a schedule table, and a field indicating whether it was executed. If the current date/time is OR HIGHER than the scheduled one, and it is not executed, you need to execute. SELECT campos FROM agendamentos WHERE executado = 0 AND current_timestamp >= agendamento

  • Exactly ... It seems quite logical to me, I’ll be!

Show 12 more comments
No answers

Browser other questions tagged

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