2

我正在运行 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)
4

1 回答 1

0

posix.signal通过在接收到信号时设置 Lua 挂钩来实现信号处理。Lua 钩子将在下一次执行 Lua 指令时运行。您的程序使用 LuaSocket,这是一个用 C 语言编写的 Lua 库。因此,如果在 LuaSocket 的 C 代码运行时收到信号,您的信号处理代码将在它返回之前不会运行。为了使这项工作以您想要的方式工作,必须修改 LuaSocket 以lua_call在它被信号中断时执行虚拟操作。这会触发你的 Lua 钩子运行。我为此打开了https://github.com/diegonehab/luasocket/issues/319

于 2020-11-21T23:01:37.433 回答