Class: Bg::DrbRunner

Inherits:
Object
  • Object
show all
Defined in:
lib/bg/drb_runner.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logfile: logfile) ⇒ DrbRunner

Returns a new instance of DrbRunner.



9
10
11
# File 'lib/bg/drb_runner.rb', line 9

def initialize(logfile: logfile)
  @logger = Logger.new(logfile || "/dev/null")
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



7
8
9
# File 'lib/bg/drb_runner.rb', line 7

def logger
  @logger
end

Instance Method Details

#ready?Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/bg/drb_runner.rb', line 13

def ready?
  true
end

#run(proc: nil, args: []) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/bg/drb_runner.rb', line 17

def run(proc: nil, args: [])
  Thread.new do
    begin
      sleep 0
      method = "define_method :run #{proc}"
      runner = Class.new { eval method }
      logger.info "Start exec: #{method}"
      runner.new.run(*Marshal.load(args))
      logger.info "Finish exec: #{method}"
    rescue Exception => e
      logger.error "Failed exec: #{method}\n#{e}"
    ensure
      DRb.stop_service rescue nil
      Process.exit
    end
  end
end