Skip to content

Instantly share code, notes, and snippets.

@edavidaja
Last active January 12, 2023 17:11
Show Gist options
  • Save edavidaja/949c61be593e14df5b6539f9cede1782 to your computer and use it in GitHub Desktop.
Save edavidaja/949c61be593e14df5b6539f9cede1782 to your computer and use it in GitHub Desktop.
#!/usr/bin/Rscript
get_nodes <- function(nodes = Sys.getenv("RSTUDIO_NODES")) {
unlist(strsplit(nodes, ",", fixed = TRUE))
}
get_health_check <- function(node_address) {
con <- url(sprintf("http://%s/health-check", node_address))
on.exit(close(con))
readLines(con)
}
parse_health_check <- function(health_check) {
fields <- unlist(strsplit(health_check, "\n|,"))
keys <- sub(
":",
"",
regmatches(fields, regexpr("^[a-z\\-]+:", fields))
)
values <- sub(
": ",
"",
regmatches(fields, regexpr(":.+$", fields))
)
data.frame(keys, values, stringsAsFactors = FALSE)
}
get_value <- function(hc_table, hc_value) {
as.double(hc_table[hc_table[["keys"]] == hc_value, "values"])
}
main <- function() {
nodes <- get_nodes()
named_hc <- setNames(lapply(nodes, get_health_check), nodes)
parsed_hc <- lapply(named_hc, parse_health_check)
mem_percent <- lapply(parsed_hc, get_value, "memory-percent")
least_mem <- names(which.min(unlist(mem_percent)))
least_mem
}
cat(main())
@kmasiello
Copy link

Wrap final line main() in cat(main()) to send the host:port to standard out without [1] as a prefix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment