-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from mrc-ide/prototype-drivers
Tidy up approach to drivers
- Loading branch information
Showing
21 changed files
with
522 additions
and
156 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,10 @@ | ||
# Generated by roxygen2: do not edit by hand | ||
|
||
export(hermod_configure) | ||
export(hermod_get_configuration) | ||
export(hermod_driver) | ||
export(hermod_init) | ||
export(hermod_task_create_explicit) | ||
export(hermod_task_eval) | ||
export(hermod_task_result) | ||
export(hermod_task_status) | ||
export(hermod_task_submit) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
##' Configure your hermod root. `hermod_configure` creates the | ||
##' configuration and `hermod_get_configuration` looks it up. | ||
##' | ||
##' @title Configure your hermod root | ||
##' | ||
##' @param driver The hermod driver; probably you want this to be | ||
##' `"windows"` as that is all we support at the moment! | ||
##' | ||
##' @param ... Arguments passed to your driver. We'll work out how to | ||
##' point you at appropriate documentation once it is written. | ||
##' | ||
##' @param root Hermod root, usually best `NULL` | ||
##' | ||
##' @export | ||
hermod_configure <- function(driver, ..., root = NULL) { | ||
root <- hermod_root(root) | ||
|
||
assert_scalar_character(driver) | ||
dr <- hermod_driver_load(driver) | ||
config <- withr::with_dir(root$path$root, dr$configure(...)) | ||
|
||
fs::dir_create(root$path$config) | ||
if (is.null(root$config)) { | ||
root$config <- list() | ||
} | ||
|
||
saveRDS(config, file.path(root$path$config, paste0(driver, ".rds"))) | ||
root$config[[driver]] <- config | ||
|
||
invisible() | ||
} | ||
|
||
|
||
##' Create a new hermod driver; this is intended to be used from other | ||
##' packages, and rarely called directly. If you are trying to run | ||
##' tasks on a cluster you do not need to call this! | ||
##' | ||
##' @param configure Function used to set core configuration for the | ||
##' driver. This function will be called from the hermod root | ||
##' directory (so `getwd()` will report the correct path). It can | ||
##' take any arguments, do any calculation and then must return any | ||
##' R object that can be serialised. The resulting configuration | ||
##' will be passed in as `config` to other driver functions. | ||
##' | ||
##' @param submit Submit a task to a cluster. This is run after the | ||
##' task is created (either automatically or manually) and takes as | ||
##' arguments the task id, the configuration, the path to the root. | ||
##' | ||
##' @export | ||
hermod_driver <- function(configure, submit) { | ||
structure(list(configure = configure, | ||
submit = submit), | ||
class = "hermod_driver") | ||
} | ||
|
||
|
||
hermod_driver_load <- function(driver, call) { | ||
if (is.null(cache$drivers[[driver]])) { | ||
valid <- "windows" | ||
assert_scalar_character(driver) | ||
if (!(driver %in% valid)) { | ||
cli::cli_abort(c("Invalid driver '{driver}'", | ||
i = "Valid choice{? is/s are}: {squote(valid)}"), | ||
call = call) | ||
} | ||
cache$drivers[[driver]] <- hermod_driver_create(driver) | ||
} | ||
cache$drivers[[driver]] | ||
} | ||
|
||
|
||
hermod_driver_create <- function(name) { | ||
pkg <- sprintf("hermod.%s", name) | ||
ns <- ensure_package(pkg) | ||
target <- sprintf("hermod_driver_%s", name) | ||
|
||
## Users should never see these errors, we are in control of our own | ||
## drivers; these just help us if we're writing new ones. | ||
stopifnot(is.function(ns[[target]])) | ||
result <- ns[[target]]() | ||
stopifnot(inherits(result, "hermod_driver")) | ||
result | ||
} | ||
|
||
|
||
hermod_driver_select <- function(name, root, call = NULL) { | ||
|
||
valid <- names(root$config) | ||
if (is.null(name)) { | ||
if (length(valid) == 0) { | ||
cli::cli_abort(c("No hermod driver configured", | ||
i = "Please run 'hermod_configure()'"), | ||
call = call) | ||
} else if (length(valid) > 1) { | ||
cli::cli_abort(c("More than one hermod driver configured", | ||
i = "Please provide the argument 'driver'", | ||
i = "Valid options are: {squote(valid)}"), | ||
arg = "driver", call = call) | ||
} | ||
name <- valid | ||
} else { | ||
assert_scalar_character(name, name = "driver") | ||
if (!(name %in% valid)) { | ||
if (length(valid) == 0) { | ||
hint <- paste("No driver configured;", | ||
"please run 'hermod_configure(\"{name}\")'") | ||
} else { | ||
hint <- "Valid option{? is/s are}: {squote(valid)}" | ||
} | ||
cli::cli_abort( | ||
c("Invalid value for 'driver': '{name}'", | ||
i = hint), | ||
arg = "driver", call = call) | ||
} | ||
} | ||
name | ||
} | ||
|
||
|
||
hermod_driver_prepare <- function(driver, root, call) { | ||
root <- hermod_root(root) | ||
driver <- hermod_driver_select(driver, root, call) | ||
list(name = driver, | ||
driver = hermod_driver_load(driver, call), | ||
config = root$config[[driver]]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
##' Submit a task to a queue | ||
##' | ||
##' This is a lower-level function that you will not often need to call. | ||
##' | ||
##' @title Submit a task | ||
##' @param id The task id | ||
##' | ||
##' @param ... Disallowed additional arguments, don't use. | ||
##' | ||
##' @param driver The name of the driver to use, or you can leave | ||
##' blank if only one is configured (this will be typical). | ||
##' | ||
##' @param root The hermod root | ||
##' | ||
##' @export | ||
hermod_task_submit <- function(id, ..., driver = NULL, root = NULL) { | ||
if (...length() > 0) { | ||
cli::cli_abort("Additional arguments to 'hermod_task_submit' not allowed") | ||
} | ||
root <- hermod_root(root) | ||
|
||
## This is a bit gross, could be tidied up later. | ||
dat <- hermod_driver_prepare(driver, root, environment()) | ||
dat$driver$submit(id, dat$config, root$path$root) | ||
|
||
writeLines(dat$name, file.path(root$path$tasks, id, STATUS_SUBMITTED)) | ||
invisible() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
hermod_driver_windows <- function() { | ||
hermod::hermod_driver( | ||
configure = windows_configure, | ||
submit = windows_submit) | ||
} | ||
|
||
|
||
windows_submit <- function(id, config, path_root) { | ||
path_batch <- write_batch_task_run(id, config, path_root) | ||
|
||
path_batch_dat <- prepare_path(path_batch, config$shares) | ||
path_batch_unc <- windows_path( | ||
file.path(path_batch_dat$path_remote, path_batch_dat$rel)) | ||
|
||
client <- get_web_client() | ||
dide_id <- client$submit(path_batch_unc, id, config$template) | ||
writeLines(dide_id, file.path(dirname(path_batch), DIDE_ID)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.