Created
December 29, 2009 02:46
-
-
Save koyachi/265115 to your computer and use it in GitHub Desktop.
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
def which(cmd) | |
result = `which #{cmd}` | |
result.chomp | |
end | |
# kim-gearman-rubyからコピペ+ | |
def start_gearmand(port = 4730) | |
cmd = "#{gearmand_path} -d -p #{port} --pid-file=#{gearmand_pidfile(port)}" | |
system cmd | |
gearmand_pid(port) | |
end | |
def stop_gearmand(port = 4730) | |
Process.kill "KILL", gearmand_pid(port) | |
File.unlink gearmand_pidfile(port) | |
end | |
def teardown_gearmands | |
glob = "/tmp/gearmand_*_#{$$}.pid" | |
Dir[glob].each do |pidfile| | |
Process.kill "KILL", `cat #{pidfile}`.to_i | |
File.unlink pidfile | |
end | |
end | |
def gearmand_pid(port = 4730) | |
`cat #{gearmand_pidfile(port)}`.to_i | |
end | |
def gearmand_pidfile(port) | |
pidfile = "/tmp/gearmand_#{port}_#{$$}.pid" | |
pidfile | |
end | |
$gearmand_path = '' | |
def gearmand_path(cmd=nil) | |
if cmd == nil | |
$gearmand_path = which(cmd) if $gearmand_path == '' | |
else | |
$gearmand_path = cmd | |
end | |
$gearmand_path | |
end |
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
# func_yyyは普通に単体試験しといてここではgearman workerとして使えてるか確認する | |
module XXX | |
class << self | |
def func_yyy | |
'ウーピィ' | |
end | |
def func_zzz | |
'internet' | |
end | |
end | |
FUNC = { | |
'yyy' => self.method(:func_yyy) | |
} | |
end | |
class XXX::YYY | |
include Log0x::Workerize | |
worker_type :gearman => { | |
:func => 'yyy' | |
} | |
def work(data,job) | |
params = JSON.parse data | |
::XXX::FUNC[@func_name].call params | |
end | |
end | |
class XXX::ZZZ | |
# 同様に | |
end | |
# まとめて | |
#module XXX | |
# %w[YYY ZZZ].each do |cls| | |
# c = Class.new(::Object) do | |
# include Log0x::Workerize | |
# worker_type :gearman => { | |
# :func => cls.downcase | |
# } | |
# end | |
# | |
# def init(args) | |
# # 適当に | |
# end | |
# | |
# def work(data,job) | |
# params = JSON.parse data | |
# ::XXX::FUNC[@func_name].call params | |
# end | |
# end | |
#end |
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
describe XXX do | |
def setup_client(args=nil) | |
client = Gearman::Client.new 'localhost:4730' | |
taskset = Gearman::TaskSet.new client | |
[client, taskset] | |
end | |
def do_task(client, taskset, method, params) | |
task = Gearman::Task.new method, params.to_json | |
taskset.add_task task | |
end | |
describe 'XXX::YYY' do | |
before do | |
gearmand_path('/usr/local/gearmand-0.10/sbin/gearmand') | |
start_gearmand | |
::XXX::FUNC['yyy'] = lambda do |params| | |
params.should == {:foo => 100, :bar => 200} | |
'mocked yyy result' | |
end | |
Thread.start do | |
Log0x::BootLoader.start(::XXX::YYY, { | |
'foo' => 'bar' | |
},{ | |
'worker_common' => { | |
'gearman' => { | |
'servers' => ['localhost:4730'] | |
} | |
} | |
}) | |
end | |
@cw = setup_client | |
end | |
after do | |
stop_gearmand | |
end | |
it 'gearmanクライアントからyyyメソッド実行してXXX::FUNC["yyy"]メソッドが呼ばれること' do | |
do_task @cw[0], @cw[1], 'yyy', {:foo => 100, :bar => 200} | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment