-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqtt.c
253 lines (198 loc) · 5.9 KB
/
qtt.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <getopt.h>
#include <hiredis.h>
int hostPort;
char * hostName;
char * queueName;
char * maxWait;
static int verbose_flag;
int deque(redisContext * ctx, const char * queue, const char * wait,
char * into, size_t max);
int enque(redisContext * ctx, const char * queue, const int argc,
const char ** argv);
void help(const char * invocation, const int optlen,
const struct option opts[]);
int
main(int argc, char ** argv)
{
char * port;
if ((port = getenv("REDIS_PORT")) == NULL) {
hostPort = 6379;
} else {
hostPort = atoi(port);
}
if ((hostName = getenv("REDIS_HOST")) == NULL) {
hostName = "localhost";
}
if ((queueName = getenv("QTT_QUEUE")) == NULL) {
queueName = "qtt:job:queue";
}
if ((maxWait = getenv("QTT_BLOCK_WAIT")) == NULL) {
maxWait = "3";
}
int optcnt = 0;
static int help_flag = 0;
static struct option long_options[] = {
{"help", no_argument, &help_flag, 1},
{"verbose", no_argument, &verbose_flag, 1},
{"port", required_argument, 0, 'p'},
{"host", required_argument, 0, 'h'},
{"queue", required_argument, 0, 'q'},
{"wait", required_argument, 0, 'w'},
};
while(1) {
int option_index = 0;
int c = getopt_long_only(argc, argv, "p:h:q:w:", long_options, &option_index);
if(c == -1) {
break;
}
optcnt++;
switch(c) {
case 0:
if(long_options[option_index].flag != 0) {
break;
}
fprintf(stderr, "option %s", long_options[option_index].name);
if(optarg) {
fprintf(stderr, " with arg %s", optarg);
}
fprintf(stderr, "\n");
break;
case 'h':
hostName = optarg;
optcnt++;
break;
case 'p':
hostPort = atoi(optarg);
optcnt++;
break;
case 'w':
maxWait = optarg;
optcnt++;
break;
case 'q':
queueName = optarg;
optcnt++;
break;
case '?':
help_flag = 1;
break;
default:
fprintf(stderr, "suprise! c is %c\n", c);
abort();
}
}
if (help_flag) {
help(argv[0], sizeof(long_options) / sizeof(long_options[0]), long_options);
return -1;
}
int remaining = argc - 1 - optcnt;
char * vals[remaining];
if(verbose_flag) {
fprintf(stderr, "Remaining args %d\n", remaining);
}
if (remaining > 0) {
int ix, iv = 0;
for(ix = optind; ix < argc; ix++) {
if(argv[ix] && argv[ix][0] == '-') {
if(verbose_flag) {
fprintf(stderr, "unexpected short option '%s'\n", argv[ix]);
}
remaining--;
continue;
}
vals[iv++] = argv[ix];
}
}
if(verbose_flag) {
fprintf(stderr, "Connecting to %s:%d - q:%s - w:%s\n", hostName, hostPort,
queueName, maxWait);
}
redisContext * ctx = redisConnect(hostName, hostPort);
if (ctx == NULL || ctx->err) {
if (ctx) {
fprintf(stderr, "connection error: %s\n", ctx->errstr);
redisFree(ctx);
} else {
fprintf(stderr, "connection error: unable to allocate context\n");
}
return 1;
}
int success = -1;
if (remaining == 0) {
const int max = 255;
char buf[max];
if(deque(ctx, queueName, maxWait, buf, max) == 0) {
fprintf(stdout, "%s\n", buf);
success = 0;
}
} else {
success = enque(ctx, queueName, remaining, (const char **)&vals);
}
redisFree(ctx);
return success;
}
int
deque(
redisContext * ctx, const char * queue, const char * wait,
char * into, size_t max)
{
int errorCode = -1;
const char * action[] = {"BLPOP", queue, wait};
redisReply * reply = redisCommandArgv(ctx, 3, action, NULL);
if (reply->type == REDIS_REPLY_ARRAY && reply->elements == 2) {
if( snprintf(into, max, "%s", reply->element[1]->str) > 0) {
errorCode = 0;
}
}
freeReplyObject(reply);
return errorCode;
}
int
enque(redisContext * ctx, const char * queue,
const int argc, const char ** argv)
{
int errorCode = -1;
const char * action[] = {"RPUSH", queue, ""};
redisReply * reply;
for(int i = 0; i < argc; i++) {
action[2] = argv[i];
redisAppendCommandArgv(ctx, 3, action, NULL);
}
redisGetReply(ctx, (void **)&reply);
switch (reply->type) {
case REDIS_REPLY_INTEGER:
errorCode = 0;
break;
default:
errorCode = -1;
break;
}
freeReplyObject(reply);
return errorCode;
}
void
help(const char * invocation, const int optlen, const struct option opts[])
{
fprintf(stderr, "Usage: %s [flags] [values]\n\n", invocation);
fprintf(stderr, " Flags:\n");
for(int ix = 0; ix < optlen; ix++) {
int v = opts[ix].val;
fprintf(stderr, " --%-5s%4s%c%s\n", opts[ix].name,
v > 1 ? "| -" : "",
v > 1 ? v : ' ',
opts[ix].has_arg ? " <arg>" : "");
}
fprintf(stderr,
"\n\nEnvironment variables can be used for default values:\n"
" REDIS_HOST\n"
" REDIS_PORT\n"
" QTT_QUEUE (redis key name of the queue)\n"
" QTT_BLOCK_WAIT (seconds to block while waiting for item in queue)\n");
fprintf(stderr,
"\n\nWhen no values are passed in %s will block for the configured"
" time, waiting for items to appear on the queue.\nWhen values are"
" passed, they are added to the queue.\n", invocation);
}