From 500fac7fd7861f926cbe2735d43e1cfc9b9c7a9e Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Thu, 1 Feb 2024 11:49:07 +0000 Subject: [Fix] Resolve issue with bayes stat in `rspamadm` mode --- lualib/lua_bayes_redis.lua | 61 ++++++++++++++++++---------------- src/libstat/backends/redis_backend.cxx | 13 ++++++-- 2 files changed, 42 insertions(+), 32 deletions(-) diff --git a/lualib/lua_bayes_redis.lua b/lualib/lua_bayes_redis.lua index 753399705..782e6fc47 100644 --- a/lualib/lua_bayes_redis.lua +++ b/lualib/lua_bayes_redis.lua @@ -134,40 +134,43 @@ exports.lua_bayes_init_statfile = function(classifier_ucl, statfile_ucl, symbol, revision = 0, -- number of learns } local cursor = 0 - rspamd_config:add_periodic(ev_base, 0.0, function(cfg, _) - local function stat_redis_cb(err, data) - lua_util.debugm(N, cfg, 'stat redis cb: %s, %s', err, data) + if ev_base then + rspamd_config:add_periodic(ev_base, 0.0, function(cfg, _) - if err then - logger.warn(cfg, 'cannot get bayes statistics for %s: %s', symbol, err) - else - local new_cursor = data[1] - current_data.users = current_data.users + data[2] - current_data.revision = current_data.revision + data[3] - if new_cursor == 0 then - -- Done iteration - final_data = lua_util.shallowcopy(current_data) - current_data = { - users = 0, - revision = 0, - } - lua_util.debugm(N, cfg, 'final data: %s', final_data) - stat_periodic_cb(cfg, final_data) - end + local function stat_redis_cb(err, data) + lua_util.debugm(N, cfg, 'stat redis cb: %s, %s', err, data) - cursor = new_cursor + if err then + logger.warn(cfg, 'cannot get bayes statistics for %s: %s', symbol, err) + else + local new_cursor = data[1] + current_data.users = current_data.users + data[2] + current_data.revision = current_data.revision + data[3] + if new_cursor == 0 then + -- Done iteration + final_data = lua_util.shallowcopy(current_data) + current_data = { + users = 0, + revision = 0, + } + lua_util.debugm(N, cfg, 'final data: %s', final_data) + stat_periodic_cb(cfg, final_data) + end + + cursor = new_cursor + end end - end - lua_redis.exec_redis_script(stat_script_id, - { ev_base = ev_base, cfg = cfg, is_write = false }, - stat_redis_cb, { tostring(cursor), - symbol, - is_spam and "learns_spam" or "learns_ham", - tostring(max_users) }) - return statfile_ucl.monitor_timeout or classifier_ucl.monitor_timeout or 30.0 - end) + lua_redis.exec_redis_script(stat_script_id, + { ev_base = ev_base, cfg = cfg, is_write = false }, + stat_redis_cb, { tostring(cursor), + symbol, + is_spam and "learns_spam" or "learns_ham", + tostring(max_users) }) + return statfile_ucl.monitor_timeout or classifier_ucl.monitor_timeout or 30.0 + end) + end return gen_classify_functor(redis_params, classify_script_id), gen_learn_functor(redis_params, learn_script_id) end diff --git a/src/libstat/backends/redis_backend.cxx b/src/libstat/backends/redis_backend.cxx index cd0c37965..14e821029 100644 --- a/src/libstat/backends/redis_backend.cxx +++ b/src/libstat/backends/redis_backend.cxx @@ -542,9 +542,16 @@ rspamd_redis_init(struct rspamd_stat_ctx *ctx, ucl_object_push_lua(L, st->stcf->opts, false); lua_pushstring(L, backend->stcf->symbol); lua_pushboolean(L, backend->stcf->is_spam); - auto **pev_base = (struct ev_loop **) lua_newuserdata(L, sizeof(struct ev_loop *)); - *pev_base = ctx->event_loop; - rspamd_lua_setclass(L, "rspamd{ev_base}", -1); + + /* Push event loop if there is one available (e.g. we are not in rspamadm mode) */ + if (ctx->event_loop) { + auto **pev_base = (struct ev_loop **) lua_newuserdata(L, sizeof(struct ev_loop *)); + *pev_base = ctx->event_loop; + rspamd_lua_setclass(L, "rspamd{ev_base}", -1); + } + else { + lua_pushnil(L); + } /* Store backend in random cookie */ char *cookie = (char *) rspamd_mempool_alloc(cfg->cfg_pool, 16); -- cgit v1.2.3