Class: Knj::Process_meta
Defined Under Namespace
Classes: Proxy_obj
Instance Attribute Summary collapse
-
#pid ⇒ Object
readonly
Returns the value of attribute pid.
-
#process ⇒ Object
readonly
Returns the value of attribute process.
Class Method Summary collapse
-
.args_parse(args) ⇒ Object
Parses the arguments given.
-
.args_parse_back(args, objects) ⇒ Object
Parses the special hashes to reflect the natural objects instead of proxy-objects.
Instance Method Summary collapse
-
#call_object(args, &block) ⇒ Object
Calls a method on an object and returns the result.
- #check_finalizers ⇒ Object
-
#destroy ⇒ Object
Destroyes the project and unsets all variables on the Process_meta-object.
-
#initialize(args = {}) ⇒ Process_meta
constructor
A new instance of Process_meta.
-
#new(class_name, *args, &block) ⇒ Object
Spawns a new object in the subprocess by that classname, with those arguments and with that block.
- #proxy_finalizer(id) ⇒ Object
-
#proxy_from_call(proxy_obj_to_call, method_name, *args) ⇒ Object
Returns a proxy-object to a object given from a call.
- #proxy_from_eval(eval_str) ⇒ Object
- #proxy_from_static(class_name, method_name, *args) ⇒ Object
-
#proxy_has?(var_name) ⇒ Boolean
Returns true if the given name exists in the subprocess-objects-hash.
-
#spawn_object(class_name, var_name = nil, *args, &block) ⇒ Object
Spawns a new object in the subprocess and returns a proxy-variable for that subprocess-object.
-
#static(const, method_name, *args, &block) ⇒ Object
Executes a static method on a class in the sub-process.
-
#static_noret(const, method_name, *args, &block) ⇒ Object
Executes a static call in the subprocess but does not capture or return the result.
-
#str_eval(str) ⇒ Object
Evaluates a string in the sub-process.
Constructor Details
#initialize(args = {}) ⇒ Process_meta
Returns a new instance of Process_meta.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/knj/process_meta.rb', line 7 def initialize(args = {}) @args = args @objects = {} @finalize = [] if @args["exec_path"] exec_path = @args["exec_path"] else exec_path = Knj::Os.executed_executable end exec_file = "#{File.dirname(__FILE__)}/scripts/process_meta_exec.rb" if args["id"] id = args["id"] else id = caller[0].to_s.strip end cmd = "#{exec_path} \"#{exec_file}\" #{Knj::Strings.unixsafe(id)}" if RUBY_ENGINE == "jruby" pid, @stdin, @stdout, @stderr = IO.popen4("#{exec_path} --#{RUBY_VERSION[0, 3]} \"#{exec_file}\" \"#{id}\"") else @stdin, @stdout, @stderr = Open3.popen3(cmd) end @stdout.sync = true @stdin.sync = true args = { :out => @stdin, :in => @stdout, :listen => true, :debug => @args["debug"] } if @args["debug"] or @args["debug_err"] args[:err] = @stderr args[:on_err] = proc{|line| $stderr.print "stderr: #{line}" } end #Wait for process to start and check that it is returning the expected output. start_line = @stdout.gets raise "Expected startline from process to be 'process_meta_started' but got: '#{start_line}'." if start_line != "process_meta_started\n" @process = Knj::Process.new(args) res = @process.send("obj" => {"type" => "process_data"}) raise "Unexpected process-data: '#{res}'." if !res.is_a?(Hash) or res["type"] != "process_data_success" @pid = res["pid"] #If block is given then run block and destroy self. if block_given? begin yield(self) ensure self.destroy end end end |
Instance Attribute Details
#pid ⇒ Object (readonly)
Returns the value of attribute pid.
5 6 7 |
# File 'lib/knj/process_meta.rb', line 5 def pid @pid end |
#process ⇒ Object (readonly)
Returns the value of attribute process.
5 6 7 |
# File 'lib/knj/process_meta.rb', line 5 def process @process end |
Class Method Details
.args_parse(args) ⇒ Object
Parses the arguments given. Proxy-object-arguments will be their natural objects in the subprocess.
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/knj/process_meta.rb', line 91 def self.args_parse(args) if args.is_a?(Array) newargs = [] args.each do |val| if val.is_a?(Knj::Process_meta::Proxy_obj) newargs << {"type" => "proxy_obj", "var_name" => val.[:name]} else newargs << Knj::Process_meta.args_parse(val) end end return newargs elsif args.is_a?(Hash) newargs = {} args.each do |key, val| if key.is_a?(Knj::Process_meta::Proxy_obj) key = {"type" => "proxy_obj", "var_name" => key.[:name]} else key = Knj::Process_meta.args_parse(key) end if val.is_a?(Knj::Process_meta::Proxy_obj) val = {"type" => "proxy_obj", "var_name" => val.[:name]} else val = Knj::Process_meta.args_parse(val) end newargs[key] = val end return newargs else return args end end |
.args_parse_back(args, objects) ⇒ Object
Parses the special hashes to reflect the natural objects instead of proxy-objects.
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/knj/process_meta.rb', line 128 def self.args_parse_back(args, objects) if args.is_a?(Array) newargs = [] args.each do |val| newargs << Knj::Process_meta.args_parse_back(val, objects) end return newargs elsif args.is_a?(Hash) and args["type"] == "proxy_obj" and args.key?("var_name") raise "No object by that var-name: '#{args["var_name"]}' in '#{objects}'." if !objects.key?(args["var_name"]) return objects[args["var_name"]] elsif args.is_a?(Hash) newargs = {} args.each do |key, val| newargs[Knj::Process_meta.args_parse_back(key, objects)] = Knj::Process_meta.args_parse_back(val, objects) end return newargs else return args end end |
Instance Method Details
#call_object(args, &block) ⇒ Object
Calls a method on an object and returns the result.
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 |
# File 'lib/knj/process_meta.rb', line 226 def call_object(args, &block) self.check_finalizers if args.key?("capture_return") capture_return = args["capture_return"] else capture_return = true end res = @process.send( { "buffer_use" => args["buffer_use"], "obj" => { "type" => "call_object_block", "var_name" => args["var_name"], "method_name" => args["method_name"], "capture_return" => capture_return, "args" => Knj::Process_meta.args_parse(args["args"]) } }, &block ) return res["result"] if res.is_a?(Hash) and res["type"] == "call_object_success" raise "Unknown result: '#{res}'." end |
#check_finalizers ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/knj/process_meta.rb', line 75 def check_finalizers return nil if @finalize.empty? remove = [] @finalize.each do |id| @process.send("obj" => { "type" => "unset", "var_name" => id }) remove << id end @finalize -= remove end |
#destroy ⇒ Object
Destroyes the project and unsets all variables on the Process_meta-object.
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 |
# File 'lib/knj/process_meta.rb', line 328 def destroy @process.send("obj" => {"type" => "exit"}) @err_thread.kill if @err_thread @process.destroy Process.kill("TERM", @pid) begin sleep 0.1 process_exists = Knj::Unix_proc.list("pids" => [@pid]) raise "Process exists." if !process_exists.empty? rescue => e raise e if e. != "Process exists." begin Process.kill(9, pid) if process_exists rescue Errno::ESRCH => e raise e if e. != "No such process" end retry end @process = nil @stdin = nil @stdout = nil @stderr = nil @objects = nil @args = nil end |
#new(class_name, *args, &block) ⇒ Object
Spawns a new object in the subprocess by that classname, with those arguments and with that block.
186 187 188 |
# File 'lib/knj/process_meta.rb', line 186 def new(class_name, *args, &block) return self.spawn_object(class_name, nil, *args, &block) end |
#proxy_finalizer(id) ⇒ Object
71 72 73 |
# File 'lib/knj/process_meta.rb', line 71 def proxy_finalizer(id) @finalize << id end |
#proxy_from_call(proxy_obj_to_call, method_name, *args) ⇒ Object
Returns a proxy-object to a object given from a call.
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 |
# File 'lib/knj/process_meta.rb', line 288 def proxy_from_call(proxy_obj_to_call, method_name, *args) proxy_obj = Knj::Process_meta::Proxy_obj.new(:process_meta => self) var_name = proxy_obj.__id__ proxy_obj.[:name] = var_name res = @process.send( "obj" => { "type" => "proxy_from_call", "proxy_obj" => proxy_obj_to_call.__id__, "method_name" => method_name, "var_name" => var_name, "args" => Knj::Process_meta.args_parse(args) } ) return proxy_obj end |
#proxy_from_eval(eval_str) ⇒ Object
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 |
# File 'lib/knj/process_meta.rb', line 253 def proxy_from_eval(eval_str) proxy_obj = Knj::Process_meta::Proxy_obj.new(:process_meta => self) var_name = proxy_obj.__id__ proxy_obj.[:name] = var_name res = @process.send( "obj" => { "type" => "proxy_from_eval", "str" => eval_str, "var_name" => var_name } ) return proxy_obj end |
#proxy_from_static(class_name, method_name, *args) ⇒ Object
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 |
# File 'lib/knj/process_meta.rb', line 269 def proxy_from_static(class_name, method_name, *args) proxy_obj = Knj::Process_meta::Proxy_obj.new(:process_meta => self) var_name = proxy_obj.__id__ proxy_obj.[:name] = var_name res = @process.send( "obj" => { "type" => "proxy_from_static", "const" => class_name, "method_name" => method_name, "var_name" => var_name, "args" => Knj::Process_meta.args_parse(args) } ) return proxy_obj end |
#proxy_has?(var_name) ⇒ Boolean
Returns true if the given name exists in the subprocess-objects-hash.
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 |
# File 'lib/knj/process_meta.rb', line 307 def proxy_has?(var_name) self.check_finalizers begin res = @process.send( "obj" => { "type" => "call_object_block", "var_name" => var_name, "method_name" => "__id__", "args" => [] } ) rescue => e return false if e..to_s.match(/^No object by that name/) raise e end return true end |
#spawn_object(class_name, var_name = nil, *args, &block) ⇒ Object
Spawns a new object in the subprocess and returns a proxy-variable for that subprocess-object.
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
# File 'lib/knj/process_meta.rb', line 191 def spawn_object(class_name, var_name = nil, *args, &block) proxy_obj = Knj::Process_meta::Proxy_obj.new(:process_meta => self, :name => var_name) if var_name == nil var_name = proxy_obj.__id__ proxy_obj.[:name] = var_name end res = @process.send( { "obj" => { "type" => "spawn_object", "class_name" => class_name, "var_name" => var_name, "args" => Knj::Process_meta.args_parse(args) } }, &block ) return proxy_obj end |
#static(const, method_name, *args, &block) ⇒ Object
Executes a static method on a class in the sub-process.
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/knj/process_meta.rb', line 169 def static(const, method_name, *args, &block) res = @process.send( "obj" => { "type" => "static", "const" => const, "method_name" => method_name, "capture_return" => true, "args" => Knj::Process_meta.args_parse(args), }, &block ) return res["result"] if res["type"] == "call_const_success" raise "Unknown result: '#{res}'." end |
#static_noret(const, method_name, *args, &block) ⇒ Object
Executes a static call in the subprocess but does not capture or return the result. Useful if the static method returns an object that would load a library after being un-marshaled.
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/knj/process_meta.rb', line 152 def static_noret(const, method_name, *args, &block) res = @process.send( "obj" => { "type" => "static", "const" => const, "method_name" => method_name, "capture_return" => false, "args" => Knj::Process_meta.args_parse(args), }, &block ) return res["result"] if res["type"] == "call_const_success" raise "Unknown result: '#{res}'." end |
#str_eval(str) ⇒ Object
Evaluates a string in the sub-process.
215 216 217 218 219 220 221 222 223 |
# File 'lib/knj/process_meta.rb', line 215 def str_eval(str) res = @process.send("obj" => { "type" => "str_eval", "str" => str }) return res["result"] if res.is_a?(Hash) and res["type"] == "call_eval_success" return "Unknown result: '#{res}'." end |