Class: Hustle::Hustler

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

Instance Method Summary collapse

Instance Method Details

#active_runnersObject



25
26
27
# File 'lib/hustle.rb', line 25

def active_runners
  @active_runners ||= {}
end

#coresObject



21
22
23
# File 'lib/hustle.rb', line 21

def cores
  @cores ||= OS.cpu_count
end

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



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/hustle.rb', line 42

def go(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)
  runner.start_remote_instance
  sleep 0 while !runner.remote_instance_ready?
  synchronize do
    active_runners[runner.pid] = runner
  end
  runner.run_remote(&block)
  finish runner, callback
end

#start_drbObject



29
30
31
# File 'lib/hustle.rb', line 29

def start_drb
  @drb ||= DRb.start_service
end

#stop_drbObject



33
34
35
36
37
38
39
40
# File 'lib/hustle.rb', line 33

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

#waitObject



56
57
58
59
60
# File 'lib/hustle.rb', line 56

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