Skip to content

Instantly share code, notes, and snippets.

@arton arton/gist:4250656
Created Dec 10, 2012

Embed
What would you like to do?
ennoumu.rb (single thread version)
# coding: utf-8
require "rbconfig"
require_relative './ennou.rb'
module Rack
module Handler
class Ennoumu < Ennou
QNAME = 'EnnouMu_Queue'
@qname = nil
@nprocs = 2
@rackup = 'rackup'
def self.config(options)
set_option(:@qname, options[:qname])
set_option(:@nprocs, options[:nprocs])
end
def self.set_option(v, s)
instance_variable_set(v, s) if s
end
def self.run(app, options = {})
setup(options)
::Ennou::Server.open(create_qname, true) do |server|
@server = server
if server.controller?
@stoprun = false
@logger.info "Ennou(#{::Ennou::VERSION}) controller pid=#{$$} start on #{RUBY_VERSION}(#{RUBY_PLATFORM})"
if @script == ''
server.add "http://#{@host}:#{@port}/"
else
server.add "http://#{@host}:#{@port}#{@script}/"
end
pids = []
cmd = "#{::File.expand_path('ruby.exe', RbConfig::CONFIG["bindir"])} #{::File.expand_path("../#{@rackup}", $0)} #{$DEBUG ? '-d' : ''} #{$VERBOSE ? '-w' : ''} #{options[:Host] == '0.0.0.0' ? '' : "-o #{@host}"} -p #{@port} -s Ennoumu \"#{options[:config]}\""
1.upto(@nprocs) do
pids << spawn(cmd)
@logger.info " spawn worker pid=#{pids.last}"
end
until @stoprun do
sleep 1
end
@logger.info "Ennou(#{::Ennou::VERSION}) controller pid=#{$$} stop"
Process.waitall
else
server.script = @script
@logger.info "script=#{server.script}, #{@script}"
@logger.info "Ennou(#{::Ennou::VERSION}) start for http://#{@host}:#{@port}#{@script} pid=#{$$}"
loop do
begin
r = server.wait(60)
next if r.nil?
run_single_thread(app, *r)
rescue Interrupt
break
end
end
@logger.info "Ennou(#{::Ennou::VERSION}) stop service for http://#{@host}:#{@port}#{@script} pid=#{$$}"
end
end
end
def self.run_single_thread(app, env, io)
env.update({'rack.version' => Rack::VERSION,
'rack.input' => io.input,
'rack.errors' => $stderr,
'rack.multithread' => true,
'rack.multiprocess' => multiprocess?,
'rack.run_once' => false,
'rack.url_scheme' => env['URL_SCHEME']
})
status, headers, body = app.call(env)
begin
io.status = status
io.headers = headers
body.each do |str|
io.write str
end
rescue
p $! if $debug
ensure
io.close
body.close if body.respond_to? :close
end
end
def self.multiprocess?
true
end
def self.shutdown
if @server.controller?
@stoprun = true
else
@server.break
@logger.info "going to shutdown ... pid=#{$$}"
end
end
private
def self.create_qname
qname = "#{QNAME}_#{(@qname.nil?) ? ((@script == '') ? @host : @script) : @qname}".gsub('/', '')
@logger.info 'EnnouMu qname=' + qname
qname
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.