From 8eb54c57b787108064736a3d2e170e304417787f Mon Sep 17 00:00:00 2001 From: wkmor1 Date: Thu, 28 Mar 2024 10:19:42 +0200 Subject: [PATCH] Send email alert in background process --- Dockerfile | 1 + init.R | 40 ++++++++++++++++++++++++++-------------- 2 files changed, 27 insertions(+), 14 deletions(-) diff --git a/Dockerfile b/Dockerfile index 530a805..bd9534c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -77,6 +77,7 @@ RUN R -s -e " \ options(warn = 2); \ utils::install.packages( \ c( \ + 'callr', \ 'covr', \ 'DT', \ 'emayili', \ diff --git a/init.R b/init.R index 88a53b5..e0adfe8 100755 --- a/init.R +++ b/init.R @@ -1,4 +1,5 @@ suppressPackageStartupMessages({ + library(callr, warn.conflicts = TRUE, quietly = TRUE) library(logger, warn.conflicts = TRUE, quietly = TRUE) library(plumber, warn.conflicts = TRUE, quietly = TRUE) library(tictoc, warn.conflicts = TRUE, quietly = TRUE) @@ -12,6 +13,26 @@ options(plumber.maxRequestSize = 1e8L, plumber.apiPath = path) convert_empty <- function(x) switch(paste0(".", x), . = "-", x) +alert <- function(host, port, agent, branch, to, from, req) { + + smtp <- emayili::server(host, port) + + subject <- sprintf("Error report: %s on branch %s", agent, branch) + + text <- sprintf( + "At [%s]: %s %s (Status: %s)", + format(Sys.time()), + req[["REQUEST_METHOD"]], + req[["PATH_INFO"]], + res[["status"]] + ) + + message <- emayili::envelope(to, from, subject = subject, text = text) + + smtp(message) + +} + status_dir <- Sys.getenv("STATUS_DIR", "status") log_dir <- Sys.getenv("LOG_DIR", "logs") @@ -65,22 +86,13 @@ p[["registerHooks"]]( if (!any(c(host, port, to, from, agent, branch) == "")) { - smtp <- emayili::server(host, port) - - subject <- sprintf("Error report: %s on branch %s", agent, branch) - - text <- sprintf( - "At [%s]: %s %s (Status: %s)", - format(Sys.time()), - req[["REQUEST_METHOD"]], - req[["PATH_INFO"]], - res[["status"]] + r_bg( + alert, + list(host, port, agent, branch, to, from, req), + poll_connection = FALSE, + cleanup = FALSE ) - message <- emayili::envelope(to, from, subject = subject, text = text) - - smtp(message) - } }