Skip to content

Commit

Permalink
Send email alert in background process
Browse files Browse the repository at this point in the history
  • Loading branch information
wkmor1 committed Mar 28, 2024
1 parent c2585fc commit 8eb54c5
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 14 deletions.
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ RUN R -s -e " \
options(warn = 2); \
utils::install.packages( \
c( \
'callr', \
'covr', \
'DT', \
'emayili', \
Expand Down
40 changes: 26 additions & 14 deletions init.R
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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")
Expand Down Expand Up @@ -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)

}

}
Expand Down

0 comments on commit 8eb54c5

Please sign in to comment.