Exemplo n.º 1
0
 /**
  * Returns true if a job can run, false otherwise.
  *
  * @param string         $class          The job to check.
  * @return bool
  */
 protected function canJobRun($class)
 {
     $job_definition = $this->jobs[$class];
     if ($job_definition->getEnabled() == false) {
         return false;
     }
     // If this job is already running, don't start it again
     if (count($this->fork_daemon->work_running($class)) != 0) {
         return false;
     }
     $last_run_time = $job_definition->getLastRunTimeStart();
     // If the job is supposed to run at a scheduled time
     if (!is_null($job_definition->getRunTime())) {
         $now = new \DateTime();
         // Check if the run time is now
         if ($job_definition->getRunTime() == $now->format('H:i')) {
             // If we haven't run before, we can run
             if (is_null($last_run_time)) {
                 return true;
             } else {
                 $last_run = new \DateTime();
                 $last_run->setTimestamp($last_run_time);
                 $difference = $last_run->diff($now);
                 // If the last run wasn't this minute, we can run
                 if ($difference->days != 0 || $difference->h != 0 || $difference->i != 0) {
                     return true;
                 }
             }
         }
         return false;
     }
     // If the job runs on an interval, check if it's ready to run
     if (!is_null($job_definition->getInterval())) {
         // If it hasn't run yet, run it!
         if (is_null($last_run_time)) {
             return true;
         }
         if (time() - $last_run_time > $job_definition->getInterval()) {
             return true;
         }
     }
     // No run condition hit
     return false;
 }