Class: Emmy::Runner

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/emmy/runner.rb

Constant Summary collapse

RUBY =
Gem.ruby
BIN_EMMY =
"bin/emmy"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRunner

Returns a new instance of Runner.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/emmy/runner.rb', line 18

def initialize
  @argv = ARGV
  @env  = ENV
  @config = EmmyHttp::Configuration.new
  @action = :start_server

  on :bootstrap do
    parse_environment!
  end
  on :bootstrap do
    defaults!
  end

  on :instance do |id|
    instance_defaults!(id)
  end

  on :parse_options do
    option_parser.parse!(argv)
  end
  on :parse_options do
    update_rack_environment!
  end
end

Instance Attribute Details

#actionObject

Returns the value of attribute action.



15
16
17
# File 'lib/emmy/runner.rb', line 15

def action
  @action
end

#argvObject

Returns the value of attribute argv.



12
13
14
# File 'lib/emmy/runner.rb', line 12

def argv
  @argv
end

#configObject

Returns the value of attribute config.



14
15
16
# File 'lib/emmy/runner.rb', line 14

def config
  @config
end

#envObject

Returns the value of attribute env.



13
14
15
# File 'lib/emmy/runner.rb', line 13

def env
  @env
end

#option_parserObject

Returns the value of attribute option_parser.



16
17
18
# File 'lib/emmy/runner.rb', line 16

def option_parser
  @option_parser
end

Instance Method Details

#bootstrap!Object



204
205
206
# File 'lib/emmy/runner.rb', line 204

def bootstrap!
  bootstrap.instance_fire(self)
end

#configure(&b) ⇒ Object



196
197
198
# File 'lib/emmy/runner.rb', line 196

def configure(&b)
  on :bootstrap, &b
end

#daemonize_serverObject



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/emmy/runner.rb', line 122

def daemonize_server
  each_server do
    Process.fork do
      Process.setsid
      exit if fork

      Fibre.reset
      # Boot instance
      instance!

      scope_pid(Process.pid) do |pid|
        puts pid
        File.umask(0000)
        bind_standard_streams
        start_server
      end
    end
  end
  sleep(1)
end

#defaults!Object



93
94
95
96
97
98
99
100
101
102
103
# File 'lib/emmy/runner.rb', line 93

def defaults!
  if config.environment == "development"
    config.stdout = "#{config.backend}.stdout"
    config.stderr = config.stdout
  end

  config.pid = "#{config.backend}.pid"
  config.log = "#{config.backend}.log"

  config.adapter ||= EmmyExtends::Thin::Controller rescue nil
end

#display_helpObject



183
184
185
# File 'lib/emmy/runner.rb', line 183

def display_help
  puts option_parser
end

#display_versionObject



187
188
189
# File 'lib/emmy/runner.rb', line 187

def display_version
  puts Emmy::VERSION
end

#each(&b) ⇒ Object



200
201
202
# File 'lib/emmy/runner.rb', line 200

def each(&b)
  on :instance, &b
end

#error(message) ⇒ Object



191
192
193
194
# File 'lib/emmy/runner.rb', line 191

def error(message)
  puts message
  exit
end

#execute_bin_emmyObject



43
44
45
46
47
# File 'lib/emmy/runner.rb', line 43

def execute_bin_emmy
  return false unless File.file?(BIN_EMMY)
  exec RUBY, BIN_EMMY, *argv
  true
end

#instance!Object



208
209
210
# File 'lib/emmy/runner.rb', line 208

def instance!
  instance.instance_fire(self, config.id)
end

#instance_defaults!(id) ⇒ Object



105
106
107
108
109
110
111
# File 'lib/emmy/runner.rb', line 105

def instance_defaults!(id)
  if config.id
    config.url.port += id if config.servers
    config.pid  = "#{config.backend}#{id}.pid"
    config.log  = "#{config.backend}#{id}.log"
  end
end

#parse_environment!Object



49
50
51
# File 'lib/emmy/runner.rb', line 49

def parse_environment!
  config.environment = env['EMMY_ENV'] || env['RACK_ENV'] || 'development'
end

#run_actionObject



113
114
115
116
117
118
119
120
# File 'lib/emmy/runner.rb', line 113

def run_action
  # Bootstrap
  bootstrap! if @action != :display_help
  parse_options!
  # start action
  send(action)
  self
end

#show_configurationObject



175
176
177
178
179
180
181
# File 'lib/emmy/runner.rb', line 175

def show_configuration
  puts "Server configuration:"
  config.attributes.each do |name, value|
    value = "off" if value.nil?
    puts "  #{name}: #{value}"
  end
end

#start_consoleObject



162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/emmy/runner.rb', line 162

def start_console
  EmmyMachine.run_once do
    if defined?(binding.pry)
      TOPLEVEL_BINDING.pry
    else
      require 'irb'
      require 'irb/completion'

      IRB.start
    end
  end
end

#start_serverObject



143
144
145
146
147
148
149
150
151
152
153
# File 'lib/emmy/runner.rb', line 143

def start_server
  Emmy.run do
    trap("INT")  { Emmy.stop }
    trap("TERM") { Emmy.stop }

    Emmy.fiber_block do
      file = backend_file
      Backend.module_eval(File.read(file), file)
    end
  end
end

#stop_serverObject



155
156
157
158
159
160
# File 'lib/emmy/runner.rb', line 155

def stop_server
  each_server do
    instance!
    stop_pid(config.pid)
  end
end

#update_rack_environment!Object



53
54
55
# File 'lib/emmy/runner.rb', line 53

def update_rack_environment!
  ENV['RACK_ENV'] = config.environment
end