this is my shouldrun function
public function shouldRun(DateTime $date,$last_run=null)
{
$log_filename = "C:\Siti\KemperCRM\modules\AOR_Scheduled_Reports\log";
if (!file_exists($log_filename))
{
// create directory/folder uploads.
if (!mkdir($log_filename, 0777, true)) {
die('Failed to create folders...');
};
}
$log_file_data = $log_filename.'/log_' . date('d-M-Y') . '.log';
//file_put_contents($log_file_data, print_r($date,1)."\n", FILE_APPEND);
global $timedate;
if (empty($date)) {
$date = new DateTime();
}
$cron = Cron\CronExpression::factory($this->schedule);
// file_put_contents($log_file_data,"schedule:". print_r($date,1)."\n", FILE_APPEND);
if (empty($last_run) && $cron->isDue($date)) {
return true;
}
$lastRun = $timedate->fromDb($last_run);
//file_put_contents($log_file_data,"this var dump: ". var_dump($this,1)."\n", FILE_APPEND);
// file_put_contents($log_file_data,"cron var dump: ". var_dump($cron,1)."\n", FILE_APPEND);
$next = $cron->getNextRunDate($lastRun);
/*file_put_contents($log_file_data,"date_controllo: ". print_r($date->format('Y-m-d H:i:s'),1)."\n", FILE_APPEND);
file_put_contents($log_file_data,"next_controllo: ". print_r($next->format('Y-m-d H:i:s'),1)."\n", FILE_APPEND);
file_put_contents($log_file_data,"last_run controllo: ". print_r($last_run,1)."\n", FILE_APPEND); */
$correct_last = date('Y-m-d H:i:s', strtotime('+2 hours', strtotime($last_run)));
//file_put_contents($log_file_data,"last_run correct controllo: ". print_r($correct_last,1)."\n \n \n", FILE_APPEND);
if((empty($last_run)))
return true;
if($correct_last > $next->format('Y-m-d H:i:s') )
return false;
if ($next->format('Y-m-d H:i:s') < $date->format('Y-m-d H:i:s')) {
return true;
}else{
// file_put_contents($log_file_data,"date_controllo: ". print_r($date,1)."\n", FILE_APPEND);
// file_put_contents($log_file_data,"next_controllo: ". print_r($next->date,1)."\n", FILE_APPEND);
return false;
}
return false;
}
there are some file_put_contents but actually for me works.
i had to add a new parameter “$last_run”, because the function itself give me always emty, so i had to pass the correct value by parameter