我正在运行 Lua 代码。(附在下面)。代码运行良好,但是当我尝试使用CTRL + C
它发送 SIGINT 时不尊重它。
local ltn12 = assert(require('ltn12'))
local cjson = assert(require('cjson'))
local http = assert(require('socket.http'))
-- local dbg = assert(require('debugger'))
-- local signal = require("posix.signal")
-- signal.signal(signal.SIGINT, function(signum)
-- io.write("\n")
-- -- put code to save some stuff here
-- os.exit(128 + signum)
-- end)
http.TIMEOUT = 120
local function execute()
print('-- COMMANDER: Starting... ----------')
os.execute('sleep 1')
local ivy_url = 'http://localhost:3000/'
if(ivy_url == nil) then
print('-- COMMANDER: Error :: ivy_url not set ----------')
execute()
end
local _heartbeat = 0
local last_timestamp = nil
local url = nil
while true do
-- _heartbeat used to inform that loop is executing properly.
if _heartbeat % 3 == 0 then
_heartbeat = 0
print('-- COMMANDER: Heartbeat... ----------')
end
_heartbeat = _heartbeat + 1
local response = {}
local since = last_timestamp
last_timestamp = os.date("%Y-%m-%dT%H:%M:%S")
if (since == nil) then
url = ivy_url .. "/switch/sites"
else
url = ivy_url .. "/switch/sites?since=" .. since
end
local one, code, headers, status = http.request {
method = "GET",
url = url,
headers = { Authorization = "Basic " .. tostring("token")},
sink = ltn12.sink.table(response)
}
if(code == 200) then
response_data = cjson.decode(response[1])
for i, site_desc in pairs(response_data['data']) do
print(site_desc)
end
print('-- COMMANDER: Sent API request to Ivy to get site info updated_at ' .. tostring(since) .. ' ----------')
elseif(code == 400) then
last_timestamp = since
response_data = cjson.decode(response[1])
print('-- COMMANDER: Got error in ivy API response : ' .. tostring(response_data['error']) .. ' at ' .. os.date("%Y-%m-%dT%H:%M:%S") .. ' ----------')
else
last_timestamp = since
print('-- COMMANDER: Got error in ivy API response : ' .. tostring(code) .. ' at ' .. os.date("%Y-%m-%dT%H:%M:%S") .. ' ----------')
end
os.execute('sleep 10')
end
end
execute()
我如何实现这一点,并确保 CTRL+C 受到 Lua 程序的尊重。
我尝试使用posix.signal
但没有用。
signal.signal(signal.SIGINT, function(signum)
io.write("Interrupting => \n")
-- put code to save some stuff here
os.exit(0)
end)