/** * Find an open port in a given range, trying several times. * Return FALSE if no open port is found after a timeout (1 second by default) * * @param string $host * @param integer $range_start * @param integer $range_end * @param integer $timeout * @return integer|boolean */ public static function ephimeral_port($host, $range_start = 1000, $range_end = 5000, $timeout = 1000) { return Attempt::make(function () use($host, $range_start, $range_end) { $port = rand($range_start, $range_end); return Network::is_port_open($host, $port) ? $port : FALSE; }, $timeout); }
/** * Start a phantomjs server in the background. Set port, server js file, additional files and log file. * * @param string $file the server js file * @param integer $port the port to start the server on * @param string $additional additional file, passed to the js server * @param string $log_file * @return string the pid of the newly started process */ public static function start($file, $port, $additional = NULL, $log_file = '/dev/null') { if (!Network::is_port_open('localhost', $port)) { throw new Exception('Port :port is already taken', array(':port' => $port)); } if ($log_file !== '/dev/null' and !is_file($log_file)) { throw new Exception('Log file (:log_file) must be a file or /dev/null', array(':log_file' => $log_file)); } return shell_exec(strtr('nohup :command > :log 2> :log & echo $!', array(':command' => self::command($file, $port, $additional), ':log' => $log_file))); }
/** * Check if the phantomjs server is actually running (the port is taken) * @return boolean */ public function is_running() { return !Network::is_port_open($this->host(), $this->port()); }