Class: Cult::Paramap::Job
- Inherits:
-
Object
- Object
- Cult::Paramap::Job
- Defined in:
- lib/cult/paramap.rb
Instance Attribute Summary collapse
-
#block ⇒ Object
readonly
Returns the value of attribute block.
-
#ident ⇒ Object
readonly
Returns the value of attribute ident.
-
#pid ⇒ Object
readonly
Returns the value of attribute pid.
-
#pipe ⇒ Object
readonly
Returns the value of attribute pipe.
-
#rebind ⇒ Object
readonly
Returns the value of attribute rebind.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Instance Method Summary collapse
- #exception ⇒ Object
- #fetch_response! ⇒ Object
-
#initialize(ident, value, rebind: {}, &block) ⇒ Job
constructor
A new instance of Job.
- #prepare_forked_environment! ⇒ Object
- #rebind_streams! ⇒ Object
- #result ⇒ Object
- #success? ⇒ Boolean
- #write_response!(scode, obj) ⇒ Object
Constructor Details
#initialize(ident, value, rebind: {}, &block) ⇒ Job
Returns a new instance of Job.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/cult/paramap.rb', line 24 def initialize(ident, value, rebind: {}, &block) @ident, @value, @rebind, @block = ident, value, rebind, block @pipe = IO.pipe @pid = fork do @pipe[0].close prepare_forked_environment! begin write_response!('=', block.call(value)) rescue Exception => e write_response!('!', e) end end @pipe[1].close end |
Instance Attribute Details
#block ⇒ Object (readonly)
Returns the value of attribute block.
21 22 23 |
# File 'lib/cult/paramap.rb', line 21 def block @block end |
#ident ⇒ Object (readonly)
Returns the value of attribute ident.
21 22 23 |
# File 'lib/cult/paramap.rb', line 21 def ident @ident end |
#pid ⇒ Object (readonly)
Returns the value of attribute pid.
22 23 24 |
# File 'lib/cult/paramap.rb', line 22 def pid @pid end |
#pipe ⇒ Object (readonly)
Returns the value of attribute pipe.
22 23 24 |
# File 'lib/cult/paramap.rb', line 22 def pipe @pipe end |
#rebind ⇒ Object (readonly)
Returns the value of attribute rebind.
21 22 23 |
# File 'lib/cult/paramap.rb', line 21 def rebind @rebind end |
#value ⇒ Object (readonly)
Returns the value of attribute value.
21 22 23 |
# File 'lib/cult/paramap.rb', line 21 def value @value end |
Instance Method Details
#exception ⇒ Object
99 100 101 102 |
# File 'lib/cult/paramap.rb', line 99 def exception fetch_response! @exception end |
#fetch_response! ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/cult/paramap.rb', line 75 def fetch_response! unless pipe[0].closed? data = @pipe[0].read scode = data[0] fail unless ['!', '='].include?(scode) data = data[1..-1] ivar = (scode == '!') ? :exception : :result begin obj = Marshal.load(data) rescue obj = nil end instance_variable_set("@#{ivar}", obj) pipe[0].close end end |
#prepare_forked_environment! ⇒ Object
54 55 56 57 58 59 60 |
# File 'lib/cult/paramap.rb', line 54 def prepare_forked_environment! rebind_streams! # Stub out things that have caused a problem in the past. Kernel.send(:define_method, :exec) do |*a| fail "don't use Kernel\#exec inside of a paramap job" end end |
#rebind_streams! ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/cult/paramap.rb', line 40 def rebind_streams! names = { stdout: STDOUT, stdin: STDIN, stderr: STDERR, nil => '/dev/null' } rebind.each do |k, v| src, dst = names[k], names[v] dst = File.open(dst, 'w+') if dst.is_a?(String) src.reopen(dst) end end |
#result ⇒ Object
94 95 96 97 |
# File 'lib/cult/paramap.rb', line 94 def result fetch_response! @result end |
#success? ⇒ Boolean
104 105 106 |
# File 'lib/cult/paramap.rb', line 104 def success? exception.nil? end |
#write_response!(scode, obj) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/cult/paramap.rb', line 62 def write_response!(scode, obj) fail unless ['!', '='].include?(scode) begin pipe[1].write(scode + Marshal.dump(obj)) rescue TypeError => e # Unmarshallable raise unless e..match(/_dump_data/) pipe[1].write(scode + Marshal.dump(nil)) end pipe[1].flush pipe[1].close end |