Skip to content

Instantly share code, notes, and snippets.

@Altech
Created October 10, 2012 14:58
Show Gist options
  • Save Altech/3866155 to your computer and use it in GitHub Desktop.
Save Altech/3866155 to your computer and use it in GitHub Desktop.
This extension enables to use block on messagepack-prc. Free variable must be an object which acccepts 'Marshal.dump'.
require 'msgpack/rpc'
require 'sourcify'
# client extension
module ClientProcHandler
def call_block(&proc)
self.call(:process_block,proc_dump(proc))
end
private
def proc_dump(proc)
local_variables = eval('local_variables',proc.binding).inject(Hash.new){|h,i|
s = Marshal.dump(eval(i.to_s,proc.binding)) rescue (next h)
h[i]= s
h
}
return proc.to_source,local_variables
end
end
# server extension
class ServerProcHandler
def process_block(dumped)
p = proc_load(*dumped)
return p.call
end
private
def proc_load(stmt, local_variables_dump)
local_variables = local_variables_dump.each_with_object(Hash.new){|(i,v),obj| obj[i.to_sym]=Marshal.load(v)}
prog = String.new
prog << local_variables.map{|i,v| "#{i}=local_variables[:#{i}]"}.join("\n")+"\n"
prog << stmt
puts prog
eval(prog)
end
end
# how to use
use_case = 1
is_server = ARGV[0] == 'server'
if is_server
# server code
server = MessagePack::RPC::Server.new
server.listen('0.0.0.0', 18800, ServerProcHandler.new)
server.run
else
# client code
case use_case
when 1 then
client = MessagePack::RPC::Client.new('localhost', 18800).extend(ClientProcHandler)
j = 22
result = client.call_block do
j*100
end
puts result # (stdout) => 2200
when 2 then
require 'td-client'
require 'mongo'
client = MessagePack::RPC::Client.new('localhost', 18800).extend(ClientProcHandler)
job = TreasureData::Client.new(API_KEY).query('database','some_hive_query')
date = Date.today
client.call_block do
job.wait
# store to database
collection = MONGODB.collection('some_collection')
job.result.each do |result|
collection.insert(score: result, date: date)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment