|
| 1 | +#include "command_parser.h" |
| 2 | +#include "redismodule.h" |
| 3 | +#include "execution/run_info.h" |
| 4 | +#include "execution/DAG/dag.h" |
| 5 | +#include "execution/parsing/dag_parser.h" |
| 6 | +#include "execution/parsing/deprecated.h" |
| 7 | +#include "execution/parsing/model_commands_parser.h" |
| 8 | +#include "execution/parsing/script_commands_parser.h" |
| 9 | + |
| 10 | +int RedisAI_ExecuteCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc, |
| 11 | + RunCommand command, bool ro_dag) { |
| 12 | + |
| 13 | + int flags = RedisModule_GetContextFlags(ctx); |
| 14 | + bool blocking_not_allowed = (flags & (REDISMODULE_CTX_FLAGS_MULTI | REDISMODULE_CTX_FLAGS_LUA)); |
| 15 | + if (blocking_not_allowed) |
| 16 | + return RedisModule_ReplyWithError( |
| 17 | + ctx, "ERR Cannot run RedisAI command within a transaction or a LUA script"); |
| 18 | + |
| 19 | + RedisAI_RunInfo *rinfo; |
| 20 | + RAI_InitRunInfo(&rinfo); |
| 21 | + int status = REDISMODULE_ERR; |
| 22 | + |
| 23 | + switch (command) { |
| 24 | + case CMD_MODELRUN: |
| 25 | + rinfo->single_op_dag = 1; |
| 26 | + RAI_DagOp *modelRunOp; |
| 27 | + RAI_InitDagOp(&modelRunOp); |
| 28 | + rinfo->dagOps = array_append(rinfo->dagOps, modelRunOp); |
| 29 | + status = ParseModelRunCommand(rinfo, modelRunOp, argv, argc); |
| 30 | + break; |
| 31 | + case CMD_SCRIPTRUN: |
| 32 | + rinfo->single_op_dag = 1; |
| 33 | + RAI_DagOp *scriptRunOp; |
| 34 | + RAI_InitDagOp(&scriptRunOp); |
| 35 | + rinfo->dagOps = array_append(rinfo->dagOps, scriptRunOp); |
| 36 | + status = ParseScriptRunCommand(rinfo, scriptRunOp, argv, argc); |
| 37 | + break; |
| 38 | + case CMD_DAGRUN: |
| 39 | + status = ParseDAGRunCommand(rinfo, ctx, argv, argc, ro_dag); |
| 40 | + break; |
| 41 | + case CMD_MODELEXECUTE: |
| 42 | + rinfo->single_op_dag = 1; |
| 43 | + RAI_DagOp *modelExecuteOp; |
| 44 | + RAI_InitDagOp(&modelExecuteOp); |
| 45 | + rinfo->dagOps = array_append(rinfo->dagOps, modelExecuteOp); |
| 46 | + status = ParseModelExecuteCommand(rinfo, modelExecuteOp, argv, argc); |
| 47 | + break; |
| 48 | + case CMD_SCRIPTEXECUTE: |
| 49 | + rinfo->single_op_dag = 1; |
| 50 | + RAI_DagOp *scriptExecOp; |
| 51 | + RAI_InitDagOp(&scriptExecOp); |
| 52 | + rinfo->dagOps = array_append(rinfo->dagOps, scriptExecOp); |
| 53 | + status = ParseScriptExecuteCommand(rinfo, scriptExecOp, argv, argc); |
| 54 | + break; |
| 55 | + case CMD_DAGEXECUTE: |
| 56 | + status = ParseDAGExecuteCommand(rinfo, ctx, argv, argc, ro_dag); |
| 57 | + break; |
| 58 | + default: |
| 59 | + break; |
| 60 | + } |
| 61 | + if (status == REDISMODULE_ERR) { |
| 62 | + RedisModule_ReplyWithError(ctx, RAI_GetErrorOneLine(rinfo->err)); |
| 63 | + RAI_FreeRunInfo(rinfo); |
| 64 | + return REDISMODULE_OK; |
| 65 | + } |
| 66 | + rinfo->dagOpCount = array_len(rinfo->dagOps); |
| 67 | + |
| 68 | + rinfo->OnFinish = DAG_ReplyAndUnblock; |
| 69 | + rinfo->client = RedisModule_BlockClient(ctx, RedisAI_DagRun_Reply, NULL, RunInfo_FreeData, 0); |
| 70 | + if (DAG_InsertDAGToQueue(rinfo) != REDISMODULE_OK) { |
| 71 | + RedisModule_UnblockClient(rinfo->client, rinfo); |
| 72 | + return REDISMODULE_ERR; |
| 73 | + } |
| 74 | + return REDISMODULE_OK; |
| 75 | +} |
0 commit comments