-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpg_redispub.c
37 lines (32 loc) · 1.1 KB
/
pg_redispub.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include "postgres.h"
#include "fmgr.h"
#include <hiredis/hiredis.h>
#include "utils/builtins.h"
PG_MODULE_MAGIC;
PG_FUNCTION_INFO_V1(redispub);
Datum redispub(PG_FUNCTION_ARGS) {
redisReply *reply = NULL;
struct timeval timeout = { 0, 500000 }; /* 0.5 minutes */
char *channel = text_to_cstring(PG_GETARG_TEXT_P(0));
char *msg = text_to_cstring(PG_GETARG_TEXT_P(1));
redisContext *ctx = redisConnectWithTimeout("127.0.0.1", 6379, timeout);
if (ctx == NULL || ctx->err) {
if (ctx) {
ereport(WARNING, (errcode(ERRCODE_WARNING), errmsg("failed to connect to redis: %s", ctx->errstr)));
redisFree(ctx);
} else {
ereport(WARNING, (errcode(ERRCODE_WARNING), errmsg("failed to connect to redis: can't allocate redis context")));
}
PG_RETURN_BOOL(false);
}
reply = redisCommand(ctx, "PUBLISH %s %s", channel, msg);
freeReplyObject(reply);
if (ctx->err) {
ereport(WARNING, (errcode(ERRCODE_WARNING), errmsg("failed to PUBLISH to redis: %s", ctx->errstr)));
redisFree(ctx);
PG_RETURN_BOOL(false);
} else {
redisFree(ctx);
PG_RETURN_BOOL(true);
}
}