diff options
author | Vsevolod Stakhov <vsevolod@rspamd.com> | 2023-03-24 09:19:30 +0000 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@rspamd.com> | 2023-03-24 09:19:30 +0000 |
commit | 3f5b2773a6afbe21f499b620f20416bcb4247a33 (patch) | |
tree | 0e4d7b6c7c2a090afd188570ca9a00487e94f94e /lualib/lua_redis.lua | |
parent | 2265ed4109cb4cee9ee1d5df0e9cf629923ce1cd (diff) | |
download | rspamd-3f5b2773a6afbe21f499b620f20416bcb4247a33.tar.gz rspamd-3f5b2773a6afbe21f499b620f20416bcb4247a33.zip |
[Minor] Allow to load Redis script from a file
Diffstat (limited to 'lualib/lua_redis.lua')
-rw-r--r-- | lualib/lua_redis.lua | 34 |
1 files changed, 32 insertions, 2 deletions
diff --git a/lualib/lua_redis.lua b/lualib/lua_redis.lua index d41805eca..6f59cbc4f 100644 --- a/lualib/lua_redis.lua +++ b/lualib/lua_redis.lua @@ -1246,8 +1246,9 @@ local function load_redis_script(script, cfg, ev_base, _) end end -local function add_redis_script(script, redis_params) - local caller = debug.getinfo(2) +local function add_redis_script(script, redis_params, caller_level) + if not caller_level then caller_level = 2 end + local caller = debug.getinfo(caller_level) local new_script = { caller = caller, @@ -1278,6 +1279,35 @@ local function add_redis_script(script, redis_params) end exports.add_redis_script = add_redis_script +-- Loads a Redis script from a file, strips comments, and passes the content to +-- `add_redis_script` function. +-- +-- @param filename The name of the file containing the Redis script. +-- @param redis_params The Redis parameters to use for this script. +-- @return The ID of the newly added Redis script. +-- +local function load_redis_script_from_file(filename, redis_params) + local lua_util = require "lua_util" + local rspamd_logger = require "rspamd_logger" + -- Read file contents + local file = io.open(filename, "r") + if not file then + rspamd_logger.errx("failed to open Redis script file: %s", filename) + return nil + end + local script = file:read("*all") + if not script then + rspamd_logger.errx("failed to load Redis script file: %s", filename) + return nil + end + file:close() + script = lua_util.strip_lua_comments(script) + + return add_redis_script(script, redis_params, 3) +end + +exports.load_redis_script_from_file = load_redis_script_from_file + local function exec_redis_script(id, params, callback, keys, args) local redis_args = {} |