Skip to content

Commit 13b1174

Browse files
committed
Fail Worker Application After 3 Failures, 3 Seconds Apart
1 parent 5c50da2 commit 13b1174

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

src/worker.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
use std::{collections::HashSet, env};
2-
1+
use std::{collections::HashSet, env, thread, time};
32
use anyhow::{anyhow, Result};
43
use log::error;
54
use redis_work_queue::{Item, KeyPrefix, WorkQueue};
@@ -46,8 +45,7 @@ pub async fn main() -> Result<()> {
4645
env::var("PINGS_REMOVE_ROUTE").expect("PINGS_REMOVE_ROUTE must be set"),
4746
)?;
4847

49-
work_loop(queue, db_pool, pings).await?;
50-
Ok(())
48+
work_loop(queue, db_pool, pings).await
5149
}
5250

5351
async fn get_simple_data(
@@ -263,15 +261,24 @@ pub async fn work_loop(
263261
db_pool: Pool<Postgres>,
264262
pings: PingClient,
265263
) -> Result<()> {
264+
let mut queue_connect_failure = 0;
265+
let three_sec = time::Duration::from_secs(3);
266266
loop {
267267
// Wait for a job with no timeout and a lease time of 5 seconds.
268268
let job: Item = match queue.get_job().await {
269269
Ok(job) => job,
270270
Err(err) => {
271271
error!("Failed to Get Job: {}", err);
272+
queue_connect_failure += 1;
273+
thread::sleep(three_sec);
274+
if queue_connect_failure >= 3 {
275+
error!("Failed to Fetch Job 3+ Times! Failing...");
276+
return Err(anyhow!("Fetch Job Failed 3 Times. Is Redis Running?"));
277+
}
272278
continue;
273279
}
274280
};
281+
queue_connect_failure = 0;
275282
match work(&job, &db_pool, &pings, &mut queue).await {
276283
// Mark successful jobs as complete
277284
Ok(()) => {

0 commit comments

Comments
 (0)