Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How does it know when was the last time a job ran? #19

Closed
drupalista-br opened this issue Jul 3, 2017 · 5 comments
Closed

How does it know when was the last time a job ran? #19

drupalista-br opened this issue Jul 3, 2017 · 5 comments

Comments

@drupalista-br
Copy link

Here is my schedule.php content:

require_once __DIR__ . '/vendor/autoload.php';
use GO\Scheduler;

$scheduler = new Scheduler();
$scheduler->call(function () {
    echo "Hello";

    return " world!";
})->everyMinute()->output('/tmp/my_file.log');

$scheduler->run();

I executed $ php schedule.php. As expected, the file /tmp/my_file.log was created and had Hello World! in it.

Immediately after that I emptied out /tmp/my_file.log and ran $ php schedule.php again. Since one minute has not passed, the /tmp/my_file.log should NOT get filled with Hello World! but it did. Why did it?

@mark-win
Copy link
Contributor

mark-win commented Jul 3, 2017

To put it simple: right, it does not know, when it ran the last time. The solution is to not run it more often than once per minute.

This project relies on cron to be the only trigger but for dev reasons. Good thing is, cron won't trigger the command more often than once per minute, unless you really push it to do so.

If you really need to make sure, that your task is not run more often, then you will have to do so by your own means. Either inside of the task itself or by modifying the trigger script (schedule.php) for a more global approach.

@peppeocchi
Copy link
Owner

@drupalista-br it's like @mark-win said, there is no way (out of the box) to know when it ran the last time, but it's true that the minimum interval for a cron job is 1 minute (in a real world scenario it won't run more than once per minute).

I can suggest you two solutions based on what you need to do:

  1. if you're trying to avoid the same script to overlap, you can use the onlyOne method. That will make sure that if the script is currently being executed by the previous cron cycle (e.g. if the script takes more that 1 minute to execute), it won't run the next minute.
    $scheduler->php('script.php')->onlyOne()
  2. if you're trying to avoid the script to run in the same interval (also if not overlapping), you can write a simple solution by using the then (docs) method, keeping the last execution time (somewhere, in a db for example) and checking that on top of your script (or inside the scheduler.php file).
// Get last execution - pseudocode
$lastExecution = DB::get('last_execution')
    ->from('scripts_table')->where('name = script.php');

// If the last execution is more than one minute ago, then add the job to the scheduler
if ($lastExecution > '1 minute') {

    $scheduler->php('script.php')->then(function () {
        // After the job is executed, update the last execution time for the script
        DB::update('scripts_table')
            ->where('name = script.php')->set('last_execution = now()');
    });

}

Of course if you have any other question please feel free to reopen this issue.

@drupalista-br
Copy link
Author

Thank you guys. I am just playing around and learning. At this point I have no project requirements other than trying to understand the logic behind it.

@joshp23
Copy link

joshp23 commented Sep 23, 2019

This comes to mind, if I have a job running once daily, and it misses a day due to ... some failure or other, I would like to be able to trigger it to run at the next available interval. Is there a way to que up over-due runs?

@peppeocchi
Copy link
Owner

@joshp23 not currently, I think you'll have to build external support to keep track of the jobs that did run and if that happens you should change the schedule for the jobs that didn't run. This might be easier if you store all your jobs in a database and you have an extra routine that checks for those kind of issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants