Skip to content

Instantly share code, notes, and snippets.

@koyachi
Created December 29, 2009 02:46
Show Gist options
  • Save koyachi/265115 to your computer and use it in GitHub Desktop.
Save koyachi/265115 to your computer and use it in GitHub Desktop.
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
# 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
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