emmy-engine 0.2.8 → 0.2.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9b287cff45993922a60dd74dd1fe309d6683181b
4
- data.tar.gz: 000276b49e6c8f1679ca5e20f8f55ce81a776703
3
+ metadata.gz: 7de6c9da1251c209b2a72734a97f4f2e6714ab77
4
+ data.tar.gz: d232969593cd5abf02d31fc18755d8243e9e4208
5
5
  SHA512:
6
- metadata.gz: e6ed1fd123c0676a7072cee623e0b1f1f4af8008999088501874ad18954318d9c7806588bbd795a57428d54a695fa95393465785fc7f000ff644bd1b3395810f
7
- data.tar.gz: 4d8a2a6a5439d610bbe427eaa4e75866ec23ef71a8dfe34666aff4e27c8b3672ba93dff34908bf41e0128112ab70d1f6d15b2a9ac23ca8f3332f199eb3f28446
6
+ metadata.gz: 507fdcd91c0488c3f02ee06a6cfc64039e2f704cdb2708ed3767cba1f54dd40620a2ef20924e7c58aee8c56ddbe4e3eab9acb2032947ebd29550c91ed83ba154
7
+ data.tar.gz: c4c7f93925386fe5cc74108b2ac8e3d6265c3f9c2bf89c5018285239bf1ca9dc0f2eb2e8020a3563be1e6689c1cbd19dd106fe2e3113fc1e66d6a275ecffb03b
data/lib/emmy.rb CHANGED
@@ -15,7 +15,11 @@ module Emmy
15
15
  module_function
16
16
 
17
17
  def env
18
- Emmy::Runner.instance.config.environment
18
+ env.config.environment
19
+ end
20
+
21
+ def runner
22
+ Emmy::Runner.instance
19
23
  end
20
24
  end
21
25
 
data/lib/emmy/runner.rb CHANGED
@@ -4,7 +4,7 @@ module Emmy
4
4
  class Runner
5
5
  include Singleton
6
6
  using EventObject
7
- events :init, :parse
7
+ events :bootstrap, :instance
8
8
 
9
9
  RUBY = Gem.ruby
10
10
  BIN_EMMY = "bin/emmy"
@@ -21,20 +21,20 @@ module Emmy
21
21
  @config = EmmyHttp::Configuration.new
22
22
  @action = :start_server
23
23
 
24
- on :parse do
24
+ on :bootstrap do
25
25
  parse_environment!
26
26
  end
27
- on :parse do
27
+ on :bootstrap do
28
28
  option_parser.parse!(argv)
29
29
  end
30
- on :parse do
30
+ on :bootstrap do
31
31
  defaults!
32
32
  end
33
- on :parse do
33
+ on :bootstrap do
34
34
  update_rack_environment!
35
35
  end
36
- on :init do
37
- initialize!
36
+ on :instance do |id|
37
+ instance_defaults!(id)
38
38
  end
39
39
  end
40
40
 
@@ -66,10 +66,20 @@ module Emmy
66
66
  opts.on("-b", "--backend NAME", "Backend name",
67
67
  "Default: backend") { |name| config.backend = name }
68
68
  opts.on("-d", "--daemonize", "Runs server in the background") { @action = :daemonize_server }
69
- opts.on("-s", "--servers NUM", "Number of servers to start") { |num| @action = :daemonize_server; config.servers = num.to_i; }
69
+ opts.on("-s", "--servers NUM", "Number of servers to start") do |num|
70
+ @action = :daemonize_server if @action == :start_server
71
+ config.servers = num.to_i
72
+ end
73
+ opts.on('', '--id NUM', "Server identifier") { |id| config.id = id.to_i }
74
+ #opts.on('-l', '--log FILE', "Log to file") { |file| config.log = file }
75
+ #opts.on('-o', '--output FILE', "Logs stdout to file") { |file| config.stdout = config.stderr = file }
76
+ #opts.on('-P', '--pid FILE', "Pid file") { |file| config.pid = file }
77
+
70
78
  # actions
79
+ opts.separator "Actions:"
71
80
  opts.on("-i", "--info", "Shows server configuration") { @action = :show_configuration }
72
81
  opts.on("-c", "--console", "Start a console") { @action = :start_console }
82
+ opts.on('-t', '--stop', "Terminate background server") { @action = :stop_server }
73
83
  opts.on("-h", "--help", "Display this help message") { @action = :display_help }
74
84
  opts.on("-v", "--version", "Display Emmy version.") { @action = :display_version }
75
85
  end
@@ -85,62 +95,49 @@ module Emmy
85
95
  config.stdout = "#{config.backend}.stdout"
86
96
  config.stderr = config.stdout
87
97
  end
98
+
99
+ config.pid = "#{config.backend}.pid"
100
+ config.log = "#{config.backend}.log"
88
101
  end
89
102
 
90
- def initialize!
91
- if config.servers > 1
92
- config.url.port += config.id
93
- config.pid = "#{config.backend}#{config.id}.pid"
94
- config.log = "#{config.backend}#{config.id}.log"
95
- else
96
- config.pid = "#{config.backend}.pid"
97
- config.log = "#{config.backend}.log"
103
+ def instance_defaults!(id)
104
+ if config.id
105
+ config.url.port += id if config.servers
106
+ config.pid = "#{config.backend}#{id}.pid"
107
+ config.log = "#{config.backend}#{id}.log"
98
108
  end
99
109
  end
100
110
 
101
111
  def run_action
102
- # Run parsers
103
- parse!
112
+ # Bootstrap
113
+ bootstrap!
104
114
  # start action
105
115
  send(action)
106
116
  self
107
117
  end
108
118
 
109
119
  def daemonize_server
110
- run_next_instance
111
- end
112
-
113
- def run_next_instance
114
- if config.id >= config.servers
115
- exit
116
- end
117
-
118
- Process.fork do
119
- Process.setsid
120
- if fork
121
- config.id += 1
122
- run_next_instance
123
- end
124
-
125
- Fibre.reset
126
- # can load configuration
127
- init.fire_for(self)
128
-
129
- scope_pid(Process.pid) do |pid|
130
- puts pid
131
- File.umask(0000)
132
- bind_standard_streams
133
- run
120
+ each_server do
121
+ Process.fork do
122
+ Process.setsid
123
+ exit if fork
124
+
125
+ Fibre.reset
126
+ # Boot instance
127
+ instance!
128
+
129
+ scope_pid(Process.pid) do |pid|
130
+ puts pid
131
+ File.umask(0000)
132
+ bind_standard_streams
133
+ start_server
134
+ end
134
135
  end
135
136
  end
137
+ sleep(1)
136
138
  end
137
139
 
138
140
  def start_server
139
- init.fire_for(self)
140
- run
141
- end
142
-
143
- def run
144
141
  Emmy.run do
145
142
  trap("INT") { Emmy.stop }
146
143
  trap("TERM") { Emmy.stop }
@@ -151,6 +148,13 @@ module Emmy
151
148
  end
152
149
  end
153
150
 
151
+ def stop_server
152
+ each_server do
153
+ instance!
154
+ stop_pid(config.pid)
155
+ end
156
+ end
157
+
154
158
  def start_console
155
159
  EmmyMachine.run_block do
156
160
  if defined?(binding.pry)
@@ -165,7 +169,6 @@ module Emmy
165
169
  end
166
170
 
167
171
  def show_configuration
168
- init.fire_for(self)
169
172
  puts "Server configuration:"
170
173
  config.attributes.each do |name, value|
171
174
  value = "off" if value.nil?
@@ -187,11 +190,35 @@ module Emmy
187
190
  end
188
191
 
189
192
  def configure(&b)
190
- on :init, &b
193
+ on :bootstrap, &b
194
+ end
195
+
196
+ def each(&b)
197
+ on :instance, &b
198
+ end
199
+
200
+ def bootstrap!
201
+ bootstrap.fire_for(self)
202
+ end
203
+
204
+ def instance!
205
+ instance.fire_for(self, config.id)
191
206
  end
192
207
 
193
208
  private
194
209
 
210
+ def each_server
211
+ unless config.servers
212
+ yield
213
+ return
214
+ end
215
+
216
+ config.servers.times do |id|
217
+ config.id = id
218
+ yield
219
+ end
220
+ end
221
+
195
222
  def backend_file
196
223
  thin = (config.backend == 'backend') ? EmmyExtends::Thin::EMMY_BACKEND : nil rescue nil
197
224
  backends = [
@@ -208,7 +235,7 @@ module Emmy
208
235
 
209
236
  def scope_pid(pid)
210
237
  FileUtils.mkdir_p(File.dirname(config.pid))
211
- stop_pid(File.read(config.pid).to_i) if File.exists?(config.pid)
238
+ stop_pid(config.pid)
212
239
  File.open(config.pid, 'w') { |f| f.write(pid) }
213
240
  if block_given?
214
241
  yield pid
@@ -216,16 +243,19 @@ module Emmy
216
243
  end
217
244
  end
218
245
 
219
- def stop_pid(pid)
246
+ def stop_pid(pid_file)
247
+ return unless File.exists?(pid_file)
248
+
249
+ pid = File.read(pid_file).to_i
220
250
  unless pid.zero?
221
251
  Process.kill("TERM", pid)
222
- puts "Restarting..."
252
+ puts "Stopping..."
223
253
  while File.exists?(config.pid)
224
254
  sleep(0.1)
225
255
  end
226
256
  #Process.wait(pid)
227
257
  end
228
- rescue
258
+ rescue Errno::ESRCH
229
259
  end
230
260
 
231
261
  def delete_pid
data/lib/emmy/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Emmy
2
- VERSION = "0.2.8"
2
+ VERSION = "0.2.9"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: emmy-engine
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.8
4
+ version: 0.2.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - inre
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-05-25 00:00:00.000000000 Z
11
+ date: 2015-05-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: emmy-machine