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
45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/hustle.rb', line 45 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 synchronize do active_runners[runner.pid] = runner end finish runner, callback, &block end |
#start_drb ⇒ Object
29 30 31 32 33 34 |
# File 'lib/hustle.rb', line 29 def start_drb @drb ||= begin DRb.start_service sleep 0 while server.is_a?(DRb::DRbServerNotFound) end end |
#stop_drb ⇒ Object
36 37 38 39 40 41 42 43 |
# File 'lib/hustle.rb', line 36 def stop_drb synchronize do if active_runners.empty? DRb.stop_service @drb = nil end end end |
#wait ⇒ Object
57 58 59 60 61 |
# File 'lib/hustle.rb', line 57 def wait active_runners.each do |_, runner| runner.callback_thread.join end end |