Class: Hustle::Hustler
- Inherits:
-
Object
- Object
- Hustle::Hustler
- Includes:
- MonitorMixin, Singleton
- Defined in:
- lib/hustle/hustler.rb
Instance Method Summary collapse
- #active_runners ⇒ Object
- #cores ⇒ Object
- #go(context: {}, callback: -> (val) {}, &block) ⇒ Object
- #start_drb ⇒ Object
- #stop_drb ⇒ Object
- #wait ⇒ Object
Instance Method Details
#active_runners ⇒ Object
16 17 18 |
# File 'lib/hustle/hustler.rb', line 16 def active_runners @active_runners ||= {} end |
#cores ⇒ Object
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_drb ⇒ Object
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_drb ⇒ Object
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 |
#wait ⇒ Object
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 |