From ed24ce101331a2d18da3776934fbbaa3a00ba82e Mon Sep 17 00:00:00 2001 From: hwware Date: Mon, 14 Oct 2024 13:18:50 +0000 Subject: [PATCH] Add API for accessing Runtime args Signed-off-by: hwware --- src/module.c | 15 +++++++++++++++ src/redismodule.h | 2 ++ src/valkeymodule.h | 7 +++++++ tests/modules/moduleparameter.c | 7 ++++++- 4 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/module.c b/src/module.c index 5b9b0dcb0c..864c584165 100644 --- a/src/module.c +++ b/src/module.c @@ -3042,6 +3042,20 @@ client *moduleGetReplyClient(ValkeyModuleCtx *ctx) { } } +ValkeyModuleRunTimeArgs *VM_GetRunTimeArgs(ValkeyModuleCtx *ctx) { + client *c = moduleGetReplyClient(ctx); + if (c == NULL) return NULL; + + ValkeyModuleRunTimeArgs *args = zmalloc(sizeof(struct ValkeyModuleRunTimeArgs)); + int argc = ctx->module->loadmod->argc; + args->argv = argc ? zmalloc(sizeof(robj *) * argc) : NULL; + args->argc = argc; + for (int i = 0; i < argc; i++) { + args->argv[i] = (char *)ctx->module->loadmod->argv[i]->ptr; + } + return args; +} + /* Send an integer reply to the client, with the specified `long long` value. * The function always returns VALKEYMODULE_OK. */ int VM_ReplyWithLongLong(ValkeyModuleCtx *ctx, long long ll) { @@ -13595,6 +13609,7 @@ void moduleRegisterCoreAPI(void) { REGISTER_API(SetModuleAttribs); REGISTER_API(IsModuleNameBusy); REGISTER_API(WrongArity); + REGISTER_API(GetRunTimeArgs); REGISTER_API(ReplyWithLongLong); REGISTER_API(ReplyWithError); REGISTER_API(ReplyWithErrorFormat); diff --git a/src/redismodule.h b/src/redismodule.h index d692104d52..77b927ea46 100644 --- a/src/redismodule.h +++ b/src/redismodule.h @@ -334,6 +334,7 @@ #define RedisModuleCommandInfo ValkeyModuleCommandInfo #define RedisModuleCommandKeySpec ValkeyModuleCommandKeySpec #define RedisModuleCommandHistoryEntry ValkeyModuleCommandHistoryEntry +#define RedisModuleRunTimeArgs ValkeyModuleRunTimeArgs /* RedisModule APIs */ #define RedisModule_OnLoad ValkeyModule_OnLoad @@ -357,6 +358,7 @@ #define RedisModule_SetModuleAttribs ValkeyModule_SetModuleAttribs #define RedisModule_IsModuleNameBusy ValkeyModule_IsModuleNameBusy #define RedisModule_WrongArity ValkeyModule_WrongArity +#define RedisModule_GetRunTimeArgs ValkeyModule_GetRunTimeArgs #define RedisModule_ReplyWithLongLong ValkeyModule_ReplyWithLongLong #define RedisModule_GetSelectedDb ValkeyModule_GetSelectedDb #define RedisModule_SelectDb ValkeyModule_SelectDb diff --git a/src/valkeymodule.h b/src/valkeymodule.h index c2cdb2f0e7..b76d99f10b 100644 --- a/src/valkeymodule.h +++ b/src/valkeymodule.h @@ -147,6 +147,11 @@ typedef long long ustime_t; #define VALKEYMODULE_CONFIG_MEMORY (1ULL << 7) /* Indicates if this value can be set as a memory value */ #define VALKEYMODULE_CONFIG_BITFLAGS (1ULL << 8) /* Indicates if this value can be set as a multiple enum values */ +typedef struct ValkeyModuleRunTimeArgs { + int argc; + char **argv; +} ValkeyModuleRunTimeArgs; + /* StreamID type. */ typedef struct ValkeyModuleStreamID { uint64_t ms; @@ -967,6 +972,7 @@ VALKEYMODULE_API void (*ValkeyModule_SetModuleAttribs)(ValkeyModuleCtx *ctx, con VALKEYMODULE_ATTR; VALKEYMODULE_API int (*ValkeyModule_IsModuleNameBusy)(const char *name) VALKEYMODULE_ATTR; VALKEYMODULE_API int (*ValkeyModule_WrongArity)(ValkeyModuleCtx *ctx) VALKEYMODULE_ATTR; +VALKEYMODULE_API struct ValkeyModuleRunTimeArgs *(*ValkeyModule_GetRunTimeArgs)(ValkeyModuleCtx *ctx)VALKEYMODULE_ATTR; VALKEYMODULE_API int (*ValkeyModule_ReplyWithLongLong)(ValkeyModuleCtx *ctx, long long ll) VALKEYMODULE_ATTR; VALKEYMODULE_API int (*ValkeyModule_GetSelectedDb)(ValkeyModuleCtx *ctx) VALKEYMODULE_ATTR; VALKEYMODULE_API int (*ValkeyModule_SelectDb)(ValkeyModuleCtx *ctx, int newid) VALKEYMODULE_ATTR; @@ -1673,6 +1679,7 @@ static int ValkeyModule_Init(ValkeyModuleCtx *ctx, const char *name, int ver, in VALKEYMODULE_GET_API(SetModuleAttribs); VALKEYMODULE_GET_API(IsModuleNameBusy); VALKEYMODULE_GET_API(WrongArity); + VALKEYMODULE_GET_API(GetRunTimeArgs); VALKEYMODULE_GET_API(ReplyWithLongLong); VALKEYMODULE_GET_API(ReplyWithError); VALKEYMODULE_GET_API(ReplyWithErrorFormat); diff --git a/tests/modules/moduleparameter.c b/tests/modules/moduleparameter.c index 62d7c0cafc..0bd5ce3556 100644 --- a/tests/modules/moduleparameter.c +++ b/tests/modules/moduleparameter.c @@ -8,7 +8,12 @@ int GET_HELLO(ValkeyModuleCtx *ctx, ValkeyModuleString **argv, int argc) { VALKEYMODULE_NOT_USED(argv); VALKEYMODULE_NOT_USED(argc); - return ValkeyModule_ReplyWithSimpleString(ctx, "This is update module parameter test module"); + ValkeyModuleRunTimeArgs *ret = ValkeyModule_GetRunTimeArgs(ctx); + ValkeyModule_Log(ctx, "warning", "dbsize command arg number is %d", + ret->argc); + ValkeyModule_Log(ctx, "warning", "dbsize command arg 0 is %s", + ret->argv[0]); + return ValkeyModule_ReplyWithSimpleString(ctx, "Module runtime args test"); } int ValkeyModule_OnLoad(ValkeyModuleCtx *ctx, ValkeyModuleString **argv, int argc) {