Created
October 10, 2012 09:33
-
-
Save Altech/3864367 to your computer and use it in GitHub Desktop.
how to send procedure by messagepack-rpc(pseudocode).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'msgpack/rpc' | |
require 'sourcify' | |
class JobExecuteClient1 | |
def do_job | |
job = CloudDatawareHouse::Client.new.query('some_query') | |
# local variables | |
type = 'one' | |
# | |
collection #<Mongo::Collection:0x...> | |
job #<CloudDatawareHouse::Job:0x...> | |
store_after_finish(job,collection) do | |
job.result.each_with_index do |value,i| | |
keyword, count = value | |
collection.insert(keyword: keyword, sum: sum, order: i+1, type: type) | |
end | |
end | |
end | |
def store_after_finish(job,collection,&proc) | |
server = StoreServer.new | |
local_variables = eval( "local_variables", binding ).inject({}){|h,n| | |
h[n] = eval( n, binding ) | |
h | |
} | |
job_id = job.id | |
collection_name = collection.name | |
local_variables.delete(:job) | |
local_variables.delete(:collection) | |
server.call(:push, job_id, collection_name, proc.to_source, local_variables) | |
end | |
end | |
class StoreServer | |
def initialize | |
@tdc = CloudDatawareHouse::Client.new | |
@db = Mongo.new | |
@queue = Queue.new | |
end | |
def run | |
loop do | |
args = @queue.deq | |
case @tdc.job(args[1]).status | |
when 'success' | |
store(*args) | |
when 'running' | |
@queue.enq(arsg) | |
else | |
next | |
end | |
sleep 1 | |
end | |
end | |
def push(*args) | |
@queue.push(args) | |
end | |
private | |
def store(proc_string, job_id, collection_name, local_variables) | |
job = @tdc.job(job_id) | |
collection = @db.collection(collection_name) | |
eval <<PROG | |
#{local_variables.map{|i,v| "#{i} = #{v}"}.join(';') | |
proc_string.call | |
PROG | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment