Skip to content

Instantly share code, notes, and snippets.

Created December 27, 2012 19:48
Show Gist options
  • Save anonymous/4391393 to your computer and use it in GitHub Desktop.
Save anonymous/4391393 to your computer and use it in GitHub Desktop.
#em.rb
#main process
require 'eventmachine'
require 'set'
$jobs = Set.new
module Process
@connection
def initialize connection
@connection = connection
$jobs << self.get_pid
end
def post_init
end
def receive_data data
@connection.send_data data
end
def unbind
@connection.send_data "rc #{get_status.exitstatus}"
$jobs.delete self.get_pid
end
end
module Connection
def post_init
end
def start_process args
EventMachine.popen "ruby ./test.rb #{args.join ' '}", Process, self
end
def receive_data data
send_data "echo: #{data}"
close_connection if data =~ /quit/i
if data =~ /launch/i
start_process data.split(" ")[1..-1]
end
end
def unbind
end
end
EventMachine.run {
EventMachine.start_server "127.0.0.1", 8080, Connection
}
#test_job.rb
# a job / script
ARGV.each {
|a|
puts "arg: #{a}"
$stdout.flush
sleep 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment