1 /* Timer API example -- Register and handle timer events
2  *
3  * -----------------------------------------------------------------------------
4  *
5  * Copyright (c) 2018, Salvatore Sanfilippo <antirez at gmail dot com>
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions are met:
10  *
11  *   * Redistributions of source code must retain the above copyright notice,
12  *     this list of conditions and the following disclaimer.
13  *   * Redistributions in binary form must reproduce the above copyright
14  *     notice, this list of conditions and the following disclaimer in the
15  *     documentation and/or other materials provided with the distribution.
16  *   * Neither the name of Redis nor the names of its contributors may be used
17  *     to endorse or promote products derived from this software without
18  *     specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #define REDISMODULE_EXPERIMENTAL_API
34 #include "../redismodule.h"
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <ctype.h>
38 #include <string.h>
39 
40 /* Timer callback. */
41 void timerHandler(RedisModuleCtx *ctx, void *data) {
42     REDISMODULE_NOT_USED(ctx);
43     printf("Fired %s!\n", data);
44     RedisModule_Free(data);
45 }
46 
47 /* HELLOTIMER.TIMER*/
48 int TimerCommand_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
49     REDISMODULE_NOT_USED(argv);
50     REDISMODULE_NOT_USED(argc);
51 
52     for (int j = 0; j < 10; j++) {
53         int delay = rand() % 5000;
54         char *buf = RedisModule_Alloc(256);
55         snprintf(buf,256,"After %d", delay);
56         RedisModuleTimerID tid = RedisModule_CreateTimer(ctx,delay,timerHandler,buf);
57         REDISMODULE_NOT_USED(tid);
58     }
59     return RedisModule_ReplyWithSimpleString(ctx, "OK");
60 }
61 
62 /* This function must be present on each Redis module. It is used in order to
63  * register the commands into the Redis server. */
64 int RedisModule_OnLoad(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
65     REDISMODULE_NOT_USED(argv);
66     REDISMODULE_NOT_USED(argc);
67 
68     if (RedisModule_Init(ctx,"hellotimer",1,REDISMODULE_APIVER_1)
69         == REDISMODULE_ERR) return REDISMODULE_ERR;
70 
71     if (RedisModule_CreateCommand(ctx,"hellotimer.timer",
72         TimerCommand_RedisCommand,"readonly",0,0,0) == REDISMODULE_ERR)
73         return REDISMODULE_ERR;
74 
75     return REDISMODULE_OK;
76 }
77