public function checkSchedule() { $timestamp = microtime(true); $hour = date('G'); if (is_numeric($this->schedule)) { //If you need to perform at intervals, then check to see whether early to perform if (isset($this->previousTime) && $timestamp <= $this->previousTime + $this->schedule) { return; } $this->previousTime = $timestamp; } else { if (is_array($this->schedule)) { if (isset($this->previousTime) && $this->previousTime == $hour || !in_array($hour, $this->schedule)) { return; } $this->previousTime = $hour; } else { if (is_string($this->schedule)) { $nextRunDate = false; try { //See https://github.com/mtdowling/cron-expression if (!isset($this->cronExpression)) { $this->cronExpression = CronExpression::factory($this->schedule); } $nextRunDate = $this->cronExpression->getNextRunDate(); } catch (\Exception $e) { $this->terminate(); } //first step always skipped if (!isset($this->previousTime)) { $this->previousTime = $nextRunDate; } if ($this->previousTime == $nextRunDate) { return; } $this->previousTime = $nextRunDate; } else { //once if (isset($this->previousTime)) { return; } $this->previousTime = true; } } } //execute daemon now try { $this->daemon->run(); } catch (\Exception $e) { $this->terminate(); } }
/** * Returns the timestamp of the next run date. * * @return int */ public function getNextRunDate() { if ($this->nextRunDate === null) { $nextRun = $this->cronExpression->getNextRunDate(); $this->nextRunDate = $nextRun->getTimestamp(); } return $this->nextRunDate; }
/** * Check if the job is due to run * * @return bool */ public function isDue() { if ($this->lastExecutionFile && is_readable($this->lastExecutionFile)) { $lastExecution = file_get_contents($this->lastExecutionFile); $lastRunDate = $this->execution->getPreviousRunDate('now', 0, true); $nextRunDate = $this->execution->getNextRunDate(); echo (is_string($this->command) ? $this->command : '[function]') . ': ' . $lastExecution . ' | ' . $lastRunDate->getTimestamp() . ' | ' . $nextRunDate->getTimestamp() . "\n"; return $lastRunDate->getTimestamp() > $lastExecution && $this->truthTest === true; } return $this->execution->isDue() && $this->truthTest === true; }
/** * {@inheritdoc} */ public function isScheduled() { try { $now = new \DateTime('now', $this->getTimeZone()); return $this->expression->getNextRunDate($now) instanceof \DateTime; // @codeCoverageIgnoreStart } catch (\Exception $exception) { return false; } // @codeCoverageIgnoreEnd }
/** * @covers Cron\CronExpression::isDue * @covers Cron\CronExpression::getNextRunDate * @covers Cron\CronExpression::unitSatisfiesCron * @dataProvider scheduleProvider */ public function testDeterminesIfCronIsDue($schedule, $relativeTime, $nextRun, $isDue) { $cron = new CronExpression($schedule); if (is_string($relativeTime)) { $relativeTime = new \DateTime($relativeTime); } else { if (is_int($relativeTime)) { $relativeTime = date('Y-m-d H:i:s', $relativeTime); } } $this->assertEquals($isDue, $cron->isDue($relativeTime)); $this->assertEquals(new \DateTime($nextRun), $cron->getNextRunDate($relativeTime)); }
/** * {@inheritDoc} */ public function getScheduleTimes() { if (!$this->isScheduled()) { return array(); } $currentTime = new \DateTime(); try { $startTime = $this->cronExpression->getNextRunDate($currentTime); } catch (\RuntimeException $e) { return array(); } $endTime = clone $startTime; $endTime->add(new \DateInterval($this->interval)); return array('start' => $startTime, 'end' => $endTime); }
/** * Get the n futur execution * * @param int $next Nombre iteration of the cron job in the futur * * @return null|String[] */ function getNextDate($next = 5) { $next_datetime = array(); if (!$this->_cron_expression) { $this->getCronExpression(); } try { for ($i = 0; $i < $next; $i++) { $next_datetime[] = $this->_cron_expression->getNextRunDate("now", $i, true)->format('Y-m-d H:i:s'); } } catch (Exception $e) { return null; } return $this->_next_datetime = $next_datetime; }
/** * Determine if a task should be run * * @param CronTask $task * @param CronExpression $cron */ public function isTaskDue(CronTask $task, CronExpression $cron) { // Get last run status $status = CronTaskStatus::get_status(get_class($task)); // If the cron is due immediately, then run it $now = new DateTime(SS_Datetime::now()->getValue()); if ($cron->isDue($now)) { if (empty($status->LastRun)) { return true; } // In case this process is invoked twice in one minute, supress subsequent executions $lastRun = new DateTime($status->LastRun); return $lastRun->format('Y-m-d H:i') != $now->format('Y-m-d H:i'); } // If this is the first time this task is ever checked, no way to detect postponed execution if (empty($status->LastChecked)) { return false; } // Determine if we have passed the last expected run time $nextExpectedDate = $cron->getNextRunDate($status->LastChecked); return $nextExpectedDate <= $now; }
/** * @param string $currentTime * @param int $nth * @param bool|false $allowCurrentDate * @return Carbon */ public function getNextRunDate($currentTime = 'now', $nth = 0, $allowCurrentDate = false) { return Carbon::instance(parent::getNextRunDate($currentTime, $nth, $allowCurrentDate)); }