commit 3f5b277: [Minor] Allow to load Redis script from a file

Vsevolod Stakhov vsevolod at rspamd.com
Fri Mar 24 09:21:03 UTC 2023


Author: Vsevolod Stakhov
Date: 2023-03-24 09:19:30 +0000
URL: https://github.com/rspamd/rspamd/commit/3f5b2773a6afbe21f499b620f20416bcb4247a33 (HEAD -> master)

[Minor] Allow to load Redis script from a file

---
 lualib/lua_redis.lua | 34 ++++++++++++++++++++++++++++++++--
 1 file 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 = {}
 


More information about the Commits mailing list