Example #1
0
 static function start()
 {
     if (!Lock::isOn('cron-running-' . $_ENV['projectName'])) {
         if (Lock::on('cron-running-' . $_ENV['projectName'])) {
             Debug::out('Starting');
             Control::req('scripts/crons/config');
             while (!Lock::isOn('cron-off-' . $_ENV['projectName'])) {
                 $time = new Time();
                 //+	ensure running only every minute {
                 $parseTime = $time->format('YmdHi');
                 if (self::$lastParsedTime == $parseTime) {
                     sleep(2);
                     continue;
                 }
                 self::$lastParsedTime = $parseTime;
                 //+	}
                 //+	execute run items {
                 $run = self::getRun($time);
                 foreach ($run as $i) {
                     $item = self::$list[$i];
                     if (self::$running[$i]) {
                         $pid = pcntl_waitpid(self::$running[$i], $status, WNOHANG);
                         if ($pid == 0) {
                             //process is currently running, don't run again
                             continue;
                         } elseif ($pid == -1) {
                             Debug::out('Process error:', $item);
                             continue;
                         }
                     }
                     $pid = pcntl_fork();
                     if ($pid == -1) {
                         Debug::quit('Could not fork process', $item);
                     } elseif ($pid) {
                         //this is the parent
                         self::$running[$i] = $pid;
                     } else {
                         //this is the child
                         self::$args = $item[2];
                         $script = Config::userFileLocation($item[1], 'control/scripts/crons');
                         Control::req($script);
                         exit;
                     }
                 }
                 //+	}
             }
             Lock::off('cron-running-' . $_ENV['projectName']);
         } else {
             Debug::quit('Failed to lock "cron-running"');
         }
     } else {
         Debug::quit('Cron already running');
     }
 }