Class: Vegas::Runner
- Inherits:
-
Object
- Object
- Vegas::Runner
- Defined in:
- lib/vegas/runner.rb
Constant Summary collapse
- ROOT_DIR =
File.(File.join('~', '.vegas'))
- PORT =
5678
- HOST =
WINDOWS ? 'localhost' : '0.0.0.0'
Instance Attribute Summary collapse
-
#app ⇒ Object
readonly
Returns the value of attribute app.
-
#app_name ⇒ Object
readonly
Returns the value of attribute app_name.
-
#args ⇒ Object
readonly
Returns the value of attribute args.
-
#filesystem_friendly_app_name ⇒ Object
readonly
Returns the value of attribute filesystem_friendly_app_name.
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
-
#quoted_app_name ⇒ Object
readonly
Returns the value of attribute quoted_app_name.
-
#rack_handler ⇒ Object
readonly
Returns the value of attribute rack_handler.
Class Method Summary collapse
Instance Method Summary collapse
- #announce_port_attempted ⇒ Object
- #app_dir ⇒ Object
- #check_for_running(path = nil) ⇒ Object
-
#daemonize! ⇒ Object
Adapted from Rackup.
- #find_port ⇒ Object
-
#initialize(app, app_name, set_options = {}, runtime_args = ARGV, &block) ⇒ Runner
constructor
A new instance of Runner.
- #kill! ⇒ Object
- #launch!(specific_url = nil, path = nil) ⇒ Object
-
#load_config_file(config_path) ⇒ Object
Loads a config file at config_path and evals it in the context of the @app.
- #log_file ⇒ Object
- #logger ⇒ Object
- #pid_file ⇒ Object
- #port_open?(check_url = nil) ⇒ Boolean
- #run! ⇒ Object
- #start(path = nil) ⇒ Object
- #status ⇒ Object
- #url ⇒ Object
- #url_file ⇒ Object
- #write_url ⇒ Object
Constructor Details
#initialize(app, app_name, set_options = {}, runtime_args = ARGV, &block) ⇒ Runner
Returns a new instance of Runner.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/vegas/runner.rb', line 24 def initialize(app, app_name, = {}, runtime_args = ARGV, &block) @options = || {} self.class.logger.level = [:debug] ? Logger::DEBUG : Logger::INFO @app = app @app_name = app_name @filesystem_friendly_app_name = @app_name.gsub(/\W+/, "_") @quoted_app_name = "'#{app_name}'" @runtime_args = runtime_args @rack_handler = setup_rack_handler # load options from opt parser @args = do |opts| if block_given? opts.separator '' opts.separator "#{quoted_app_name} options:" yield(self, opts, app) end end if @should_kill kill! exit!(0) end # Handle :before_run hook if (before_run = .delete(:before_run)).respond_to?(:call) before_run.call(self) end # Set app options @host = [:host] || HOST if app.respond_to?(:set) app.set() app.set(:vegas, self) end # Make sure app dir is setup FileUtils.mkdir_p(app_dir) return if [:start] == false # evaluate the launch_path path = if [:launch_path].respond_to?(:call) [:launch_path].call(self) else [:launch_path] end start(path) end |
Instance Attribute Details
#app ⇒ Object (readonly)
Returns the value of attribute app.
17 18 19 |
# File 'lib/vegas/runner.rb', line 17 def app @app end |
#app_name ⇒ Object (readonly)
Returns the value of attribute app_name.
17 18 19 |
# File 'lib/vegas/runner.rb', line 17 def app_name @app_name end |
#args ⇒ Object (readonly)
Returns the value of attribute args.
17 18 19 |
# File 'lib/vegas/runner.rb', line 17 def args @args end |
#filesystem_friendly_app_name ⇒ Object (readonly)
Returns the value of attribute filesystem_friendly_app_name.
17 18 19 |
# File 'lib/vegas/runner.rb', line 17 def filesystem_friendly_app_name @filesystem_friendly_app_name end |
#host ⇒ Object (readonly)
Returns the value of attribute host.
17 18 19 |
# File 'lib/vegas/runner.rb', line 17 def host @host end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
17 18 19 |
# File 'lib/vegas/runner.rb', line 17 def @options end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
17 18 19 |
# File 'lib/vegas/runner.rb', line 17 def port @port end |
#quoted_app_name ⇒ Object (readonly)
Returns the value of attribute quoted_app_name.
17 18 19 |
# File 'lib/vegas/runner.rb', line 17 def quoted_app_name @quoted_app_name end |
#rack_handler ⇒ Object (readonly)
Returns the value of attribute rack_handler.
17 18 19 |
# File 'lib/vegas/runner.rb', line 17 def rack_handler @rack_handler end |
Class Method Details
.logger ⇒ Object
239 240 241 242 243 244 245 246 247 |
# File 'lib/vegas/runner.rb', line 239 def self.logger @logger ||= LOGGER if defined?(LOGGER) if !@logger @logger = Logger.new(STDOUT) @logger.formatter = Proc.new {|s, t, n, msg| "[#{t}] #{msg}\n"} @logger end @logger end |
.logger=(logger) ⇒ Object
235 236 237 |
# File 'lib/vegas/runner.rb', line 235 def self.logger=(logger) @logger = logger end |
Instance Method Details
#announce_port_attempted ⇒ Object
135 136 137 |
# File 'lib/vegas/runner.rb', line 135 def announce_port_attempted logger.info "trying port #{port}..." end |
#app_dir ⇒ Object
80 81 82 |
# File 'lib/vegas/runner.rb', line 80 def app_dir [:app_dir] || File.join(ROOT_DIR, filesystem_friendly_app_name) end |
#check_for_running(path = nil) ⇒ Object
156 157 158 159 160 161 162 163 164 165 |
# File 'lib/vegas/runner.rb', line 156 def check_for_running(path = nil) if File.exists?(pid_file) && File.exists?(url_file) running_url = File.read(url_file) if !port_open?(running_url) logger.warn "#{quoted_app_name} is already running at #{running_url}" launch!(running_url, path) exit!(1) end end end |
#daemonize! ⇒ Object
Adapted from Rackup
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/vegas/runner.rb', line 181 def daemonize! if RUBY_VERSION < "1.9" logger.debug "Parent Process: #{Process.pid}" exit!(0) if fork logger.debug "Child Process: #{Process.pid}" else Process.daemon(true, true) end File.umask 0000 FileUtils.touch log_file STDIN.reopen log_file STDOUT.reopen log_file, "a" STDERR.reopen log_file, "a" logger.debug "Child Process: #{Process.pid}" File.open(pid_file, 'w') {|f| f.write("#{Process.pid}") } at_exit { delete_pid! } end |
#find_port ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/vegas/runner.rb', line 116 def find_port if @port = [:port] announce_port_attempted unless port_open? logger.warn "Port #{port} is already in use. Please try another. " + "You can also omit the port flag, and we'll find one for you." end else @port = PORT announce_port_attempted until port_open? @port += 1 announce_port_attempted end end end |
#kill! ⇒ Object
208 209 210 211 212 213 214 |
# File 'lib/vegas/runner.rb', line 208 def kill! pid = File.read(pid_file) logger.warn "Sending INT to #{pid.to_i}" Process.kill(kill_command, pid.to_i) rescue => e logger.warn "pid not found at #{pid_file} : #{e}" end |
#launch!(specific_url = nil, path = nil) ⇒ Object
202 203 204 205 206 |
# File 'lib/vegas/runner.rb', line 202 def launch!(specific_url = nil, path = nil) return if [:skip_launch] cmd = WINDOWS ? "start" : "open" system "#{cmd} #{specific_url || url}#{path}" end |
#load_config_file(config_path) ⇒ Object
Loads a config file at config_path and evals it in the context of the @app.
227 228 229 230 231 232 233 |
# File 'lib/vegas/runner.rb', line 227 def load_config_file(config_path) abort "Can not find config file at #{config_path}" if !File.readable?(config_path) config = File.read(config_path) # trim off anything after __END__ config.sub!(/^__END__\n.*/, '') @app.module_eval(config) end |
#log_file ⇒ Object
92 93 94 |
# File 'lib/vegas/runner.rb', line 92 def log_file [:log_file] || File.join(app_dir, "#{filesystem_friendly_app_name}.log") end |
#logger ⇒ Object
249 250 251 |
# File 'lib/vegas/runner.rb', line 249 def logger self.class.logger end |
#pid_file ⇒ Object
84 85 86 |
# File 'lib/vegas/runner.rb', line 84 def pid_file [:pid_file] || File.join(app_dir, "#{filesystem_friendly_app_name}.pid") end |
#port_open?(check_url = nil) ⇒ Boolean
139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/vegas/runner.rb', line 139 def port_open?(check_url = nil) begin check_url ||= url [:no_proxy] ? open(check_url, :proxy => nil) : open(check_url) false rescue Errno::ECONNREFUSED => e true rescue Errno::EPERM => e # catches the "Operation not permitted" under Cygwin true end end |
#run! ⇒ Object
167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/vegas/runner.rb', line 167 def run! logger.info "Running with Rack handler: #{@rack_handler.inspect}" rack_handler.run app, :Host => host, :Port => port do |server| trap(kill_command) do ## Use thins' hard #stop! if available, otherwise just #stop server.respond_to?(:stop!) ? server.stop! : server.stop logger.info "#{quoted_app_name} received INT ... stopping" delete_pid! end end end |
#start(path = nil) ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/vegas/runner.rb', line 100 def start(path = nil) logger.info "Running with Windows Settings" if WINDOWS logger.info "Starting #{quoted_app_name}..." begin check_for_running(path) find_port write_url launch!(url, path) daemonize! unless [:foreground] run! rescue RuntimeError => e logger.warn "There was an error starting #{quoted_app_name}: #{e}" exit end end |
#status ⇒ Object
216 217 218 219 220 221 222 223 224 |
# File 'lib/vegas/runner.rb', line 216 def status if File.exists?(pid_file) logger.info "#{quoted_app_name} running" logger.info "PID #{File.read(pid_file)}" logger.info "URL #{File.read(url_file)}" if File.exists?(url_file) else logger.info "#{quoted_app_name} not running!" end end |
#url ⇒ Object
96 97 98 |
# File 'lib/vegas/runner.rb', line 96 def url "http://#{host}:#{port}" end |
#url_file ⇒ Object
88 89 90 |
# File 'lib/vegas/runner.rb', line 88 def url_file [:url_file] || File.join(app_dir, "#{filesystem_friendly_app_name}.url") end |
#write_url ⇒ Object
152 153 154 |
# File 'lib/vegas/runner.rb', line 152 def write_url File.open(url_file, 'w') {|f| f << url } end |