Skip to content

Commit 7677b85

Browse files
committed
cloud_config() -> posit_workbench_config()
1 parent d029f4a commit 7677b85

File tree

6 files changed

+56
-59
lines changed

6 files changed

+56
-59
lines changed

NAMESPACE

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ export(.flat)
2626
export(.progress)
2727
export(.stop)
2828
export(call_mirai)
29-
export(cloud_config)
3029
export(cluster_config)
3130
export(collect_mirai)
3231
export(daemon)
@@ -51,6 +50,7 @@ export(nextcode)
5150
export(nextget)
5251
export(nextstream)
5352
export(on_daemon)
53+
export(posit_workbench_config)
5454
export(register_serial)
5555
export(remote_config)
5656
export(require_daemons)
@@ -83,6 +83,7 @@ importFrom(nanonext,listen)
8383
importFrom(nanonext,mclock)
8484
importFrom(nanonext,monitor)
8585
importFrom(nanonext,msleep)
86+
importFrom(nanonext,ncurl)
8687
importFrom(nanonext,nng_error)
8788
importFrom(nanonext,opt)
8889
importFrom(nanonext,parse_url)

NEWS.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
#### New Features
1010

11-
* Adds `cloud_config()` to launch remote daemons using a cloud / cloud-based managed platform. Currently supports Posit Workbench.
11+
* Adds `posit_workbench_config()` to launch remote daemons using the default-configured Posit Workbench Launcher.
1212
* Adds `with_daemons()` and `local_daemons()` helper functions for using a particular compute profile.
1313
This works with daemons that are already set up unlike the existing `with.miraiDaemons()` method, which creates a new scope and tears it down when finished (#360).
1414
* A mirai now has an attribute `id`, which is a monotonically increasing integer identifier unique to each session.

R/launchers.R

Lines changed: 39 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,8 @@ launch_remote <- function(n = 1L, remote = remote_config(), ..., tls = NULL, .co
9494
dots <- if (...length()) parse_dots(envir, ...) else envir[["dots"]]
9595
if (is.null(tls)) tls <- envir[["tls"]]
9696

97-
if (length(remote) == 2L) {
98-
platform <- remote[["platform"]]
97+
if (length(remote) == 2L && remote[["platform"]] == "posit_workbench") {
9998
args <- remote[["args"]]
100-
platform != "posit" && stop(._[["platform_unsupported"]])
10199
tools <- posit_tools()
102100
is.environment(tools) || stop(._[["posit_api"]])
103101
return(posit_workbench_launch(n, args, tools))
@@ -401,45 +399,32 @@ cluster_config <- function(command = "sbatch", options = "", rscript = "Rscript"
401399
list(command = "/bin/sh", args = args, rscript = rscript, quote = NULL)
402400
}
403401

404-
#' Cloud Remote Launch Configuration
402+
#' Posit Workbench Launch Configuration
405403
#'
406-
#' Generates a remote configuration for launching daemons via cloud /
407-
#' cloud-based managed platforms.
408-
#'
409-
#' @param platform \[default "posit"\] character name of the platform
410-
#' (case-insensitive). Currently the only option is "posit" to use the Posit
411-
#' Workbench launcher.
404+
#' Generates a remote configuration for launching daemons via the default
405+
#' configured Posit Workbench launcher method.
412406
#'
413407
#' @inherit remote_config return
414408
#'
415409
#' @seealso [ssh_config()], [cluster_config()], and [remote_config()] for other
416410
#' types of remote launch configuration.
417411
#'
418412
#' @examples
419-
#' tryCatch(cloud_config(), error = identity)
413+
#' tryCatch(posit_workbench_config(), error = identity)
420414
#'
421415
#' \dontrun{
422416
#'
423417
#' # Launch 2 daemons using the Posit Workbench default:
424-
#' daemons(n = 2, url = host_url(), remote = cloud_config(platform = "posit"))
418+
#' daemons(n = 2, url = host_url(), remote = posit_workbench_config())
425419
#' }
426420
#'
427421
#' @export
428422
#'
429-
cloud_config <- function(platform = "posit") {
430-
platform <- tolower(platform)
431-
args <- switch(
432-
platform,
433-
posit = {
434-
tools <- posit_tools()
435-
is.environment(tools) || stop(._[["posit_api"]])
436-
get_info <- .subset2(tools, ".rs.api.launcher.getInfo")
437-
cluster <- get_info()[["clusters"]][[1L]]
438-
list(name = cluster[["name"]], image = cluster[["defaultImage"]])
439-
},
440-
stop(._[["platform_unsupported"]])
441-
)
442-
list(platform = platform, args = args)
423+
posit_workbench_config <- function() {
424+
tools <- posit_tools()
425+
is.null(tools) && stop(._[["posit_api"]])
426+
args <- posit_get_info(tools)
427+
list(platform = "posit_workbench", args = args)
443428
}
444429

445430
#' URL Constructors
@@ -528,17 +513,37 @@ find_dot <- function(args) {
528513
}
529514

530515
posit_tools <- function() {
531-
idx <- match("tools:rstudio", search(), nomatch = 0L)
532-
idx || return()
533-
tools <- as.environment(idx)
534-
feature_available <- .subset2(tools, ".rs.api.launcher.jobsFeatureAvailable")
535-
is.function(feature_available) && feature_available() || return()
536-
tools
516+
from_json <- get0(".rs.fromJSON")
517+
cookie <- Sys.getenv("RS_SESSION_RPC_COOKIE")
518+
server <- Sys.getenv("RS_SERVER_ADDRESS")
519+
is.function(from_json) && nzchar(cookie) && nzchar(server) || return()
520+
list(from_json = from_json, cookie = cookie, server = server)
521+
}
522+
523+
posit_get_info <- function(tools) {
524+
info <- ncurl(
525+
url = file.path(tools[["server"]], "api", "get_compute_envs"),
526+
headers = c(cookie = tools[["cookie"]])
527+
)
528+
data <- tools[["from_json"]](info[["data"]])
529+
cluster <- .subset2(data, c(1L, 1L, 1L))
530+
list(name = cluster[["name"]], image = cluster[["defaultImage"]])
537531
}
538532

539533
posit_workbench_launch <- function(n, args, tools) {
540-
submit_job <- .subset2(tools, ".rs.api.launcher.submitJob")
541-
new_container <- .subset2(tools, ".rs.api.launcher.newContainer")
534+
tools <- posit_tools()
535+
json <- sprintf(
536+
'{"method":"launch_job","kwparams":{"job":{"cluster":"%s","container":{"image":"%s"},"name":"mirai_daemon","exe":"Rscript","args":["-e","mirai::daemon(\\"%s\\")"]}}}',
537+
args[["name"]],
538+
args[["image"]],
539+
nextget("url")
540+
)
541+
info <- ncurl(
542+
url = file.path(tools[["server"]], "api", "launch_job"),
543+
method = "POST",
544+
headers = c(cookie = tools[["cookie"]]),
545+
data = json
546+
)
542547
cluster <- args[["name"]]
543548
container <- new_container(args[["image"]])
544549
cmds <- launch_remote(n)

R/mirai-package.R

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
#'
2828
#' @importFrom nanonext .advance call_aio call_aio_ collect_aio collect_aio_
2929
#' .context cv cv_signal cv_value dial ip_addr is_error_value .keep listen
30-
#' .mark mclock monitor msleep nng_error opt opt<- parse_url pipe_id
30+
#' .mark mclock monitor msleep ncurl nng_error opt opt<- parse_url pipe_id
3131
#' pipe_notify random .read_header .read_marker read_monitor reap recv
3232
#' recv_aio request send serial_config socket stat stop_aio tls_config
3333
#' unresolved .unresolved until wait write_cert
@@ -80,8 +80,7 @@
8080
n_zero = "the number of daemons must be zero or greater",
8181
not_found = "compute profile `%s` not found",
8282
numeric_n = "`n` must be numeric, did you mean to provide `url`?",
83-
platform_unsupported = "`platform` is currently not supported",
84-
posit_api = "this launch configuration can only be used from Posit Workbench",
83+
posit_api = "can only be used from Posit Workbench",
8584
sync_daemons = "mirai: initial sync with daemon(s) [%d secs elapsed]",
8685
sync_dispatcher = "mirai: initial sync with dispatcher [%d secs elapsed]",
8786
within_map = "cannot create local daemons from within mirai map"

man/cloud_config.Rd renamed to man/posit_workbench_config.Rd

Lines changed: 8 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/tests.R

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,23 +61,20 @@ test_null(register_serial("test_klass1", serialize, unserialize))
6161
test_null(register_serial(c("test_klass2", "test_klass3"), list(serialize, serialize), list(unserialize, unserialize)))
6262
test_equal(length(mirai:::.[["serial"]][[3L]]), 3L)
6363
# cloud launcher tests
64-
test_error(cloud_config(platform = ""), "not supported")
6564
is.null(mirai:::posit_tools()) && {
6665
ns <- new.env(parent = emptyenv())
6766
`[[<-`(ns, ".rs.api.launcher.jobsFeatureAvailable", function() TRUE)
6867
`[[<-`(ns, ".rs.api.launcher.getInfo", function() list(clusters = list(list(name = "Kubernetes", defaultImage = "1.a.b.reg.prov.com/int-r-sess:ubuntu2204-20250609"))))
6968
`[[<-`(ns, ".rs.api.launcher.newContainer", function(image) image)
7069
`[[<-`(ns, ".rs.api.launcher.submitJob", function(...) NULL)
7170
attach(ns, name = "tools:rstudio")
72-
cfg <- cloud_config(platform = "posit")
71+
cfg <- posit_workbench_config()
7372
test_type("list", cfg)
74-
test_zero(daemons(url = local_url(), dispatcher = FALSE))
73+
test_nzchar(daemons(url = local_url(), dispatcher = FALSE))
7574
test_class("miraiLaunchCmd", launch_remote(n = 2L, remote = cfg))
76-
cfg$platform <- "wrong"
77-
test_error(launch_remote(n = 2L, remote = cfg), "not supported")
78-
test_zero(daemons(0))
75+
test_null(daemons(0))
7976
detach()
80-
test_error(cloud_config(platform = "posit"), "can only be used from Posit Workbench")
77+
test_error(posit_workbench_config(), "can only be used from Posit Workbench")
8178
}
8279
# mirai and daemons tests
8380
connection && {

0 commit comments

Comments
 (0)