Sha256: 1967bba84e03f2c4fa8c157e7043cde757e285772b73a383e50e86fa24b173de
Contents?: true
Size: 1.24 KB
Versions: 2
Compression:
Stored size: 1.24 KB
Contents
module Phantom class << self def run(pid_file: nil, on_ok: nil, on_error: nil, &block) return Phantom::Base.new(nil) unless block_given? raise ForkError.new('Running process exists.', pid_file) if pid_file and File.exist?(pid_file) i, o = IO.pipe f = File.new(pid_file, 'w') if pid_file pid = fork do at_exit do o.flush o.close File.delete pid_file if pid_file end trap(:TERM) do Process.abort end i.close begin block.call if block_given? Marshal.dump(Status::OK, o) rescue StandardError => e Marshal.dump(e, o) end end Process.detach(pid) f.write(pid.to_s) if pid_file f.close if pid_file o.close Thread.abort_on_exception = true Thread.new do begin status = Marshal.load(i) if status == Status::OK then on_ok.call if on_ok else on_error.call(status) if on_error end rescue Errno::EPIPE, EOFError => e on_error.call(e) if on_error ensure i.close end end return Phantom::Base.new(pid) end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
phantom-0.0.2.pre | lib/phantom/class_methods.rb |
phantom-0.0.1 | lib/phantom/class_methods.rb |