Beispiel #1
0
 /**
  * デフォルト実行のメソッドになります。
  * このメソッド以外がモジュールとして呼ばれることはありません。
  *
  * @param array $params モジュールの受け取るパラメータ
  */
 public function execute($params)
 {
     // 出力バッファをリセットする。
     while (ob_get_level() > 0) {
         ob_end_clean();
     }
     Vizualizer_Logger::$logFilePrefix = "batch_";
     Vizualizer_Logger::$logOutputStandard = true;
     $this->info("Batch " . $this->getName() . " Start.");
     if ($this->getDaemonName() != "") {
         if (count($params) > 3 && $params[3] == "stop") {
             if (($fp = fopen($this->getDaemonName() . ".unlock", "w+")) !== FALSE) {
                 fclose($fp);
             }
         } elseif (($fp = fopen($this->getDaemonName() . ".lock", "a+")) !== FALSE) {
             if (!flock($fp, LOCK_EX | LOCK_NB)) {
                 list($time, $pid) = explode(",", trim(file_get_contents($this->getDaemonName() . ".lock")));
                 // 12時間以上起動し続けている場合は再起動を実施
                 if ($time + 12 * 3600 < time()) {
                     system("kill -KILL " . $pid);
                 }
                 $this->info("Batch " . $this->getName() . " was already running.");
                 die("プログラムは既に実行中です。");
             }
             // デーモンの起動時刻とプロセスIDをロックファイルに記述
             ftruncate($fp, 0);
             fwrite($fp, time() . "," . getmypid());
             if ($this->isUnlocked()) {
                 // 実行前にunlockファイルがある場合は予め削除する。
                 unlink($this->getDaemonName() . ".unlock");
             }
             while (true) {
                 Vizualizer::now()->reset();
                 $this->info("==== START " . $this->getName() . " ROUTINE ======");
                 $this->executeImpl($params);
                 $this->info("==== END " . $this->getName() . " ROUTINE ======");
                 if (file_exists($this->getDaemonName() . ".unlock")) {
                     // unlockファイルがある場合はループを終了
                     unlink($this->getDaemonName() . ".unlock");
                     break;
                 }
                 // 一周回ったら所定秒数ウェイト
                 if ($this->getDaemonInterval() > 10) {
                     sleep($this->getDaemonInterval());
                 } else {
                     sleep(60);
                 }
             }
             fclose($fp);
         }
     } else {
         if (($fp = fopen($this->getDaemonName() . ".lock", "w+")) !== FALSE) {
             if (!flock($fp, LOCK_EX | LOCK_NB)) {
                 $this->info("Batch " . $this->getName() . " was already running.");
                 die("プログラムは既に実行中です。");
             }
             $this->executeImpl($params);
             fclose($fp);
         }
     }
     $this->info("Batch " . $this->getName() . " End.");
 }