lib/knj/process_meta.rb in knjrbfw-0.0.9 vs lib/knj/process_meta.rb in knjrbfw-0.0.10

- old
+ new

@@ -15,15 +15,22 @@ 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}\"") + pid, @stdin, @stdout, @stderr = IO.popen4("#{exec_path} --#{RUBY_VERSION[0, 3]} \"#{exec_file}\" \"#{id}\"") else - @stdin, @stdout, @stderr, wait_thr = Open3.popen3("#{exec_path} \"#{exec_file}\"") - @pid = wait_thr.pid + @stdin, @stdout, @stderr = Open3.popen3(cmd) end args = { :out => @stdin, :in => @stdout, @@ -38,10 +45,14 @@ } end @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 @@ -306,25 +317,20 @@ return true end #Destroyes the project and unsets all variables on the Process_meta-object. def destroy - begin - @process.send("obj" => {"type" => "exit"}) - rescue Exception => e - raise e if e.message != "exit" - end - + @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 - STDOUT.print "Process wont kill - try to kill...\n" + rescue => e + raise e if e.message != "Process exists." begin Process.kill(9, pid) if process_exists rescue Errno::ESRCH => e raise e if e.message != "No such process" \ No newline at end of file