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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
use std::thread;
use std::time::Duration;
use actix::prelude::*;
use serde_json;
use redis;
use crate::application::RedisManager;
use crate::models::{job, queue, OcyResult, ServerInfo};
pub struct ApplicationActor {
client: redis::Client,
conn: Option<redis::Connection>,
}
impl Actor for ApplicationActor {
type Context = SyncContext<Self>;
fn started(&mut self, _ctx: &mut Self::Context) {
debug!("Ocypod application worker started");
}
}
impl ApplicationActor {
pub fn new(client: redis::Client) -> Self {
ApplicationActor { client, conn: None }
}
fn get_connection(&mut self) -> OcyResult<&redis::Connection> {
if self.is_connection_valid() {
return Ok(self.conn.as_ref().unwrap());
}
self.init_connection()
}
fn is_connection_valid(&self) -> bool {
match self.conn {
Some(ref conn) => {
match redis::cmd("PING").query(conn) {
Ok(()) => true,
Err(err) => {
warn!("Redis PING failed: {}", err);
false
}
}
}
None => false,
}
}
fn init_connection(&mut self) -> OcyResult<&redis::Connection> {
loop {
match self.client.get_connection() {
Ok(conn) => {
debug!("Established new Redis connection");
self.conn = Some(conn);
return Ok(self.conn.as_ref().unwrap());
},
Err(err) => {
warn!("Redis connection failed, retrying in 1s: {}", err);
thread::sleep(Duration::from_secs(5));
}
}
}
}
}
pub struct GetQueueNames;
impl Message for GetQueueNames {
type Result = OcyResult<Vec<String>>;
}
impl Handler<GetQueueNames> for ApplicationActor {
type Result = OcyResult<Vec<String>>;
fn handle(&mut self, _msg: GetQueueNames, _ctx: &mut Self::Context) -> Self::Result {
RedisManager::new(self.get_connection()?).queue_names()
}
}
pub struct GetTaggedJobs(pub String);
impl Message for GetTaggedJobs {
type Result = OcyResult<Vec<u64>>;
}
impl Handler<GetTaggedJobs> for ApplicationActor {
type Result = OcyResult<Vec<u64>>;
fn handle(&mut self, msg: GetTaggedJobs, _ctx: &mut Self::Context) -> Self::Result {
RedisManager::new(self.get_connection()?).tagged_job_ids(&msg.0)
}
}
pub struct CreateOrUpdateQueue(pub String, pub queue::Settings);
impl Message for CreateOrUpdateQueue {
type Result = OcyResult<bool>;
}
impl Handler<CreateOrUpdateQueue> for ApplicationActor {
type Result = OcyResult<bool>;
fn handle(&mut self, msg: CreateOrUpdateQueue, _ctx: &mut Self::Context) -> Self::Result {
RedisManager::new(self.get_connection()?).create_or_update_queue(&msg.0, &msg.1)
}
}
pub struct DeleteQueue(pub String);
impl Message for DeleteQueue {
type Result = OcyResult<bool>;
}
impl Handler<DeleteQueue> for ApplicationActor {
type Result = OcyResult<bool>;
fn handle(&mut self, msg: DeleteQueue, _ctx: &mut Self::Context) -> Self::Result {
RedisManager::new(self.get_connection()?).delete_queue(&msg.0)
}
}
pub struct GetQueueSettings(pub String);
impl Message for GetQueueSettings {
type Result = OcyResult<queue::Settings>;
}
impl Handler<GetQueueSettings> for ApplicationActor {
type Result = OcyResult<queue::Settings>;
fn handle(&mut self, msg: GetQueueSettings, _ctx: &mut Self::Context) -> Self::Result {
RedisManager::new(self.get_connection()?).queue_settings(&msg.0)
}
}
pub struct CreateJob(pub String, pub job::CreateRequest);
impl Message for CreateJob {
type Result = OcyResult<u64>;
}
impl Handler<CreateJob> for ApplicationActor {
type Result = OcyResult<u64>;
fn handle(&mut self, msg: CreateJob, _ctx: &mut Self::Context) -> Self::Result {
RedisManager::new(self.get_connection()?).create_job(&msg.0, &msg.1)
}
}
pub struct NextJob(pub String);
impl Message for NextJob {
type Result = OcyResult<Option<job::Payload>>;
}
impl Handler<NextJob> for ApplicationActor {
type Result = OcyResult<Option<job::Payload>>;
fn handle(&mut self, msg: NextJob, _ctx: &mut Self::Context) -> Self::Result {
RedisManager::new(self.get_connection()?).next_queued_job(&msg.0)
}
}
pub struct GetInfo;
impl Message for GetInfo {
type Result = OcyResult<ServerInfo>;
}
impl Handler<GetInfo> for ApplicationActor {
type Result = OcyResult<ServerInfo>;
fn handle(&mut self, _msg: GetInfo, _ctx: &mut Self::Context) -> Self::Result {
RedisManager::new(self.get_connection()?).server_info()
}
}
pub struct GetJobFields(pub u64, pub Option<Vec<job::Field>>);
impl Message for GetJobFields {
type Result = OcyResult<job::JobMeta>;
}
impl Handler<GetJobFields> for ApplicationActor {
type Result = OcyResult<job::JobMeta>;
fn handle(&mut self, msg: GetJobFields, _ctx: &mut Self::Context) -> Self::Result {
RedisManager::new(self.get_connection()?).job_fields(msg.0, msg.1.as_ref().map(|v| &**v))
}
}
pub struct GetJobStatus(pub u64);
impl Message for GetJobStatus {
type Result = OcyResult<job::Status>;
}
impl Handler<GetJobStatus> for ApplicationActor {
type Result = OcyResult<job::Status>;
fn handle(&mut self, msg: GetJobStatus, _ctx: &mut Self::Context) -> Self::Result {
RedisManager::new(self.get_connection()?).job_status(msg.0)
}
}
pub struct UpdateJob(pub u64, pub job::UpdateRequest);
impl Message for UpdateJob {
type Result = OcyResult<()>;
}
impl Handler<UpdateJob> for ApplicationActor {
type Result = OcyResult<()>;
fn handle(&mut self, msg: UpdateJob, _ctx: &mut Self::Context) -> Self::Result {
RedisManager::new(self.get_connection()?).update_job(msg.0, &msg.1)
}
}
pub struct Heartbeat(pub u64);
impl Message for Heartbeat {
type Result = OcyResult<()>;
}
impl Handler<Heartbeat> for ApplicationActor {
type Result = OcyResult<()>;
fn handle(&mut self, msg: Heartbeat, _ctx: &mut Self::Context) -> Self::Result {
RedisManager::new(self.get_connection()?).update_job_heartbeat(msg.0)
}
}
pub struct DeleteJob(pub u64);
impl Message for DeleteJob {
type Result = OcyResult<bool>;
}
impl Handler<DeleteJob> for ApplicationActor {
type Result = OcyResult<bool>;
fn handle(&mut self, msg: DeleteJob, _ctx: &mut Self::Context) -> Self::Result {
RedisManager::new(self.get_connection()?).delete_job(msg.0)
}
}
pub struct GetJobOutput(pub u64);
impl Message for GetJobOutput {
type Result = OcyResult<serde_json::Value>;
}
impl Handler<GetJobOutput> for ApplicationActor {
type Result = OcyResult<serde_json::Value>;
fn handle(&mut self, msg: GetJobOutput, _ctx: &mut Self::Context) -> Self::Result {
RedisManager::new(self.get_connection()?).job_output(msg.0)
}
}
pub struct SetJobOutput(pub u64, pub serde_json::Value);
impl Message for SetJobOutput {
type Result = OcyResult<()>;
}
impl Handler<SetJobOutput> for ApplicationActor {
type Result = OcyResult<()>;
fn handle(&mut self, msg: SetJobOutput, _ctx: &mut Self::Context) -> Self::Result {
RedisManager::new(self.get_connection()?).set_job_output(msg.0, &msg.1)
}
}
pub struct CheckJobTimeouts;
impl Message for CheckJobTimeouts {
type Result = OcyResult<Vec<u64>>;
}
impl Handler<CheckJobTimeouts> for ApplicationActor {
type Result = OcyResult<Vec<u64>>;
fn handle(&mut self, _msg: CheckJobTimeouts, _ctx: &mut Self::Context) -> Self::Result {
RedisManager::new(self.get_connection()?).check_job_timeouts()
}
}
pub struct CheckJobExpiry;
impl Message for CheckJobExpiry {
type Result = OcyResult<Vec<u64>>;
}
impl Handler<CheckJobExpiry> for ApplicationActor {
type Result = OcyResult<Vec<u64>>;
fn handle(&mut self, _msg: CheckJobExpiry, _ctx: &mut Self::Context) -> Self::Result {
RedisManager::new(self.get_connection()?).check_job_expiry()
}
}
pub struct CheckJobRetries;
impl Message for CheckJobRetries {
type Result = OcyResult<Vec<u64>>;
}
impl Handler<CheckJobRetries> for ApplicationActor {
type Result = OcyResult<Vec<u64>>;
fn handle(&mut self, _msg: CheckJobRetries, _ctx: &mut Self::Context) -> Self::Result {
RedisManager::new(self.get_connection()?).check_job_retries()
}
}
pub struct CheckDbIntegrity;
impl Message for CheckDbIntegrity {
type Result = OcyResult<()>;
}
impl Handler<CheckDbIntegrity> for ApplicationActor {
type Result = OcyResult<()>;
fn handle(&mut self, _msg: CheckDbIntegrity, _ctx: &mut Self::Context) -> Self::Result {
RedisManager::new(self.get_connection()?).check_db_integrity()
}
}
pub struct CheckHealth;
impl Message for CheckHealth {
type Result = OcyResult<()>;
}
impl Handler<CheckHealth> for ApplicationActor {
type Result = OcyResult<()>;
fn handle(&mut self, _msg: CheckHealth, _ctx: &mut Self::Context) -> Self::Result {
RedisManager::new(self.get_connection()?).check_ping()
}
}