Class: Hustle::Hustler
- Inherits:
-
Object
- Object
- Hustle::Hustler
- Includes:
- MonitorMixin, Singleton
- Defined in:
- lib/hustle.rb
Instance Method Summary collapse
- #active_runners ⇒ Object
- #cores ⇒ Object
- #go(callback: -> (val) {}, &block) ⇒ Object
- #start_drb ⇒ Object
- #stop_drb ⇒ Object
- #wait ⇒ Object
Instance Method Details
#active_runners ⇒ Object
25 26 27 |
# File 'lib/hustle.rb', line 25 def active_runners @active_runners ||= {} end |
#cores ⇒ Object
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_drb ⇒ Object
29 30 31 |
# File 'lib/hustle.rb', line 29 def start_drb @drb ||= DRb.start_service end |
#stop_drb ⇒ Object
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 |
#wait ⇒ Object
56 57 58 59 60 |
# File 'lib/hustle.rb', line 56 def wait active_runners.each do |_, runner| runner.callback_thread.join end end |