Class: Hustle::Runner
- Inherits:
-
Object
- Object
- Hustle::Runner
- Defined in:
- lib/hustle/runner.rb
Instance Attribute Summary collapse
-
#callback_thread ⇒ Object
Returns the value of attribute callback_thread.
-
#context ⇒ Object
readonly
Returns the value of attribute context.
-
#pid ⇒ Object
readonly
Returns the value of attribute pid.
-
#uri ⇒ Object
readonly
Returns the value of attribute uri.
Instance Method Summary collapse
-
#initialize(uri, context: {}) ⇒ Runner
constructor
methods to be run on the local instance.
- #proc_to_string(proc) ⇒ Object
- #remote_instance ⇒ Object
- #remote_instance_ready? ⇒ Boolean
- #remote_instance_started? ⇒ Boolean
- #run(code_string) ⇒ Object
- #run_remote(context: {}, &block) ⇒ Object
- #start_remote_instance ⇒ Object
-
#stop ⇒ Object
methods to be run on the remote instance.
- #stop_remote_instance ⇒ Object
Constructor Details
#initialize(uri, context: {}) ⇒ Runner
methods to be run on the local instance
10 11 12 13 |
# File 'lib/hustle/runner.rb', line 10 def initialize(uri, context: {}) @uri = uri @context = context end |
Instance Attribute Details
#callback_thread ⇒ Object
Returns the value of attribute callback_thread.
6 7 8 |
# File 'lib/hustle/runner.rb', line 6 def callback_thread @callback_thread end |
#context ⇒ Object (readonly)
Returns the value of attribute context.
5 6 7 |
# File 'lib/hustle/runner.rb', line 5 def context @context end |
#pid ⇒ Object (readonly)
Returns the value of attribute pid.
5 6 7 |
# File 'lib/hustle/runner.rb', line 5 def pid @pid end |
#uri ⇒ Object (readonly)
Returns the value of attribute uri.
5 6 7 |
# File 'lib/hustle/runner.rb', line 5 def uri @uri end |
Instance Method Details
#proc_to_string(proc) ⇒ Object
51 52 53 54 55 56 |
# File 'lib/hustle/runner.rb', line 51 def proc_to_string(proc) code = proc.source start = code.index(/(?<=\{| do)/) finish = code.rindex(/\}|end/) - 1 code[start..finish].strip end |
#remote_instance ⇒ Object
15 16 17 |
# File 'lib/hustle/runner.rb', line 15 def remote_instance DRbObject.new_with_uri(uri) end |
#remote_instance_ready? ⇒ Boolean
23 24 25 26 27 28 29 |
# File 'lib/hustle/runner.rb', line 23 def remote_instance_ready? begin !remote_instance.uri.nil? rescue DRb::DRbConnError false end end |
#remote_instance_started? ⇒ Boolean
19 20 21 |
# File 'lib/hustle/runner.rb', line 19 def remote_instance_started? !pid.nil? end |
#run(code_string) ⇒ Object
64 65 66 67 68 69 70 |
# File 'lib/hustle/runner.rb', line 64 def run(code_string) begin eval code_string, binding rescue Exception => e e end end |
#run_remote(context: {}, &block) ⇒ Object
45 46 47 48 49 |
# File 'lib/hustle/runner.rb', line 45 def run_remote(context: {}, &block) sleep 0 while !remote_instance_ready? code_string = proc_to_string(block) remote_instance.run code_string end |
#start_remote_instance ⇒ Object
31 32 33 34 35 36 37 38 39 |
# File 'lib/hustle/runner.rb', line 31 def start_remote_instance return if remote_instance_started? @pid = fork do DRb.start_service uri, self DRb.thread.join end Process.detach pid pid end |
#stop ⇒ Object
methods to be run on the remote instance
60 61 62 |
# File 'lib/hustle/runner.rb', line 60 def stop DRb.stop_service end |
#stop_remote_instance ⇒ Object
41 42 43 |
# File 'lib/hustle/runner.rb', line 41 def stop_remote_instance remote_instance.stop end |