-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathsendMail.R
51 lines (46 loc) · 2.06 KB
/
sendMail.R
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
sendMail = function(reg, ids, result.str, extra.msg = "",
disable.mail, condition, first, last, conf) {
if (disable.mail)
return(invisible(NULL))
conf = getBatchJobsConf()
ischunk = (length(ids) > 1L)
first.id = if(ischunk) ids[[1L]] else ids
# should we mail
mail.conds = list(start = conf$mail.start, done = conf$mail.done, error = conf$mail.error)
mail.cond = mail.conds[[condition]]
if (mail.cond == "all" ||
(mail.cond %in% c("first", "first+last") && first.id == first) ||
(mail.cond %in% c("last", "first+last") && first.id == last)) {
myformat = function(title, lines) {
collapse(c(sprintf("### %s", title), lines, "", ""), "\n")
}
cstr = switch(condition,
"start" = "started",
"done" = "finished",
"error" = "terminated with exception")
subj = sprintf("[%s]: %s %s has %s", reg$id, ifelse(ischunk, "Chunk with first job ", "Job"), first.id, cstr)
msg = myformat("Ids", ids)
# append result and status information
if (condition != "start") {
if (extra.msg != "")
msg = paste0(msg, myformat("Message", extra.msg))
msg = paste0(msg, myformat("Results", result.str))
if(first.id == last)
msg = paste0(msg, myformat("Status", capture.output(showStatus(reg, run.and.exp = FALSE))))
}
# if a mail problem occurs, we only warn but do not terminate
if( is.function(conf$mail.control) ){ # use email function if nessecary
sendmail <- function(from, to, subj, msg, ...){
conf$mail.control(status = condition, from = from, to = to, subject = subj, body = msg, registry = reg, ids = ids)
}
}
ok = try (sendmail(conf$mail.from, conf$mail.to, subj, msg, control = conf$mail.control))
if (is.error(ok)) {
warningf("Could not send mail to signal condition '%s'!\nFrom: %s\nTo: %s\nControl: %s\nError message: %s",
condition, conf$mail.from, conf$mail.to, convertToShortString(conf$mail.control), as.character(ok))
} else {
messagef("Mail signaling condition '%s' send to %s", condition, conf$mail.to)
}
}
invisible(NULL)
}