예제 #1
0
 public static function lock($jobName)
 {
     $lockFile = LOCK_DIR . $jobName . LOCK_SUFFIX;
     if (file_exists($lockFile)) {
         // Is running?
         self::$pid = file_get_contents($lockFile);
         if (self::isRunning()) {
             error_log("==" . self::$pid . "== Already in progress...");
             return FALSE;
         } else {
             error_log("==" . self::$pid . "== Previous job died abruptly...");
         }
     }
     self::$pid = getmypid();
     file_put_contents($lockFile, self::$pid);
     error_log("==" . self::$pid . "== Lock acquired, processing the job...");
     return self::$pid;
 }
예제 #2
0
 /**
  * Dispatches the cron jobs
  *
  * @param int $
  */
 public function dispatch($jobKey = null)
 {
     $register = new Ot_Cron_JobRegister();
     $cs = new Ot_Model_DbTable_CronStatus();
     if (!is_null($jobKey)) {
         $thisJob = $register->getJob($jobKey);
         if (is_null($jobKey)) {
             throw new Exception('Job not found');
         }
         if (!$cs->isEnabled($thisJob->getKey())) {
             throw new Ot_Exception('Job is disabled and cannot be run');
         }
         $lastRunDt = $cs->getLastRunDt($thisJob->getKey());
         $jobObj = $thisJob->getJobObj();
         // make sure job isn't already running
         if (($pid = Ot_Cron_Lock::lock($thisJob->getKey())) == FALSE) {
             return;
         }
         $jobObj->execute($lastRunDt);
         Ot_Cron_Lock::unlock($thisJob->getKey());
         $cs->executed($thisJob->getKey(), time());
         return;
     }
     $now = time();
     $jobs = $register->getJobs();
     foreach ($jobs as $job) {
         if ($this->shouldExecute($job->getSchedule(), $now)) {
             if (!$cs->isEnabled($job->getKey())) {
                 continue;
             }
             $lastRunDt = $cs->getLastRunDt($job->getKey());
             $jobObj = $job->getJobObj();
             if (($pid = Ot_Cron_Lock::lock($job->getKey())) == FALSE) {
                 continue;
             }
             $jobObj->execute($lastRunDt);
             Ot_Cron_Lock::unlock($job->getKey());
             $cs->executed($job->getKey(), time());
         }
     }
 }