| 4502 | } |
| 4503 | |
| 4504 | static PyObject* gearsTimeEvent(PyObject *cls, PyObject *args){ |
| 4505 | if(PyTuple_Size(args) < 2 || PyTuple_Size(args) > 3){ |
| 4506 | PyErr_SetString(GearsError, "not enough arguments for time event"); |
| 4507 | return NULL; |
| 4508 | } |
| 4509 | PyObject* callback = PyTuple_GetItem(args, 1); |
| 4510 | if(!PyFunction_Check(callback)){ |
| 4511 | PyErr_SetString(GearsError, "callback must be a function"); |
| 4512 | return NULL; |
| 4513 | } |
| 4514 | PyObject* timeInSec = PyTuple_GetItem(args, 0); |
| 4515 | if(!PyLong_Check(timeInSec)) { |
| 4516 | PyErr_SetString(GearsError, "time argument must be a long"); |
| 4517 | return NULL; |
| 4518 | } |
| 4519 | |
| 4520 | PythonThreadCtx* ptctx = GetPythonThreadCtx(); |
| 4521 | if(!ptctx->currSession){ |
| 4522 | PyErr_SetString(GearsError, "can not create time event on a python created thread"); |
| 4523 | return NULL; |
| 4524 | } |
| 4525 | RedisModuleCtx* ctx = RedisModule_GetThreadSafeContext(NULL); |
| 4526 | RedisModule_AutoMemory(ctx); |
| 4527 | RedisModuleString* keyNameStr = NULL; |
| 4528 | if(PyTuple_Size(args) == 3){ |
| 4529 | PyObject* keyName = PyTuple_GetItem(args, 2); |
| 4530 | if(PyUnicode_Check(keyName)){ |
| 4531 | size_t len; |
| 4532 | const char* keyNameCStr = PyUnicode_AsUTF8AndSize(keyName, &len); |
| 4533 | keyNameStr = RedisModule_CreateString(ctx, keyNameCStr, len); |
| 4534 | } |
| 4535 | } |
| 4536 | long period = PyLong_AsLong(timeInSec); |
| 4537 | |
| 4538 | TimerData* td = RG_ALLOC(sizeof(*td)); |
| 4539 | td->status = TE_STATUS_RUNNING; |
| 4540 | td->period = period; |
| 4541 | td->callback = callback; |
| 4542 | td->session = PythonSessionCtx_ShallowCopy(ptctx->currSession); |
| 4543 | Py_INCREF(callback); |
| 4544 | |
| 4545 | RedisGears_LockHanlderAcquire(ctx); |
| 4546 | |
| 4547 | if(keyNameStr){ |
| 4548 | RedisModuleKey* key = RedisModule_OpenKey(ctx, keyNameStr, REDISMODULE_WRITE); |
| 4549 | if(RedisModule_KeyType(key) != REDISMODULE_KEYTYPE_EMPTY){ |
| 4550 | TimeEvent_Free(td); |
| 4551 | RedisGears_LockHanlderRelease(ctx); |
| 4552 | RedisModule_FreeThreadSafeContext(ctx); |
| 4553 | return Py_False; |
| 4554 | } |
| 4555 | RedisModule_ModuleTypeSetValue(key, TimeEventType, td); |
| 4556 | } |
| 4557 | |
| 4558 | td->id = RedisModule_CreateTimer(ctx, period * 1000, TimeEvent_Callback, td); |
| 4559 | |
| 4560 | RedisGears_LockHanlderRelease(ctx); |
| 4561 | RedisModule_FreeThreadSafeContext(ctx); |
nothing calls this directly
no test coverage detected