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

Checking for missed cron job? #37

Closed
tcarpenter2014 opened this issue Sep 11, 2014 · 7 comments
Closed

Checking for missed cron job? #37

tcarpenter2014 opened this issue Sep 11, 2014 · 7 comments

Comments

@tcarpenter2014
Copy link

Hi,
Is there a way using cron.afterRun and $inTime to check for a job not being run when it was supposed to? For example, I have the a job that gets added in a cron.collectJobs listener that is supposed to run everyday at a certain time and it just so happens that server reboot was required or maybe Laravel was down at that time. Would it be possible to detect that the cron job was not executed in the cron.afterRun so that I can execute the command that was supposed to be previously executed? If so, could you show a code example?
Thanks in advance,
Troy

@liebig
Copy link
Owner

liebig commented Sep 12, 2014

Hi Troy,
Yes you can. Please test this:

Event::listen('cron.afterRun', function($rundate, $inTime, $runtime, $errors, $crons, $lastrun){
    if($inTime === -1) {
        // No previous Cron runs found, do you run Cron the first time?
    } elseif ($inTime) {
        // Cron was called in time, so don't worry
    } else {
        // Whoops, Cron is not in time! You need to check this. In this block you can for example log this error or write a mail
    }
});

@liebig liebig self-assigned this Sep 12, 2014
@tcarpenter2014
Copy link
Author

Will the $crons array return a list of all the jobs that where supposed to run and didn't if $inTime is false or will it return a list of all the jobs I have added to cron? If $inTime is false I would really need cron.afterRun to return the job names that were supposed to run but didn't because the system was unavailable to run it at the time it was supposed to.

@tcarpenter2014
Copy link
Author

In looking through the code I noticed that you do retrieve the last date the Cron was run in $lastRun variable. I think that could be a helpful parameter if it was passed to the cron.afterRun. That way when $inTime was false I could do a comparison to see how much time has elapsed since the last Cron run and determinations can be made from there how to proceed with that information.

The issue is that I have a command that gets ran at a certain time of the day and sends out a text message when the Cron job executes. If this Cron job is not executed because the job was not ran I would need to make that determination in the cron.afterRun event I would want to call the command from that cron.afterRun event. If I have the previous run date and time to compare against the current run date and time I can then determine if the job that sends the text messages was supposed to be run within the time frame since the last Cron job was run.

liebig added a commit that referenced this issue Sep 16, 2014
Fixing cron.afterRun event to pass the parameters to the listener as values and not as array
Adding test case for the new cron.afterRun event and fixing the old one
@liebig
Copy link
Owner

liebig commented Sep 16, 2014

I added the $lastRun parameter to the cron.afterRun event. Please test.

@tcarpenter2014
Copy link
Author

Since $lastrun does not get passed every time to the afterRun event you might want to update your documentation to look like this for the event:

Event::listen('cron.afterRun', function($rundate, $inTime, $runtime, $errors, $crons, $lastrun = null){

});

If not setup this way then the Laravel log will fill up with missing argument exceptions really quick.

Thanks for all your help. The enhancement works very well.

@liebig
Copy link
Owner

liebig commented Sep 19, 2014

You are right, Troy. It is not a good solution to add the $lastRun parameter to the event only if there was a Cron run before. I changed this with commit e894e8e, updated the test cases and corrected the readme file at the events section. Now you always get this event parameter but it could be an empty array. Please test again. Thank you for improving Cron.

@liebig
Copy link
Owner

liebig commented Sep 29, 2014

Closed due to inactivity.

@liebig liebig closed this as completed Sep 29, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants