Skip to content

Commit

Permalink
Add retries, to some execution steps
Browse files Browse the repository at this point in the history
  • Loading branch information
meztez committed Jul 16, 2024
1 parent cf73136 commit 5ee9718
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 29 deletions.
3 changes: 1 addition & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: plumberDeploy
Type: Package
Title: Plumber Deployment
Version: 0.2.1.10000
Version: 0.2.2
Roxygen: list(markdown = TRUE)
Authors@R: c(
person("Bruno", "Tremblay", role = c("cre", "ctb"), email = "[email protected]"),
Expand All @@ -25,4 +25,3 @@ Imports:
utils
BugReports: https://github.com/meztez/plumberDeploy/issues
URL: https://github.com/meztez/plumberDeploy
SystemRequirements: libssh >= 0.6.0 (the original, not libssh2)
62 changes: 45 additions & 17 deletions R/digital-ocean.R
Original file line number Diff line number Diff line change
Expand Up @@ -78,19 +78,16 @@ do_provision <- function(droplet, unstable=FALSE, example=TRUE, ...){
}

droplet <- do.call(analogsea::droplet_create, createArgs)

# I often still get a closed port after droplet_wait returns. Buffer for just a bit
Sys.sleep(25)
Sys.sleep(3)

# Refresh the droplet; sometimes the original one doesn't yet have a network interface.
droplet <- analogsea::droplet(id=droplet$id)
droplet <- with_retries(analogsea::droplet(id=droplet$id))
}

# Provision
lines <- droplet_capture(droplet, 'swapon | grep "/swapfile" | wc -l', keyfile = keyfile)
lines <- with_retries(droplet_capture(droplet, 'swapon | grep "/swapfile" | wc -l', keyfile = keyfile))
if (lines != "1"){
analogsea::ubuntu_add_swap(droplet)
Sys.sleep(5)
}

do_install_plumber(droplet, unstable)
Expand All @@ -108,32 +105,41 @@ do_install_plumber = function(droplet, unstable, ...) {
analogsea::droplet_ssh(
droplet,
"sudo echo 'DEBIAN_FRONTEND=noninteractive' >> /etc/environment",
"export R_VERSION=%s.%s" |> sprintf(R.version[["major"]], R.version[["minor"]]),
"curl -O https://cdn.rstudio.com/r/ubuntu-2404/pkgs/r-${R_VERSION}_1_amd64.deb",
"sudo apt-get update -y",
"sudo apt-get install -y ./r-${R_VERSION}_1_amd64.deb",
"sudo ln -s -f /opt/R/${R_VERSION}/bin/R /usr/local/bin/R",
"sudo ln -s -f /opt/R/${R_VERSION}/bin/Rscript /usr/local/bin/Rscript",
"curl -O https://cdn.rstudio.com/r/ubuntu-2404/pkgs/r-%s.%s_1_amd64.deb" |> sprintf(R.version[["major"]], R.version[["minor"]]),
...
)
with_retries(
analogsea::droplet_ssh(
droplet,
"sudo apt-get update -y",
"sudo apt-get install -y ./r-%s.%s_1_amd64.deb" |> sprintf(R.version[["major"]], R.version[["minor"]]),
...
)
)
analogsea::droplet_ssh(
droplet,
"sudo ln -s -f /opt/R/%s.%s/bin/R /usr/local/bin/R" |> sprintf(R.version[["major"]], R.version[["minor"]]),
"sudo ln -s -f /opt/R/%s.%s/bin/Rscript /usr/local/bin/Rscript" |> sprintf(R.version[["major"]], R.version[["minor"]]),
...
)
Sys.sleep(3)
install_plumber(droplet, unstable, ...)
install_api(droplet, ...)
Sys.sleep(3)
install_nginx(droplet, ...)
Sys.sleep(3)
install_firewall(droplet, ...)
}

install_plumber <- function(droplet, unstable, ...){

analogsea::ubuntu_apt_get_install(droplet, "libssl-dev", "make", "libsodium-dev", "libcurl4-openssl-dev", ...)
with_retries(
analogsea::ubuntu_apt_get_install(droplet, "libssl-dev", "make", "libsodium-dev", "libcurl4-openssl-dev", ...)
)

if (unstable){
analogsea::install_r_package(droplet, "remotes", repo = "https://packagemanager.posit.co/cran/__linux__/noble/latest", ...)
analogsea::droplet_ssh(droplet, "Rscript -e \"remotes::install_github('rstudio/plumber')\"", ...)
} else {
analogsea::install_r_package(droplet, "plumber", repo = "https://packagemanager.posit.co/cran/__linux__/noble/latest", ...)

}

}
Expand Down Expand Up @@ -176,7 +182,7 @@ install_firewall <- function(droplet, ...){
}

install_nginx <- function(droplet, ...){
analogsea::ubuntu_apt_get_install(droplet, "nginx", ...)
with_retries(analogsea::ubuntu_apt_get_install(droplet, "nginx", ...))
analogsea::droplet_ssh(droplet, "rm -f /etc/nginx/sites-enabled/default", ...) # Disable the default site
analogsea::droplet_ssh(droplet, "mkdir -p /var/certbot", ...)
analogsea::droplet_ssh(droplet, "mkdir -p /etc/nginx/sites-available/plumber-apis/", ...)
Expand Down Expand Up @@ -600,5 +606,27 @@ do_ip = function(droplet, path) {
return(ip)
}

with_retries <- with_retries <- function(expr, retries = 5L) {
attempt <- 1L
while (attempt <= retries) {
no_error <- TRUE
res <- tryCatch(
eval.parent(substitute(expr)),
error = function(e) {
message(sprintf("Error encountered : %s\n", e |> as.character()))
no_error <<- FALSE
}
)
if (no_error) break
message(paste("Retrying attemps", attempt, "in", 3^attempt, "seconds.\n"))
Sys.sleep(3^attempt)
attempt <- attempt + 1L
if (attempt > retries) {
stop("Expression could not be evaluated.")
}
}
return(res)
}

# nocov end

12 changes: 2 additions & 10 deletions cran-comments.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
## Test environments
* local R installation, R 4.0.2
* ubuntu 16.04 (on travis-ci), R 4.0.2
* win-builder (devel)

## R CMD check results

0 errors | 0 warnings | 1 note

* This is a new release.
0 errors ✔ | 0 warnings ✔ | 0 notes ✔

---
Fixes \value issues in Rd files, fixes ::: in examples, and fixes authors.
R CMD check succeeded

0 comments on commit 5ee9718

Please sign in to comment.