forked from jafarlihi/cppq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.cpp
55 lines (42 loc) · 1.73 KB
/
example.cpp
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
#include "cppq.hpp"
#include <nlohmann/json.hpp>
const std::string TypeEmailDelivery = "email:deliver";
typedef struct {
int UserID;
std::string TemplateID;
} EmailDeliveryPayload;
void to_json(nlohmann::json& j, const EmailDeliveryPayload& p) {
j = nlohmann::json{{"UserID", p.UserID}, {"TemplateID", p.TemplateID}};
}
cppq::Task NewEmailDeliveryTask(EmailDeliveryPayload payload) {
nlohmann::json j = payload;
return cppq::Task{TypeEmailDelivery, j.dump(), 10};
}
void HandleEmailDeliveryTask(cppq::Task& task) {
nlohmann::json parsedPayload = nlohmann::json::parse(task.payload);
int userID = parsedPayload["UserID"];
std::string templateID = parsedPayload["TemplateID"];
nlohmann::json r;
r["Sent"] = true;
task.result = r.dump();
return;
}
int main(int argc, char *argv[]) {
cppq::registerHandler(TypeEmailDelivery, &HandleEmailDeliveryTask);
redisOptions redisOpts = {0};
REDIS_OPTIONS_SET_TCP(&redisOpts, "127.0.0.1", 6379);
redisContext *c = redisConnectWithOptions(&redisOpts);
if (c == NULL || c->err) {
std::cerr << "Failed to connect to Redis" << std::endl;
return 1;
}
cppq::Task task = NewEmailDeliveryTask(EmailDeliveryPayload{.UserID = 666, .TemplateID = "AH"});
cppq::Task task2 = NewEmailDeliveryTask(EmailDeliveryPayload{.UserID = 606, .TemplateID = "BH"});
cppq::Task task3 = NewEmailDeliveryTask(EmailDeliveryPayload{.UserID = 666, .TemplateID = "CH"});
cppq::enqueue(c, task, "default");
cppq::enqueue(c, task2, "high");
cppq::enqueue(c, task3, "default", cppq::scheduleOptions(std::chrono::system_clock::now() + std::chrono::minutes(1)));
cppq::pause(c, "default");
cppq::unpause(c, "default");
cppq::runServer(redisOpts, {{"low", 5}, {"default", 10}, {"high", 20}}, 1000);
}