为什么 lua 不遵守我的 SIGINT 信号
Why is lua not respecting my SIGINT signal
我是运行一个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()
如何实现这一点,并确保 Lua 程序遵守 CTRL+C。
我尝试使用 posix.signal
但没有用。
signal.signal(signal.SIGINT, function(signum)
io.write("Interrupting => \n")
-- put code to save some stuff here
os.exit(0)
end)
posix.signal
通过在接收到信号时设置 Lua 挂钩来实现信号处理。 Lua 钩子将在下一次执行 Lua 指令时 运行。您的程序使用 LuaSocket,一个用 C 编写的 Lua 库。因此,如果在 LuaSocket 的 C 代码为 运行ning 时有信号传入,您的信号处理代码将获胜't 运行 直到 returns。为了按照您想要的方式进行这项工作,必须修改 LuaSocket 以在它被信号中断时执行虚拟 lua_call
。然后这将触发您的 Lua 钩子到 运行。关于这个我打开了https://github.com/diegonehab/luasocket/issues/319
我是运行一个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()
如何实现这一点,并确保 Lua 程序遵守 CTRL+C。
我尝试使用 posix.signal
但没有用。
signal.signal(signal.SIGINT, function(signum)
io.write("Interrupting => \n")
-- put code to save some stuff here
os.exit(0)
end)
posix.signal
通过在接收到信号时设置 Lua 挂钩来实现信号处理。 Lua 钩子将在下一次执行 Lua 指令时 运行。您的程序使用 LuaSocket,一个用 C 编写的 Lua 库。因此,如果在 LuaSocket 的 C 代码为 运行ning 时有信号传入,您的信号处理代码将获胜't 运行 直到 returns。为了按照您想要的方式进行这项工作,必须修改 LuaSocket 以在它被信号中断时执行虚拟 lua_call
。然后这将触发您的 Lua 钩子到 运行。关于这个我打开了https://github.com/diegonehab/luasocket/issues/319