def daemon(*a,&b)
jrd = new(*a, &b)
r, w = IO::pipe
unless((pid = fork))
$0 = "#{ self }".gsub(%/[^a-zA-Z]+/,'_').downcase
begin
r.close
n = 0
uri = nil
socket = nil
42.times do
begin
s = "%s/%s_%s_%s_%s" %
[Dir::tmpdir, File::basename($0), Process::ppid, n, rand(42)]
u = "drbunix://#{ s }"
DRb::start_service u, jrd
socket = s
uri = u
break
rescue Errno::EADDRINUSE
n += 1
end
end
if socket and uri
w.write socket
w.close
pid = Process::pid
ppid = Process::ppid
cur = Thread::current
Thread::new(pid, ppid, cur) do |pid, ppid, cur|
loop do
begin
Process::kill 0, ppid
sleep 42
rescue
cur.raise "parent <#{ ppid }> died unexpectedly"
end
end
end
DRb::thread.join
else
w.close
end
ensure
exit!
end
else
w.close
socket = r.read
r.close
if socket and File::exist?(socket)
at_exit{ FileUtils::rm_f socket }
uri = "drbunix://#{ socket }"
DRb::start_service 'druby://localhost:0', nil
jrd = DRbObject::new nil, uri
jrd.pid = pid
jrd.uri = uri
else
raise "failed to start job runner daemon"
end
end
return jrd
end