emmy-engine 0.2.6 → 0.2.8

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: fa1990028500d30ba25d45be63ffcb856b5e9519
4
- data.tar.gz: b7ff2fb12a960c81d683818ab97ef728a0c2e938
3
+ metadata.gz: 9b287cff45993922a60dd74dd1fe309d6683181b
4
+ data.tar.gz: 000276b49e6c8f1679ca5e20f8f55ce81a776703
5
5
  SHA512:
6
- metadata.gz: e070764e6a01891301d125cdfdaf30e06d77680f2cbfd23d90d8c7445edf27ff9e62c4b24402865aa7abd59ece6fe758bebe17b79dca43f255af18af0c8d68ef
7
- data.tar.gz: eb2fb3785e56a923693fba2e4568622fb1b361ca346f364d93fd314a72e91ad43d02d67b110bdcced22ca419fbdbb721649ed3447b02aeea4726c8edd9ca66ab
6
+ metadata.gz: e6ed1fd123c0676a7072cee623e0b1f1f4af8008999088501874ad18954318d9c7806588bbd795a57428d54a695fa95393465785fc7f000ff644bd1b3395810f
7
+ data.tar.gz: 4d8a2a6a5439d610bbe427eaa4e75866ec23ef71a8dfe34666aff4e27c8b3672ba93dff34908bf41e0128112ab70d1f6d15b2a9ac23ca8f3332f199eb3f28446
data/emmy-engine.gemspec CHANGED
@@ -1,8 +1,8 @@
1
1
  # coding: utf-8
2
2
  lib = File.expand_path('../lib', __FILE__)
3
3
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
-
5
4
  require 'emmy/version'
5
+
6
6
  version = File.read(File.expand_path('../../EMMY_VERSION', __FILE__)).strip
7
7
  if version != Emmy::VERSION
8
8
  puts "Different version numbers"
data/lib/emmy.rb CHANGED
@@ -8,8 +8,15 @@ module Emmy
8
8
  include EventObject
9
9
  include Fibre::Synchrony
10
10
 
11
- autoload :Http, 'emmy/http'
12
- autoload :Runner, 'emmy/runner'
11
+ autoload :Http, 'emmy/http'
12
+ autoload :Backend, 'emmy/backend'
13
+ autoload :Runner, 'emmy/runner'
14
+
15
+ module_function
16
+
17
+ def env
18
+ Emmy::Runner.instance.config.environment
19
+ end
13
20
  end
14
21
 
15
22
  require 'emmy/version'
@@ -0,0 +1,25 @@
1
+ module Emmy
2
+ module Backend
3
+ extend EmmyMachine::ClassMethods
4
+ using Emmy
5
+
6
+ module_function
7
+
8
+ attr_reader :apps
9
+
10
+ def app(name: nil, &b)
11
+ @apps ||= {}
12
+ name ||= Emmy::Runner.instance.config.backend
13
+
14
+ if b
15
+ app = EmmyHttp::Application.new
16
+ app.instance_eval(&b)
17
+ @apps[name] = app
18
+ else
19
+ @apps[name]
20
+ end
21
+ end
22
+
23
+ #<<<
24
+ end
25
+ end
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 :parse
7
+ events :init, :parse
8
8
 
9
9
  RUBY = Gem.ruby
10
10
  BIN_EMMY = "bin/emmy"
@@ -33,6 +33,9 @@ module Emmy
33
33
  on :parse do
34
34
  update_rack_environment!
35
35
  end
36
+ on :init do
37
+ initialize!
38
+ end
36
39
  end
37
40
 
38
41
  def execute_bin_emmy
@@ -62,8 +65,8 @@ module Emmy
62
65
  "Default: #{config.url.host}") { |address| config.url.host = address }
63
66
  opts.on("-b", "--backend NAME", "Backend name",
64
67
  "Default: backend") { |name| config.backend = name }
65
- opts.on("-d", "--daemonize", "Runs server in the background") { @action = :daemonize_server }
66
- opts.on("-s", "--silence", "Logging disabled") { config.logging = false }
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; }
67
70
  # actions
68
71
  opts.on("-i", "--info", "Shows server configuration") { @action = :show_configuration }
69
72
  opts.on("-c", "--console", "Start a console") { @action = :start_console }
@@ -78,14 +81,23 @@ module Emmy
78
81
  config.group = "worker"
79
82
  end
80
83
 
81
- config.pid ||= "#{config.backend}.pid"
82
- config.log ||= "#{config.backend}.log"
83
84
  if config.environment == "development"
84
85
  config.stdout = "#{config.backend}.stdout"
85
86
  config.stderr = config.stdout
86
87
  end
87
88
  end
88
89
 
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"
98
+ end
99
+ end
100
+
89
101
  def run_action
90
102
  # Run parsers
91
103
  parse!
@@ -95,21 +107,48 @@ module Emmy
95
107
  end
96
108
 
97
109
  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
+
98
118
  Process.fork do
99
119
  Process.setsid
100
- exit if fork
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)
101
128
 
102
129
  scope_pid(Process.pid) do |pid|
103
130
  puts pid
104
- File.umask(0000) # rw-rw-rw-
131
+ File.umask(0000)
105
132
  bind_standard_streams
106
- start_server
133
+ run
107
134
  end
108
135
  end
109
136
  end
110
137
 
111
138
  def start_server
112
- load backend_file
139
+ init.fire_for(self)
140
+ run
141
+ end
142
+
143
+ def run
144
+ Emmy.run do
145
+ trap("INT") { Emmy.stop }
146
+ trap("TERM") { Emmy.stop }
147
+
148
+ Emmy.fiber_block do
149
+ Backend.module_eval(File.read(backend_file), backend_file)
150
+ end
151
+ end
113
152
  end
114
153
 
115
154
  def start_console
@@ -120,12 +159,13 @@ module Emmy
120
159
  require 'irb'
121
160
  require 'irb/completion'
122
161
 
123
- IRB.start
162
+ IRB.start
124
163
  end
125
164
  end
126
165
  end
127
166
 
128
167
  def show_configuration
168
+ init.fire_for(self)
129
169
  puts "Server configuration:"
130
170
  config.attributes.each do |name, value|
131
171
  value = "off" if value.nil?
@@ -146,13 +186,17 @@ module Emmy
146
186
  exit
147
187
  end
148
188
 
189
+ def configure(&b)
190
+ on :init, &b
191
+ end
192
+
149
193
  private
150
194
 
151
195
  def backend_file
152
196
  thin = (config.backend == 'backend') ? EmmyExtends::Thin::EMMY_BACKEND : nil rescue nil
153
197
  backends = [
154
- "#{Dir.getwd}/#{config.backend}.rb",
155
- "#{Dir.getwd}/config/#{config.backend}.rb",
198
+ "#{Dir.getwd}/#{config.backend}.em",
199
+ "#{Dir.getwd}/config/#{config.backend}.em",
156
200
  thin
157
201
  ].compact
158
202
 
@@ -164,6 +208,7 @@ module Emmy
164
208
 
165
209
  def scope_pid(pid)
166
210
  FileUtils.mkdir_p(File.dirname(config.pid))
211
+ stop_pid(File.read(config.pid).to_i) if File.exists?(config.pid)
167
212
  File.open(config.pid, 'w') { |f| f.write(pid) }
168
213
  if block_given?
169
214
  yield pid
@@ -171,6 +216,18 @@ module Emmy
171
216
  end
172
217
  end
173
218
 
219
+ def stop_pid(pid)
220
+ unless pid.zero?
221
+ Process.kill("TERM", pid)
222
+ puts "Restarting..."
223
+ while File.exists?(config.pid)
224
+ sleep(0.1)
225
+ end
226
+ #Process.wait(pid)
227
+ end
228
+ rescue
229
+ end
230
+
174
231
  def delete_pid
175
232
  File.delete(config.pid)
176
233
  end
data/lib/emmy/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Emmy
2
- VERSION = "0.2.6"
2
+ VERSION = "0.2.8"
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.6
4
+ version: 0.2.8
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-12 00:00:00.000000000 Z
11
+ date: 2015-05-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: emmy-machine
@@ -124,6 +124,7 @@ files:
124
124
  - bin/emmy
125
125
  - emmy-engine.gemspec
126
126
  - lib/emmy.rb
127
+ - lib/emmy/backend.rb
127
128
  - lib/emmy/http.rb
128
129
  - lib/emmy/runner.rb
129
130
  - lib/emmy/version.rb