Redis调试LUA脚本

发布于 2024-01-10  41.4k 次阅读


可以使用下面的步骤创建一个新的debug会话:

  • 在本地创建一个Lua脚本
  • 使用redis-cli,通过--ldb参数进入到debug模式,使用--eval参数指定需要debugLua脚本

d:\d.lua 脚本内容如下:

local keys = KEYS
for _, key in ipairs(keys) do
	print('keys:', key, keys)
    local fields = redis.call('HKEYS', key)
    for _, field in ipairs(fields) do
        local value = redis.call('HGET', key, field)
        local success, schedule = pcall(cjson.decode, value)
        if success then
            if schedule and schedule.state == 'Executing' then
                redis.call('HDEL', key, field)
                print('Deleted:', key, field)
            else
                print('No action needed:', key, field, schedule)
            end
        else
            print('Failed to decode JSON:', key, field, value)
        end
    end
end

通过D:\Program\Redis-6.2.10-Windows-x64>redis-cli -h localhost --ldb --eval d:/d.lua DATA:SCHEDULE:LAST:GetEvents:0 启动,输入 "n" 调试下一步


Lua debugging session started, please use:
quit    -- End the session.
restart -- Restart the script in debug mode again.
help    -- Show Lua script debugging commands.

* Stopped at 1, stop reason = step over
-> 1   local keys = KEYS
lua debugger> n
* Stopped at 2, stop reason = step over
-> 2   for _, key in ipairs(keys) do
lua debugger> n
* Stopped at 3, stop reason = step over
-> 3    print('keys:',key,keys)
lua debugger> n
* Stopped at 4, stop reason = step over
-> 4       local fields = redis.call('HKEYS', key)
lua debugger> n
<redis> HKEYS DATA:SCHEDULE:LAST:GetEvents:0
<reply> ["A1128IEUG5Z45F","a1f646a1795a4c98b8c94d51d559d3f0","A15BSRPFWB9KKL","A11JNNRR8HJRN0","A10YJ2U6PSJI0R","A10HEQFM4IOK7F","A15W5WP8U704O4","5e3737ddf0d045a08a2cb566442fa2bc","A15Q089GFUYYGA","A11M1QP5UBM8SO","A17THTTG7XBFWH","A16QC0OZVGI35S","A15EMH ...
<hint> The above reply was trimmed. Use 'maxlen 0' to disable trimming.
* Stopped at 5, stop reason = step over
-> 5       for _, field in ipairs(fields) do
lua debugger> n
* Stopped at 6, stop reason = step over
-> 6           local value = redis.call('HGET', key, field)
lua debugger> n
<redis> HGET DATA:SCHEDULE:LAST:GetEvents:0 A1128IEUG5Z45F
<reply> ""
* Stopped at 7, stop reason = step over
-> 7           local success, schedule = pcall(cjson.decode, value)
lua debugger> n
* Stopped at 8, stop reason = step over
-> 8           if success then
lua debugger> n
* Stopped at 16, stop reason = step over
-> 16              print('Failed to decode JSON:', key, field, value)
lua debugger> n
* Stopped at 5, stop reason = step over
-> 5       for _, field in ipairs(fields) do
lua debugger> n
* Stopped at 6, stop reason = step over
-> 6           local value = redis.call('HGET', key, field)
lua debugger> n
<redis> HGET DATA:SCHEDULE:LAST:GetEvents:0 a1f646a1795a4c98b8c94d51d559d3f0
<reply> "{\"@class\":\"org.cmb.spapi.domain.entity.DataSchedule\",\"id\":[\"java.lang.Long\",532556000115345408],\"sellerId\":\"\",\"operation\":\"GetEvents\",\"begin\":null,\"end\":null,\"stage\":0,\"failedTimes\":null,\"state\":\"Executing\",\"createAt\" ...
* Stopped at 7, stop reason = step over
-> 7           local success, schedule = pcall(cjson.decode, value)
lua debugger> n
* Stopped at 8, stop reason = step over
-> 8           if success then
lua debugger> n
* Stopped at 9, stop reason = step over
-> 9               if schedule and schedule.state == 'Executing' then
lua debugger> n
* Stopped at 10, stop reason = step over
-> 10                  redis.call('HDEL', key, field)
lua debugger> n
<redis> HDEL DATA:SCHEDULE:LAST:GetEvents:0 a1f646a1795a4c98b8c94d51d559d3f0
<reply> 1
* Stopped at 11, stop reason = step over
-> 11                  print('Deleted:', key, field)
lua debugger> n
* Stopped at 5, stop reason = step over
-> 5       for _, field in ipairs(fields) do
lua debugger> n
* Stopped at 6, stop reason = step over
-> 6           local value = redis.call('HGET', key, field)
lua debugger> n
<redis> HGET DATA:SCHEDULE:LAST:GetEvents:0 A15BSRPFWB9KKL
<reply> "{\"@class\":\"org.cmb.spapi.domain.entity.DataSchedule\",\"id\":[\"java.lang.Long\",523336722400977664],\"sellerId\":\"A15BSRPFWB9KKL\",\"operation\":\"GetEvents\",\"begin\":\"2021-12-17T03:13:17.746Z\",\"end\":\"2021-12-17T06:13:17.746Z\",\"stage ...
* Stopped at 7, stop reason = step over
-> 7           local success, schedule = pcall(cjson.decode, value)