Last active
October 12, 2018 14:32
-
-
Save Caffe1neAdd1ct/b58470ce066a0d5cda048e8055a8ca4f to your computer and use it in GitHub Desktop.
Generate a crontab time table showing all the times tasks run at
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once './vendor/autoload.php'; | |
/** @todo read straight from the crontab file */ | |
$jobs = [ | |
'task1' => [ | |
'0 6 * * *' => (new \Cron\CronExpression('0 6 * * *')), | |
'45 7 * * *' => (new \Cron\CronExpression('45 7 * * *')), | |
'15 8 * * *' => (new \Cron\CronExpression('15 8 * * *')), | |
'30 8 * * *' => (new \Cron\CronExpression('30 8 * * *')), | |
'45 8 * * *' => (new \Cron\CronExpression('45 8 * * *')), | |
'00 8,12,15,18 * * *' => (new \Cron\CronExpression('00 8,12,15,18 * * *')), | |
'30 12 * * *' => (new \Cron\CronExpression('30 12 * * *')), | |
], | |
'task2' => [ | |
'*/20 9,10,11,13,14,16,17,19,20 * * *' => (new \Cron\CronExpression('*/20 9,10,11,13,14,16,17,19,20 * * *')), | |
'20,40 12,15,18 * * *' => (new \Cron\CronExpression('20,40 12,15,18 * * *')), | |
], | |
'task3' => [ | |
'18 8-18/4 * * *' => (new \Cron\CronExpression('18 8-18/4 * * *')), | |
], | |
'task4' => [ | |
'35 19 * * *' => (new \Cron\CronExpression('35 19 * * *')), | |
], | |
'task5' => [ | |
'10,30,50 9,10,11,13,14,15,17,18,19 * * *' => (new \Cron\CronExpression('10,30,50 9,10,11,13,14,15,17,18,19 * * *')), | |
'10,30 16,20 * * *' => (new \Cron\CronExpression('10,30 16,20 * * *')), | |
'10,35 12 * * *' => (new \Cron\CronExpression('10,35 12 * * *')), | |
], | |
'task6' => [ | |
'50 8,12,16,20 * * *' => (new \Cron\CronExpression('50 8,12,16,20 * * *')), | |
] | |
]; | |
$times = []; | |
/** Create a period of minutes for a full day */ | |
$begin = (new DateTime())->setTimezone(new DateTimeZone('Europe/London'))->setTime(0, 0, 0); | |
$end = (new DateTime())->setTimezone(new DateTimeZone('Europe/London'))->setTime(23, 59, 59); | |
$interval = DateInterval::createFromDateString('1 minute'); | |
$periods = new DatePeriod($begin, $interval, $end); | |
foreach ($jobs as $jobName => $jobTimes) { | |
\array_walk($jobTimes, function($jobTime, $jobIndex) use ($periods, $jobName, &$times) { | |
$previousNextJobTime = false; | |
foreach ($periods as $period) { | |
$period = $period->setTimezone(new DateTimeZone('Europe/London')); | |
/* @var $period DateTime */ | |
/* @var $jobTime \Cron\CronExpression */ | |
$currentNextJobTime = (new \DateTime("@" . $jobTime->getNext($period)))->setTimezone(new DateTimeZone('Europe/London')); | |
/** Skip periods between current period selection and the last next job calculated to run */ | |
if($previousNextJobTime && $period < $previousNextJobTime) { | |
continue; | |
} | |
/** Skip previous next job time and current next job time if they are the same calulation */ | |
if($previousNextJobTime && $previousNextJobTime == $currentNextJobTime) { | |
continue; | |
} | |
$endOfDayCutoff = (new DateTime())->setTime(23, 59, 59)->setTimezone(new DateTimeZone('Europe/London')); | |
/** ignore if we've gone over the cutoff time */ | |
if($currentNextJobTime > $endOfDayCutoff) { | |
continue; | |
} | |
$times[$jobName][$jobIndex] = $currentNextJobTime->format('Y-m-d H:i:s'); | |
echo $jobName . '|' . $jobIndex . '|' . $currentNextJobTime->format('Y-m-d H:i:s') . PHP_EOL; | |
$previousNextJobTime = $currentNextJobTime; | |
} | |
reset($periods); | |
}); | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "vendor/crontab-analyser", | |
"description": "Description of project crontab-analyser.", | |
"authors": [ | |
{ | |
"name": "Kevin Andrews", | |
"email": "[email protected]" | |
} | |
], | |
"require": { | |
"poliander/cron": "^2.0" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment