This repository has been archived by the owner on May 21, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpraetor.cpp
228 lines (188 loc) · 4.35 KB
/
praetor.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
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
/**
* COPYRIGHT 2014 (C) Jason Volk
* COPYRIGHT 2014 (C) Svetlana Tkachenko
*
* DISTRIBUTED UNDER THE GNU GENERAL PUBLIC LICENSE (GPL) (see: LICENSE)
*/
// libircbot irc::bot::
#include "ircbot/bot.h"
using namespace irc::bot;
// SPQF
#include "log.h"
#include "vote.h"
#include "votes.h"
#include "vdb.h"
#include "praetor.h"
Praetor::Praetor(Bot &bot,
Vdb &vdb):
bot(bot),
vdb(vdb),
interrupted(false),
thread(&Praetor::worker,this),
init_thread(&Praetor::init,this)
{
}
Praetor::~Praetor()
noexcept
{
interrupted.store(true,std::memory_order_release);
cond.notify_all();
init_thread.join();
thread.join();
}
void Praetor::init()
{
const std::lock_guard<Bot> l(bot);
bot.set_tls_context();
std::cout << "[Praetor]: Initiating the schedule."
<< " Reading " << vdb.count() << " votes..."
<< std::endl;
auto it(vdb.cbegin(stldb::SNAPSHOT));
const auto end(vdb.cend());
for(; it != end && !interrupted.load(std::memory_order_consume); ++it) try
{
add(Adoc(it->second));
}
catch(const Exception &e)
{
const auto &id(it->first);
std::cerr << "[Praetor]: Init reading #" << id
<< ": \033[1;31m" << e << "\033[0m"
<< std::endl;
}
}
void Praetor::worker()
{
{
const std::lock_guard<Bot> l(bot);
bot.set_tls_context();
}
while(!interrupted.load(std::memory_order_consume)) try
{
process();
}
catch(const Internal &e)
{
std::cerr << "[Praetor]: \033[1;41m" << e << "\033[0m"
<< std::endl;
}
}
void Praetor::process()
{
using system_clock = std::chrono::system_clock;
std::unique_lock<decltype(mutex)> lock(mutex);
cond.wait_until(lock,system_clock::from_time_t(sched.next_abs()));
while(sched.next_rel() <= 0)
{
const id_t id(sched.next_id());
sched.pop();
const unlock_guard<decltype(lock)> unlock(lock);
if(!process(id))
{
const time_t retry_absolute(time(NULL) + 300);
add(id,retry_absolute);
}
}
}
bool Praetor::process(const id_t &id)
{
const std::unique_lock<Bot> lock(bot);
const std::unique_ptr<Vote> vote(vdb.get(id));
return process(*vote);
}
bool Praetor::process(Vote &vote)
noexcept try
{
const auto &chans(get_chans());
if(!chans.has(vote.get_chan_name()))
return false;
vote.expire();
return true;
}
catch(const std::exception &e)
{
std::cerr << "\033[1;31m"
<< "[Praetor]:"
<< " Vote #" << vote.get_id()
<< " error: " << e.what()
<< "\033[0m"
<< std::endl;
return false;
}
void Praetor::add(const Adoc &doc)
try
{
const auto id(lex_cast<id_t>(doc["id"]));
const auto ended(secs_cast(doc["ended"]));
const auto expiry(secs_cast(doc["expiry"]));
const auto cfgfor(secs_cast(doc["cfg.for"]));
if(expiry || (cfgfor <= 0) || !ended || doc.has("reason"))
return;
const time_t absolute(ended + cfgfor);
printf("%lu [Praetor]: Scheduling #%u [type: %s chan: %s cfgfor: %ld ended: %ld] absolute: %ld relative: %ld\n",
time(NULL),
id,
doc["type"].c_str(),
doc["chan"].c_str(),
cfgfor,
ended,
absolute,
absolute - time(NULL));
add(id,absolute);
}
catch(const std::exception &e)
{
std::cerr << "\033[1;31m"
<< "[Praetor]: add(doc): "
<< " Vote #" << doc["id"]
<< " error: " << e.what()
<< "\033[0m"
<< std::endl;
}
void Praetor::add(std::unique_ptr<Vote> &&vote)
try
{
const id_t &id(vote->get_id());
const auto &cfg(vote->get_cfg());
const auto cfgfor(secs_cast(cfg["for"]));
if(cfgfor <= 0)
return;
const time_t absolute(time(NULL) + cfgfor);
add(id,absolute);
}
catch(const std::exception &e)
{
std::cerr << "\033[1;31m"
<< "[Praetor]: add(vote): "
<< " Vote #" << vote->get_id()
<< " error: " << e.what()
<< "\033[0m"
<< std::endl;
}
void Praetor::add(const id_t &id,
const time_t &absolute)
{
const std::lock_guard<std::mutex> l(mutex);
sched.add(id,absolute);
cond.notify_one();
}
id_t Schedule::pop()
{
const auto ret = std::get<id_t>(sched.at(0));
sched.pop_front();
return ret;
}
void Schedule::add(const id_t &id,
const time_t &absolute)
{
sched.emplace_back(std::make_tuple(id,absolute));
sort();
}
void Schedule::sort()
{
std::sort(sched.begin(),sched.end(),[]
(const auto &a, const auto &b)
{
return std::get<time_t>(a) < std::get<time_t>(b);
});
}