コード例 #1
0
ファイル: crawl_daemon.php プロジェクト: yakar/yioop
 /**
  * Used to send a message the given daemon or run the program in the
  * foreground.
  *
  * @param array $argv an array of command line arguments. The argument
  *     start will check if the process control functions exists if these
  *     do they will fork and detach a child process to act as a daemon.
  *     a lock file will be created to prevent additional daemons from
  *     running. If the message is stop then a message file is written to
  *     tell the daemon to stop. If the argument is terminal then the
  *     program won't be run as a daemon.
  * @param string $name the prefix to use for lock and message files
  * @param bool $exit_type whether this function should exit or return
  *     by default a lock file is only written if exit (this allows
  *     both queue server processes (Indexer and Scheduler) to use the
  *     same lock file
  */
 static function init($argv, $name, $exit_type = 1)
 {
     self::$name = $name;
     if (isset($argv[2]) && $argv[2] != "none") {
         self::$subname = $argv[2];
     } else {
         self::$subname = "";
     }
     //don't let our script be run from apache
     if (isset($_SERVER['DOCUMENT_ROOT']) && strlen($_SERVER['DOCUMENT_ROOT']) > 0) {
         echo "BAD REQUEST";
         exit;
     }
     if (!isset($argv[1])) {
         echo "{$name} needs to be run with a command-line argument.\n";
         echo "For example,\n";
         echo "php {$name}.php start //starts the {$name} as a daemon\n";
         echo "php {$name}.php stop //stops the {$name} daemon\n";
         echo "php {$name}.php terminal //runs {$name} within the current " . "process, not as a daemon, output going to the terminal\n";
         exit;
     }
     $messages_file = self::getMesssageFileName(self::$name, self::$subname);
     switch ($argv[1]) {
         case "start":
             $options = "";
             for ($i = 3; $i < count($argv); $i++) {
                 $options .= " " . $argv[$i];
             }
             $subname = !isset($argv[2]) || $argv[2] == 'none' ? 'none' : self::$subname;
             $name_prefix = isset($argv[3]) ? $argv[3] : self::$subname;
             $name_string = CrawlDaemon::getNameString($name, $name_prefix);
             echo "Starting {$name_string}...\n";
             CrawlDaemon::start($name, $subname, $options, $exit_type);
             break;
         case "stop":
             CrawlDaemon::stop($name, self::$subname);
             break;
         case "terminal":
             self::$mode = 'terminal';
             $info = array();
             $info[self::STATUS] = self::WAITING_START_MESSAGE_STATE;
             file_put_contents($messages_file, serialize($info));
             chmod($messages_file, 0777);
             define("LOG_TO_FILES", false);
             break;
         case "child":
             self::$mode = 'daemon';
             $info = array();
             $info[self::STATUS] = self::WAITING_START_MESSAGE_STATE;
             file_put_contents($messages_file, serialize($info));
             chmod($messages_file, 0777);
             define("LOG_TO_FILES", true);
             // if false log messages are sent to the console
             break;
         default:
             exit;
             break;
     }
 }