webmcp

view framework/env/execute/command.lua @ 487:91d0c8304d74

Do not execute abortable command if termination has already been requested
author jbe
date Mon Jun 12 03:18:54 2017 +0200 (2017-06-12)
parents 7d53e12f0804
children 5b1c4f76e44f
line source
1 --[[--
2 output, -- collected data from stdout if process exited successfully
3 errmsg = -- error message if execution failed or if process didn't exit successfully
4 execute.command{
5 command = { filename, arg1, arg2, ... }, -- command and arguments
6 stdin_data = stdin_data, -- optional data to be sent to process via stdin
7 stdout_result_handler = stdout_result_handler, -- callback receiving: stdout data, success boolean, optional error message
8 stderr_line_handler = stderr_line_handler, -- callback for processing stderr line-wise
9 exit_handler = exit_handler, -- callback when process exited
10 signal_handler = signal_handler, -- callback when process terminated due to signal
11 timeout_handler = timeout_handler, -- callback when process gets killed due to timeout
12 abort_handler = abort_handler, -- callback when process gets killed due to request by poll function
13 abortable = abortable, -- set to true if process shall be terminated if poll function requests termination
14 poll = poll, -- alternative poll command with moonbridge_io.poll(...) semantics
15 db = db, -- database handle for waiting for notifies
16 db_notify_handler = db_notify_handler -- handler for database notifications which may return true to kill process
17 }
19 --]]--
21 function execute.command(args)
23 local moonbridge_io = require("moonbridge_io")
24 local poll = args.poll or moonbridge_io.poll
26 local stdout_chunks, stderr_chunks = {}, {}
28 local function return_error(errmsg)
29 if args.stdout_result_handler then
30 args.stdout_result_handler(table.concat(stdout_chunks), false, errmsg)
31 end
32 return nil, errmsg
33 end
35 if args.abortable then
36 local pollready, pollmsg, pollterm = poll(nil, nil, 0, true)
37 if pollterm then
38 if args.abort_handler then args.abort_handler() end
39 return_error(pollmsg)
40 end
41 end
43 local start = moonbridge_io.timeref()
44 local process, errmsg = moonbridge_io.exec(table.unpack(args.command))
45 if not process then return nil, errmsg end
47 local read_fds = {[process.stdout] = true, [process.stderr] = true}
48 local write_fds = {[process.stdin] = true}
49 if args.db then
50 read_fds[args.db.fd] = true
51 end
53 local function write(...)
54 if write_fds[process.stdin] then
55 local buffered = process.stdin:flush_nb(...)
56 if not buffered or buffered == 0 then
57 process.stdin:close()
58 write_fds[process.stdin] = nil
59 end
60 end
61 end
62 write(args.stdin_data or "")
64 while
65 read_fds[process.stdout] or read_fds[process.stderr] or
66 write_fds[process.stdin]
67 do
68 local timeout = args.timeout and args.timeout-moonbridge_io.timeref(start)
69 local pollready, pollmsg, pollterm =
70 poll(read_fds, write_fds, timeout, args.abortable)
71 if not pollready then
72 process:kill():wait()
73 if pollterm then
74 if args.abort_handler then args.abort_handler() end
75 else
76 if args.timeout_handler then args.timeout_handler() end
77 end
78 return return_error(pollmsg)
79 end
80 if args.db then
81 local channel, payload, pid = db:wait(0)
82 if channel then
83 if args.db_notify_handler(channel, payload, pid) then
84 process:kill():wait()
85 return return_error("Database event received")
86 end
87 end
88 end
89 if read_fds[process.stdout] then
90 local chunk, status = process.stdout:read_nb()
91 if not chunk or status == "eof" then
92 process.stdout:close()
93 read_fds[process.stdout] = nil
94 end
95 if chunk and chunk ~= "" then
96 stdout_chunks[#stdout_chunks+1] = chunk
97 end
98 end
99 if read_fds[process.stderr] then
100 local chunk, status = process.stderr:read_nb()
101 if not chunk or status == "eof" then
102 process.stderr:close()
103 read_fds[process.stderr] = nil
104 end
105 if chunk and args.stderr_line_handler then
106 while true do
107 local chunk1, chunk2 = string.match(chunk, "(.-)\n(.*)")
108 if not chunk1 then break end
109 stderr_chunks[#stderr_chunks+1] = chunk1
110 args.stderr_line_handler(table.concat(stderr_chunks))
111 stderr_chunks = {}
112 chunk = chunk2
113 end
114 if chunk ~= "" then
115 stderr_chunks[#stderr_chunks+1] = chunk
116 end
117 if status == "eof" then
118 local line = table.concat(stderr_chunks)
119 if #line > 0 then args.stderr_line_handler(line) end
120 end
121 end
122 end
123 write()
124 end
126 local status = process:wait()
128 if status < 0 then
129 if args.signal_handler then
130 args.signal_handler(-status)
131 end
132 return return_error("Command terminated by signal " .. -status)
133 elseif status > 0 then
134 if args.exit_handler then
135 args.exit_handler(status)
136 end
137 return return_error("Command returned exit code " .. status)
138 elseif args.stdout_result_handler then
139 args.stdout_result_handler(table.concat(stdout_chunks), true)
140 return true
141 else
142 return table.concat(stdout_chunks)
143 end
145 end

Impressum / About Us