I'm trying to asynchronously execute programs in openresty, and lua-resty-exec and lua-resty shell have nice nonblocking interfaces along the lines of
local exec = require'resty.exec'
local prog = exec.new('/tmp/exec.sock')
local res, err = prog('uname')
I know that prog() in this case doesn’t block the web werver, but it does block progression the request.
I’m hoping to do something along the lines of this pseudocode.
location /run_program {
content_by_lua_block {
local prog = require'resty.exec'.new('/tmp/exec.sock')
local handle = async_prog(‘long_running_program') -- I don’t want to wait until the subprocess exits here
ngx.say(‘started a long running program at handle ’ .. handle)
}
}
location /check_program_status {
content_by_lua_block {
local handle = ngx.req.get_uri_args().handle
local res, err = async_prog_wait(handle) -- also don’t block here, just status like waitpid() with WNOHANG
ngx.say(‘long running program status is ’ .. res)
}
}
Anyone have any ideas? Or a suggestion for a better way to do it?
Thanks
Erik
init_worker_by_lua_block {
processmgr = require('process_manager').new()
ngx.timer.at(0,function()
processmgr:run()
end)
}
local handle = ngx.thread.spawn(function()
local res = prog('long_running_program')
call_some_useful_function_when_done
end)
local handle = ngx.thread.spawn(function()
ngx.shared.progs.set('some-key',true)
local res = prog('long_running_program')
ngx.shared.progs.set('some-key',nil)
end)
Those are some useful ideas, I appreciate the response! In the meantime I hacked together an asynchronous version using luvit, which was a bit of fun. https://luvit.io/
But I’ll review and see if your suggestion is more appropriate, I’d rather not maintain my one-off code if I don’t have to.