Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion tests/testing_complete.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
SUCCESS: All tests passed on branch: bug/crashing-worker-lower-parallel-blocks
SUCCESS: All tests passed on branch: bug/fix-redis-stale-connection
41 changes: 41 additions & 0 deletions tests/testthat/test_worker.R
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,47 @@ test_that("get_from_in_progress_queue returns message when queue has items", {
expect_equal(result[[2]], test_payload)
})

test_that("in-progress removal reconnects after a stale Redis connection", {
redis_env <- new.env(parent = globalenv())
sys.source("worker/redis_client.R", envir = redis_env)

calls <- new.env(parent = emptyenv())
calls$initial <- 0
calls$replacement <- 0

stale_conn <- list(
LREM = function(queue, count, element) {
calls$initial <- calls$initial + 1
stop("Failure communicating with the Redis server")
}
)
replacement_conn <- list(
LREM = function(queue, count, element) {
calls$replacement <- calls$replacement + 1
return(1)
}
)
redis_env$connect_to_redis <- function(...) replacement_conn

had_global_conn <- exists("redis_conn", envir = .GlobalEnv, inherits = FALSE)
if (had_global_conn) {
previous_global_conn <- get("redis_conn", envir = .GlobalEnv, inherits = FALSE)
}
on.exit({
if (had_global_conn) {
assign("redis_conn", previous_global_conn, envir = .GlobalEnv)
} else if (exists("redis_conn", envir = .GlobalEnv, inherits = FALSE)) {
rm("redis_conn", envir = .GlobalEnv)
}
}, add = TRUE)

redis_env$remove_from_in_progress_queue(stale_conn, "test-payload")

expect_equal(calls$initial, 1)
expect_equal(calls$replacement, 1)
expect_identical(get("redis_conn", envir = .GlobalEnv), replacement_conn)
})

# test_that("Pipeline worker processes message from in_progress queue when custom message has in_progress queue name", {
# redis_payload <- "/home/pipeline/tests/data/hg38_tsv_redis_message_in_progress.json"
# output <- system(glue::glue("Rscript worker/pipeline_worker.R --custom_message_file {redis_payload}"),
Expand Down
21 changes: 20 additions & 1 deletion worker/pipeline_worker.R
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ main <- function() {
flog.info("Starting pipeline worker")

while (TRUE) {
gwas_info <- NULL

# If the stop_processing file exists, hang forever
stop_processing_file <- file.exists(glue::glue("{data_dir}/stop_processing"))
if (stop_processing_file) {
Expand Down Expand Up @@ -133,14 +135,31 @@ main <- function() {
Sys.sleep(5)
},
error = function(e) {
if (exists("gwas_info")) {
if (!is.null(gwas_info)) {
flog.error(paste(gwas_info$metadata$guid, "Error processing message:", e$message))
if (!is.null(e$call)) {
flog.error(paste(gwas_info$metadata$guid, "Error call:", deparse(e$call)))
}
} else {
flog.error(paste("Error processing message: ", e$message))
}

error_call <- if (is.null(e$call)) "" else paste(deparse(e$call), collapse = " ")
redis_error <- grepl("redis", paste(e$message, error_call), ignore.case = TRUE)
if (redis_error && !is_test_run) {
tryCatch(
{
flog.warn("Redis communication failed. Reconnecting before polling queues again.")
redis_conn <<- connect_to_redis()
},
error = function(reconnect_error) {
flog.error(paste("Failed to reconnect to Redis:", reconnect_error$message))
return()
}
)
Sys.sleep(5)
}

if (is_test_run) stop(e)
}
)
Expand Down
32 changes: 29 additions & 3 deletions worker/redis_client.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,29 @@ connect_to_redis <- function(max_retries = 5, initial_delay = 2) {
return()
}

run_redis_command_with_reconnect <- function(redis_conn, command, operation_name) {
return(tryCatch(
command(redis_conn),
error = function(e) {
flog.warn(glue::glue(
"Redis {operation_name} failed: {e$message}. Reconnecting and retrying once."
))
new_conn <- connect_to_redis()
assign("redis_conn", new_conn, envir = .GlobalEnv)
return(command(new_conn))
}
))
}

send_to_dlq <- function(redis_conn, message) {
if (grepl("Caught error", message, fixed = TRUE)) {
return(invisible(NULL))
}
redis_conn$LPUSH(process_gwas_dlq, message)
run_redis_command_with_reconnect(
redis_conn,
function(conn) conn$LPUSH(process_gwas_dlq, message),
"DLQ push"
)
return()
}

Expand All @@ -57,11 +75,19 @@ get_from_delete_queue <- function(redis_conn) {
}

add_to_in_progress_queue <- function(redis_conn, message) {
redis_conn$LPUSH(process_gwas_in_progress, message)
run_redis_command_with_reconnect(
redis_conn,
function(conn) conn$LPUSH(process_gwas_in_progress, message),
"in-progress queue push"
)
return()
}

remove_from_in_progress_queue <- function(redis_conn, message) {
redis_conn$LREM(process_gwas_in_progress, 0, message)
run_redis_command_with_reconnect(
redis_conn,
function(conn) conn$LREM(process_gwas_in_progress, 0, message),
"in-progress queue removal"
)
return()
}
Loading