Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update module args api #8

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/commands.def
Original file line number Diff line number Diff line change
Expand Up @@ -7404,6 +7404,29 @@ struct COMMAND_ARG MODULE_LOADEX_Args[] = {
{MAKE_ARG("args",ARG_TYPE_STRING,-1,"ARGS",NULL,NULL,CMD_ARG_OPTIONAL|CMD_ARG_MULTIPLE,0,NULL)},
};

/********** MODULE SET_ARGUMENT ********************/

#ifndef SKIP_CMD_HISTORY_TABLE
/* MODULE SET_ARGUMENT history */
#define MODULE_SET_ARGUMENT_History NULL
#endif

#ifndef SKIP_CMD_TIPS_TABLE
/* MODULE SET_ARGUMENT tips */
#define MODULE_SET_ARGUMENT_Tips NULL
#endif

#ifndef SKIP_CMD_KEY_SPECS_TABLE
/* MODULE SET_ARGUMENT key specs */
#define MODULE_SET_ARGUMENT_Keyspecs NULL
#endif

/* MODULE SET_ARGUMENT argument table */
struct COMMAND_ARG MODULE_SET_ARGUMENT_Args[] = {
{MAKE_ARG("name",ARG_TYPE_STRING,-1,NULL,NULL,NULL,CMD_ARG_NONE,0,NULL)},
{MAKE_ARG("arg",ARG_TYPE_STRING,-1,NULL,NULL,NULL,CMD_ARG_OPTIONAL|CMD_ARG_MULTIPLE,0,NULL)},
};

/********** MODULE UNLOAD ********************/

#ifndef SKIP_CMD_HISTORY_TABLE
Expand Down Expand Up @@ -7432,6 +7455,7 @@ struct COMMAND_STRUCT MODULE_Subcommands[] = {
{MAKE_CMD("list","Returns all loaded modules.","O(N) where N is the number of loaded modules.","4.0.0",CMD_DOC_NONE,NULL,NULL,"server",COMMAND_GROUP_SERVER,MODULE_LIST_History,0,MODULE_LIST_Tips,1,moduleCommand,2,CMD_ADMIN|CMD_NOSCRIPT,0,MODULE_LIST_Keyspecs,0,NULL,0)},
{MAKE_CMD("load","Loads a module.","O(1)","4.0.0",CMD_DOC_NONE,NULL,NULL,"server",COMMAND_GROUP_SERVER,MODULE_LOAD_History,0,MODULE_LOAD_Tips,0,moduleCommand,-3,CMD_NO_ASYNC_LOADING|CMD_ADMIN|CMD_NOSCRIPT|CMD_PROTECTED,0,MODULE_LOAD_Keyspecs,0,NULL,2),.args=MODULE_LOAD_Args},
{MAKE_CMD("loadex","Loads a module using extended parameters.","O(1)","7.0.0",CMD_DOC_NONE,NULL,NULL,"server",COMMAND_GROUP_SERVER,MODULE_LOADEX_History,0,MODULE_LOADEX_Tips,0,moduleCommand,-3,CMD_NO_ASYNC_LOADING|CMD_ADMIN|CMD_NOSCRIPT|CMD_PROTECTED,0,MODULE_LOADEX_Keyspecs,0,NULL,3),.args=MODULE_LOADEX_Args},
{MAKE_CMD("set-argument","Sets module arguments to new values during runtime.","O(1)","8.2.0",CMD_DOC_NONE,NULL,NULL,"server",COMMAND_GROUP_SERVER,MODULE_SET_ARGUMENT_History,0,MODULE_SET_ARGUMENT_Tips,0,moduleCommand,-3,CMD_NO_ASYNC_LOADING|CMD_ADMIN|CMD_NOSCRIPT|CMD_PROTECTED,0,MODULE_SET_ARGUMENT_Keyspecs,0,NULL,2),.args=MODULE_SET_ARGUMENT_Args},
{MAKE_CMD("unload","Unloads a module.","O(1)","4.0.0",CMD_DOC_NONE,NULL,NULL,"server",COMMAND_GROUP_SERVER,MODULE_UNLOAD_History,0,MODULE_UNLOAD_Tips,0,moduleCommand,3,CMD_NO_ASYNC_LOADING|CMD_ADMIN|CMD_NOSCRIPT|CMD_PROTECTED,0,MODULE_UNLOAD_Keyspecs,0,NULL,1),.args=MODULE_UNLOAD_Args},
{0}
};
Expand Down
10 changes: 10 additions & 0 deletions src/module.c
Original file line number Diff line number Diff line change
Expand Up @@ -3042,6 +3042,15 @@ client *moduleGetReplyClient(ValkeyModuleCtx *ctx) {
}
}

int VM_UpdateRunTimeArgs(ValkeyModuleCtx *ctx, int index, char *value) {
client *c = moduleGetReplyClient(ctx);
if (c == NULL) return VALKEYMODULE_OK;

ValkeyModuleString *o = createStringObject(value, strlen(value));
ctx->module->loadmod->argv[index] = o;
return VALKEYMODULE_OK;
}

/* 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) {
Expand Down Expand Up @@ -13560,6 +13569,7 @@ void moduleRegisterCoreAPI(void) {
REGISTER_API(SetModuleAttribs);
REGISTER_API(IsModuleNameBusy);
REGISTER_API(WrongArity);
REGISTER_API(UpdateRunTimeArgs);
REGISTER_API(ReplyWithLongLong);
REGISTER_API(ReplyWithError);
REGISTER_API(ReplyWithErrorFormat);
Expand Down
1 change: 1 addition & 0 deletions src/redismodule.h
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,7 @@
#define RedisModule_SetModuleAttribs ValkeyModule_SetModuleAttribs
#define RedisModule_IsModuleNameBusy ValkeyModule_IsModuleNameBusy
#define RedisModule_WrongArity ValkeyModule_WrongArity
#define RedisModule_UpdateRunTimeArgs ValkeyModule_UpdateRunTimeArgs
#define RedisModule_ReplyWithLongLong ValkeyModule_ReplyWithLongLong
#define RedisModule_GetSelectedDb ValkeyModule_GetSelectedDb
#define RedisModule_SelectDb ValkeyModule_SelectDb
Expand Down
2 changes: 2 additions & 0 deletions src/valkeymodule.h
Original file line number Diff line number Diff line change
Expand Up @@ -967,6 +967,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 int (*ValkeyModule_UpdateRunTimeArgs)(ValkeyModuleCtx *ctx, int index, char *value)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;
Expand Down Expand Up @@ -1673,6 +1674,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(UpdateRunTimeArgs);
VALKEYMODULE_GET_API(ReplyWithLongLong);
VALKEYMODULE_GET_API(ReplyWithError);
VALKEYMODULE_GET_API(ReplyWithErrorFormat);
Expand Down
1 change: 1 addition & 0 deletions tests/modules/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ TEST_MODULES = \
eventloop.so \
moduleconfigs.so \
moduleconfigstwo.so \
moduleparameter.so \
publish.so \
usercall.so \
postnotifications.so \
Expand Down
28 changes: 28 additions & 0 deletions tests/modules/moduleparameter.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include "valkeymodule.h"
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

int GET_HELLO(ValkeyModuleCtx *ctx, ValkeyModuleString **argv, int argc) {
VALKEYMODULE_NOT_USED(argv);
VALKEYMODULE_NOT_USED(argc);

ValkeyModule_UpdateRunTimeArgs(ctx, 0, "99");
return ValkeyModule_ReplyWithSimpleString(ctx, "Module runtime args test");
}

int ValkeyModule_OnLoad(ValkeyModuleCtx *ctx, ValkeyModuleString **argv, int argc) {
VALKEYMODULE_NOT_USED(argv);
VALKEYMODULE_NOT_USED(argc);

if (ValkeyModule_Init(ctx,"myhello",1,VALKEYMODULE_APIVER_1)
== VALKEYMODULE_ERR) return VALKEYMODULE_ERR;


if (ValkeyModule_CreateCommand(ctx,"hello.hi",
GET_HELLO,"fast",0,0,0) == VALKEYMODULE_ERR)
return VALKEYMODULE_ERR;

return VALKEYMODULE_OK;
}
16 changes: 16 additions & 0 deletions tests/unit/moduleapi/moduleconfigs.tcl
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
set testmodule [file normalize tests/modules/moduleconfigs.so]
set testmoduletwo [file normalize tests/modules/moduleconfigstwo.so]
set testmoduleparameter [file normalize tests/modules/moduleparameter.so]


start_server {tags {"modules"}} {
r module load $testmodule
Expand Down Expand Up @@ -243,5 +245,19 @@ start_server {tags {"modules"}} {
assert_equal [r config get moduleconfigs.memory_numeric] "moduleconfigs.memory_numeric 1024"
}
}

test {Module Update Args } {
r module load $testmoduleparameter 10 20 30

set t [r module list]
set modulename [lmap x [r module list] {dict get $x name}]
assert_not_equal [lsearch $modulename myhello] -1
string match "10 20 30" [lmap x [r module list] {dict get $x args}]
r hello.hi
string match "99 20 30" [lmap x [r module list] {dict get $x args}]
}

}



Loading