I'm using:
- Ruby 1.9.3-p448
- Windows Server 2008
I have a file that contains commands that is used by a program, I'm using it in this way
C:\> PATH_TO_FOLDER/program.exe file.txt
File.txt
have some commands so program.exe
will do the following:
- Execute commands
- Reads from a DB using an ODBC method used by program
- Outputs result in a txt file
Using powershell this command works fine and as expected.
Now I have this in a file (app.rb
):
require 'sinatra'
require 'open3'
get '/process' do
program_path = "path to program.exe"
file_name = "file.txt"
Open3.popen3(program_path, file_name) do |i, o, e, w|
# I have some commands here to execute but just as an example I'm using o.read
puts o.read
end
end
Now when using this by accessing http://localhost/process
, Open3
works by doing this (I'm not 100% sure but after trying several times I think is the only option)
Reads commands and executes them (this is ok)
Tries to read from DB by using ODBC method (Here is my problem. I need to receive some output from
Open3
so I can show it in a browser, but I guess when it tries to read it starts another process thatOpen3
is not aware of, so Open3 goes on and finish without waiting for it)Exits
Exits
I've found about following:
- Use
Thread.join
(in this case,w.join
) in order to wait for process to finish, but it doesn't work Open4
seems to handle child process but doesn't work on WindowsProcess.wait(pid)
, in this casepid = w.pid
, but also doesn't workTimeout.timeout(n)
, the problem here is that I'm not sure how long will it take.
Is there any way of handling this? (waiting for Open3
subprocess so I get proper output).