-
Notifications
You must be signed in to change notification settings - Fork 44
/
beanstalk.cc
355 lines (294 loc) · 9.1 KB
/
beanstalk.cc
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
#include "beanstalk.hpp"
#include <stdexcept>
#include <sstream>
#include <iostream>
using namespace std;
namespace Beanstalk {
Job::Job()
: _id(0)
, _body() {
}
Job::Job(int64_t id, const char *data, size_t size)
: _id(id)
, _body(data, size) {
}
Job::Job(BSJ *job)
: Job() {
if (job) {
_body.assign(job->data, job->size);
_id = job->id;
bs_free_job(job);
}
}
string& Job::body() {
return _body;
}
int64_t Job::id() const {
return _id;
}
/* start helpers */
void parsedict(stringstream &stream, info_hash_t &dict) {
string key, value;
while(true) {
stream >> key;
if (stream.eof()) break;
if (key[0] == '-') continue;
stream >> value;
key.erase(--key.end());
dict[key] = value;
}
}
void parselist(stringstream &stream, info_list_t &list) {
string value;
while(true) {
stream >> value;
if (stream.eof()) break;
if (value[0] == '-') continue;
list.push_back(value);
}
}
/* end helpers */
Client::~Client() {
disconnect();
}
Client::Client()
: _handle(-1)
, _host()
, _port(0)
, _timeout_secs(0) {
}
Client::Client(std::string host, uint16_t port, float secs)
: Client() {
connect(host, port, secs);
}
void Client::connect(const std::string& host, uint16_t port, float secs) {
if (_handle > 0)
throw ConnectException("already connected to beanstalkd at ", _host, _port);
_host = host;
_port = port;
_timeout_secs = secs;
_handle = secs > 0 ? bs_connect_with_timeout((char *)_host.c_str(), _port, secs) : bs_connect((char*)host.c_str(), port);
if (_handle < 0) {
if (_handle == BS_STATUS_TIMED_OUT)
throw TimeoutException("connect");
throw ConnectException(_host, _port);
}
}
bool Client::is_connected() {
return _handle > 0;
}
bool Client::disconnect() {
if (_handle > 0 && bs_disconnect(_handle) == BS_STATUS_OK) {
_handle = -1;
return true;
}
return false;
}
void Client::version(int *major, int *minor, int *patch) {
bs_version(major, minor, patch);
}
std::string Client::version() {
int major, minor, patch;
version(&major, &minor, &patch);
return std::to_string(major) + "." + std::to_string(minor) + "." + std::to_string(patch);
}
void Client::reconnect() {
disconnect();
connect(_host, _port, _timeout_secs);
}
bool Client::use(const std::string& tube) {
return bs_use(_handle, (const char*)tube.c_str()) == BS_STATUS_OK;
}
bool Client::watch(const std::string& tube) {
return bs_watch(_handle, (const char*)tube.c_str()) == BS_STATUS_OK;
}
bool Client::ignore(const std::string& tube) {
return bs_ignore(_handle, (const char*)tube.c_str()) == BS_STATUS_OK;
}
int64_t Client::put(const std::string& body, uint32_t priority, uint32_t delay, uint32_t ttr) {
int64_t id = bs_put(_handle, priority, delay, ttr, (const char*)body.data(), body.size());
return (id > 0 ? id : 0);
}
int64_t Client::put(const char *body, size_t bytes, uint32_t priority, uint32_t delay, uint32_t ttr) {
int64_t id = bs_put(_handle, priority, delay, ttr, body, bytes);
return (id > 0 ? id : 0);
}
bool Client::del(const Job &job) {
return del(job.id());
}
bool Client::del(int64_t id) {
int response_code = bs_delete(_handle, id);
if (response_code == BS_STATUS_FAIL)
throw ConnectException(_host, _port);
return response_code == BS_STATUS_OK;
}
bool Client::reserve(Job &job) {
BSJ *bsj;
int response_code = bs_reserve(_handle, &bsj);
if (response_code == BS_STATUS_OK) {
job = bsj;
return true;
}
else if (response_code == BS_STATUS_FAIL) {
throw ConnectException(_host, _port);
}
return false;
}
bool Client::reserve(Job &job, uint32_t timeout) {
BSJ *bsj;
int response_code = bs_reserve_with_timeout(_handle, timeout, &bsj);
if (response_code == BS_STATUS_OK) {
job = bsj;
return true;
}
else if (response_code == BS_STATUS_TIMED_OUT) {
throw TimeoutException("reserve-with-timeout");
}
else if (response_code == BS_STATUS_FAIL) {
throw ConnectException(_host, _port);
}
return false;
}
bool Client::release(const Job &job, uint32_t priority, uint32_t delay) {
return release(job.id(), priority, delay);
}
bool Client::release(int64_t id, uint32_t priority, uint32_t delay) {
int response_code = bs_release(_handle, id, priority, delay);
if (response_code == BS_STATUS_FAIL)
throw ConnectException(_host, _port);
return response_code == BS_STATUS_OK;
}
bool Client::bury(const Job &job, uint32_t priority) {
return bury(job.id(), priority);
}
bool Client::bury(int64_t id, uint32_t priority) {
int response_code = bs_bury(_handle, id, priority);
if (response_code == BS_STATUS_FAIL)
throw ConnectException(_host, _port);
return response_code == BS_STATUS_OK;
}
bool Client::touch(const Job &job) {
return touch(job.id());
}
bool Client::touch(int64_t id) {
int response_code = bs_touch(_handle, id);
if (response_code == BS_STATUS_FAIL)
throw ConnectException(_host, _port);
return response_code == BS_STATUS_OK;
}
bool Client::peek(Job &job, int64_t id) {
BSJ *bsj;
if (bs_peek(_handle, id, &bsj) == BS_STATUS_OK) {
job = bsj;
return true;
}
return false;
}
bool Client::peek_ready(Job &job) {
BSJ *bsj;
if (bs_peek_ready(_handle, &bsj) == BS_STATUS_OK) {
job = bsj;
return true;
}
return false;
}
bool Client::peek_delayed(Job &job) {
BSJ *bsj;
if (bs_peek_delayed(_handle, &bsj) == BS_STATUS_OK) {
job = bsj;
return true;
}
return false;
}
bool Client::peek_buried(Job &job) {
BSJ *bsj;
if (bs_peek_buried(_handle, &bsj) == BS_STATUS_OK) {
job = bsj;
return true;
}
return false;
}
bool Client::kick(int bound) {
return bs_kick(_handle, bound) == BS_STATUS_OK;
}
string Client::list_tube_used() {
char *name;
string tube;
if (bs_list_tube_used(_handle, &name) == BS_STATUS_OK) {
tube.assign(name);
free(name);
}
return tube;
}
info_list_t Client::list_tubes() {
char *yaml, *data;
info_list_t tubes;
if (bs_list_tubes(_handle, &yaml) == BS_STATUS_OK) {
if ((data = strstr(yaml, "---"))) {
stringstream stream(data);
parselist(stream, tubes);
}
free(yaml);
}
return tubes;
}
info_list_t Client::list_tubes_watched() {
char *yaml, *data;
info_list_t tubes;
if (bs_list_tubes_watched(_handle, &yaml) == BS_STATUS_OK) {
if ((data = strstr(yaml, "---"))) {
stringstream stream(data);
parselist(stream, tubes);
}
free(yaml);
}
return tubes;
}
info_hash_t Client::stats() {
char *yaml, *data;
info_hash_t stats;
string key, value;
if (bs_stats(_handle, &yaml) == BS_STATUS_OK) {
if ((data = strstr(yaml, "---"))) {
stringstream stream(data);
parsedict(stream, stats);
}
free(yaml);
}
return stats;
}
info_hash_t Client::stats_job(int64_t id) {
char *yaml, *data;
info_hash_t stats;
string key, value;
if (bs_stats_job(_handle, id, &yaml) == BS_STATUS_OK) {
if ((data = strstr(yaml, "---"))) {
stringstream stream(data);
parsedict(stream, stats);
}
free(yaml);
}
return stats;
}
info_hash_t Client::stats_tube(const std::string& name) {
char *yaml, *data;
info_hash_t stats;
string key, value;
if (bs_stats_tube(_handle, (const char*)name.c_str(), &yaml) == BS_STATUS_OK) {
if ((data = strstr(yaml, "---"))) {
stringstream stream(data);
parsedict(stream, stats);
}
free(yaml);
}
return stats;
}
bool Client::ping() {
char *yaml;
if (bs_list_tubes(_handle, &yaml) == BS_STATUS_OK) {
free(yaml);
return true;
}
return false;
}
}