Class: Hustle::Hustler

Inherits:
Object
  • Object
show all
Includes:
MonitorMixin, Singleton
Defined in:
lib/hustle/hustler.rb

Instance Method Summary collapse

Instance Method Details

#active_runnersObject



16
17
18
# File 'lib/hustle/hustler.rb', line 16

def active_runners
  @active_runners ||= {}
end

#coresObject



12
13
14
# File 'lib/hustle/hustler.rb', line 12

def cores
  @cores ||= OS.cpu_count
end

#go(context: {}, callback: -> (val) {}, &block) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/hustle/hustler.rb', line 36

def go(context: {}, callback: -> (val) {}, &block)
  start_drb
  sleep 0 while active_runners.size >= cores.size
  uri = "druby://127.0.0.1:#{random_port}"
  runner = Runner.new(uri, context: context)
  runner.start_remote_instance
  synchronize do
    active_runners[runner.pid] = runner
  end
  finish runner, callback, &block
end

#start_drbObject



20
21
22
23
24
25
# File 'lib/hustle/hustler.rb', line 20

def start_drb
  @drb ||= begin
    DRb.start_service
    sleep 0 while server.is_a?(DRb::DRbServerNotFound)
  end
end

#stop_drbObject



27
28
29
30
31
32
33
34
# File 'lib/hustle/hustler.rb', line 27

def stop_drb
  synchronize do
    if active_runners.empty?
      DRb.stop_service
      @drb = nil
    end
  end
end

#waitObject



48
49
50
51
52
# File 'lib/hustle/hustler.rb', line 48

def wait
  active_runners.each do |_, runner|
    runner.callback_thread.join
  end
end