Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat add option arguments to run() #102

Merged
merged 11 commits into from
Oct 28, 2024
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: whirl
Title: Logging package
Version: 0.1.2
Version: 0.1.3
Authors@R: c(
person("Aksel", "Thomsen", , "[email protected]", role = c("aut", "cre")),
person("Lovemore", "Gakava", , "[email protected]", role = "aut"),
Expand Down
13 changes: 13 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
# whirl 0.1.3 (2024-10-23)
* Adding additional arguments to `run()` allowing the user to:
- control the verbosity level
- specify whether renv should be checked
- specify which files to track
- adjust the output format of the log files.

# whirl 0.1.2 (2024-10-21)
* Updated package website url and example code

# whirl 0.1.1 (2024-10-07)
* Fix enabling rendering of md log formats("gfm", "commonmark", "markua").

# whirl 0.1.0 (2024-10-01)
* First public release.

Expand Down
9 changes: 6 additions & 3 deletions R/enrich_input.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
#' @param input Can be a vector, a list or a whirl config file.
#' @param steps A filter argument for selecting specific steps that should be
#' executed
#' @inheritParams options_params
#'
#' @return A list
#' @noRd
enrich_input <- function(input, steps = NULL) {
enrich_input <- function(input, steps = NULL,
verbosity_level = options::opt("verbosity_level", env = "whirl")) {

# Characterize the input
is_config_file <- any(grepl("yaml|yml", tools::file_ext(input)))
Expand All @@ -19,7 +21,7 @@ enrich_input <- function(input, steps = NULL) {
config_whirl <- yaml::yaml.load_file(input)
got <- config_whirl$"steps"
} else {
root_dir = "."
root_dir = getwd()
}

# Convert vector to list
Expand Down Expand Up @@ -93,7 +95,8 @@ enrich_input <- function(input, steps = NULL) {

zephyr::msg(message_,
msg_fun = cli::cli_inform,
levels_to_write = "verbose"
levels_to_write = "verbose",
verbosity_level = verbosity_level
)

invisible(out)
Expand Down
12 changes: 8 additions & 4 deletions R/internal_run.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@
#' steps listed in the config file will be executed.
#' @param queue The whirl_r_queue that should execute the scripts
#' @param level Depth of the recursive config calls. The initial call will have 1
#'
#' @inheritParams options_params
#' @return A tibble containing the execution results for all the scripts.
#' @noRd
internal_run <- function(input, steps, queue, level) {
internal_run <- function(input, steps, queue, level,
verbosity_level = options::opt("verbosity_level", env = "whirl")) {

# Enrich the input with "name" and "path" elements
enriched <- enrich_input(input, steps)
enriched <- enrich_input(input, steps, verbosity_level)

# Loop over the elements
for (i in seq_along(enriched)) {
Expand All @@ -26,7 +27,10 @@ internal_run <- function(input, steps, queue, level) {

# Messages
cli_level <- get(paste0("cli_h", min(level, 3)), envir = asNamespace("cli"))
zephyr::msg(name, msg_fun = cli_level, levels_to_write = "verbose")
zephyr::msg(name,
msg_fun = cli_level,
levels_to_write = "verbose",
verbosity_level = verbosity_level)

# If the step points to a config file then re-initiate internal_run()
if (any(grepl("yaml|yml", tools::file_ext(files)))) {
Expand Down
34 changes: 28 additions & 6 deletions R/run.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
#' @param summary_file A character string specifying the file path where the
#' summary log will be stored.
#' @inheritParams options_params
#'
#' @return A tibble containing the execution results for all the scripts.
#'
#'@examplesIf FALSE
Expand All @@ -41,22 +40,34 @@

run <- function(input,
steps = NULL,
summary_file = "summary.html",
n_workers = options::opt("n_workers", env = "whirl"),
summary_file = "summary.html"
check_renv = options::opt("check_renv", env = "whirl"),
verbosity_level = options::opt("verbosity_level", env = "whirl"),
track_files = options::opt("track_files", env = "whirl"),
out_formats = options::opt("out_formats", env = "whirl")
) {

# Additional Settings
track_files_discards = options::opt("track_files_discards", env = "whirl")
track_files_keep = options::opt("track_files_keep", env = "whirl")
approved_pkgs_folder = options::opt("approved_pkgs_folder", env = "whirl")
approved_pkgs_url = options::opt("approved_pkgs_url", env = "whirl")

# Message when initiating
d <- cli::cli_div(theme = list(rule = list(
color = "skyblue3", "line-type" = "double"
)))

zephyr::msg("Executing scripts and generating logs",
levels_to_write = "verbose",
levels_to_write = c("verbose", "minimal"),
verbosity_level = verbosity_level,
msg_fun = cli::cli_rule)

# Message when ending
on.exit(zephyr::msg("End of process",
levels_to_write = "verbose",
levels_to_write = c("verbose", "minimal"),
verbosity_level = verbosity_level,
msg_fun = cli::cli_rule))
on.exit(cli::cli_end(d), add = TRUE)

Expand All @@ -65,19 +76,30 @@ run <- function(input,

zephyr::msg("Executing scripts in parallel using {n_workers} cores\n",
levels_to_write = "verbose",
verbosity_level = verbosity_level,
msg_fun = cli::cli_inform)

# Initiating the queue
queue <- whirl_queue$new(n_workers = n_workers)
queue <- whirl_queue$new(n_workers = n_workers,
check_renv = check_renv,
verbosity_level = verbosity_level,
track_files = track_files,
out_formats = out_formats,
track_files_discards = track_files_discards,
track_files_keep = track_files_keep,
approved_pkgs_folder = approved_pkgs_folder,
approved_pkgs_url = approved_pkgs_url)

result <- internal_run(input = input,
steps = steps,
queue = queue,
level = 1)
level = 1,
verbosity_level = verbosity_level)

# Create the summary log
summary_tibble <- util_queue_summary(result$queue)
render_summary(input = summary_tibble, summary_file = summary_file)

invisible(result$queue)

}
68 changes: 60 additions & 8 deletions R/whirl_queue.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,29 @@ whirl_queue <- R6::R6Class(
classname = "whirl_queue",
public = list(

#' @inheritParams options_params
#' @description Initialize the new whirl_queue
#' @param n_workers [numeric] Maximum number of workers to be used simultaneously
#' @return A [whirl_queue] object
initialize = \(n_workers = options::opt("n_workers", env = "whirl")) {
wq_initialise(self, private, n_workers)
initialize = \(n_workers = options::opt("n_workers", env = "whirl"),
verbosity_level = options::opt("verbosity_level", env = "whirl"),
check_renv = options::opt("check_renv", env = "whirl"),
track_files = options::opt("track_files", env = "whirl"),
out_formats = options::opt("out_formats", env = "whirl"),
track_files_discards = options::opt("track_files_discards", env = "whirl"),
track_files_keep = options::opt("track_files_keep", env = "whirl"),
approved_pkgs_folder = options::opt("approved_pkgs_folder", env = "whirl"),
approved_pkgs_url = options::opt("approved_pkgs_url", env = "whirl")
) {
wq_initialise(self, private,
n_workers,
verbosity_level,
check_renv,
track_files,
out_formats,
track_files_discards,
track_files_keep,
approved_pkgs_folder,
approved_pkgs_url)
},

#' @description Push scripts to the queue
Expand Down Expand Up @@ -96,11 +114,32 @@ whirl_queue <- R6::R6Class(
private = list(
.queue = NULL,
.workers = NULL,
.n_workers = NULL
.n_workers = NULL,
verbosity_level = NULL,
check_renv = NULL,
track_files = NULL,
out_formats = NULL,
track_files_discards = NULL,
track_files_keep = NULL,
approved_pkgs_folder = NULL,
approved_pkgs_url = NULL
)
)

wq_initialise <- function(self, private, n_workers) {
wq_initialise <- function(self, private, n_workers,
verbosity_level, check_renv, track_files, out_formats,
track_files_discards, track_files_keep,
approved_pkgs_folder, approved_pkgs_url) {

private$check_renv <- check_renv
private$verbosity_level <- verbosity_level
private$track_files <- track_files
private$out_formats <- out_formats
private$track_files_discards <- track_files_discards
private$track_files_keep <- track_files_keep
private$approved_pkgs_folder <- approved_pkgs_folder
private$approved_pkgs_url <- approved_pkgs_url

private$.queue <- tibble::tibble(
id = numeric(),
tag = character(),
Expand Down Expand Up @@ -138,14 +177,27 @@ wq_skip <- function(self, private, scripts, tag) {
wq_add_queue(self, private, scripts, tag, status = "skipped")
}

wq_poll <- function(self, private, timeout) {
wq_poll <- function(self, private, timeout,
check_renv, verbosity_level, track_files, out_formats,
track_files_discards, track_files_keep,
approved_pkgs_folder, approved_pkgs_url) {

# Start new sessions if there are available workers and waiting scripts in the queue

if (length(self$next_ids)) {
nid <- self$next_ids
wid <- self$next_workers
private$.workers[["session"]][wid] <- replicate(n = length(wid), expr = whirl_r_session$new(), simplify = FALSE)
private$.workers[["session"]][wid] <- replicate(
n = length(wid),
expr = whirl_r_session$new(check_renv = private$check_renv,
verbosity_level = private$verbosity_level,
track_files = private$track_files,
out_formats = private$out_formats,
track_files_discards = private$track_files_discards,
track_files_keep = private$track_files_keep,
approved_pkgs_folder = private$approved_pkgs_folder,
approved_pkgs_url = private$approved_pkgs_url),
simplify = FALSE)
private$.workers[wid, "id_script"] <- nid
private$.workers[wid, "active"] <- TRUE
private$.queue[nid, "status"] <- "running"
Expand Down Expand Up @@ -200,7 +252,7 @@ wq_next_step <- function(self, private, wid) {
purrr::pluck(private$.queue, "result", id_script) <- session$
log_finish()$
create_outputs(out_dir = dirname(purrr::pluck(private$.queue, "script", id_script)),
format = options::opt("out_formats", env = "whirl"))
format = private$out_formats)

purrr::pluck(private$.queue, "status", id_script) <-
purrr::pluck(private$.queue, "result", id_script, "status", "status")
Expand Down
63 changes: 34 additions & 29 deletions R/whirl_r_session.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,27 @@ whirl_r_session <- R6::R6Class(
public = list(

#' @description Initialize the new whirl R session
#' @param verbose [logical] Should the progress be printed to the console?
#' @param check_renv [logical] Should renv be checked?
#' @param track_files [logical] Should the files be tracked?
#' @param track_files_discards [character] Files to discard from tracking
#' @param track_files_keep [character] Files to keep from tracking
#' @param approved_pkgs_folder [character] Folder with approved packages
#' @param approved_pkgs_url [character] URL with approved packages
#' @inheritParams options_params
#' @return A [whirl_r_session] object
initialize = \(
verbose = options::opt("verbosity_level", env = "whirl"),
check_renv = options::opt("check_renv", env = "whirl"),
track_files = options::opt("track_files", env = "whirl"),
track_files_discards = options::opt("track_files_discards", env = "whirl"),
track_files_keep = options::opt("track_files_keep", env = "whirl"),
approved_pkgs_folder = options::opt("approved_pkgs_folder", env = "whirl"),
approved_pkgs_url = options::opt("approved_pkgs_url", env = "whirl")
) {

wrs_initialize(verbose, check_renv, track_files, track_files_discards, track_files_keep, approved_pkgs_folder, approved_pkgs_url, self, private, super)
}
,
initialize = \(verbosity_level = options::opt("verbosity_level", env = "whirl"),
check_renv = options::opt("check_renv", env = "whirl"),
track_files = options::opt("track_files", env = "whirl"),
out_formats = options::opt("out_formats", env = "whirl"),
track_files_discards = options::opt("track_files_discards", env = "whirl"),
track_files_keep = options::opt("track_files_keep", env = "whirl"),
approved_pkgs_folder = options::opt("approved_pkgs_folder", env = "whirl"),
approved_pkgs_url = options::opt("approved_pkgs_url", env = "whirl")
) {
wrs_initialize(verbosity_level,
check_renv,
track_files,
out_formats,
track_files_discards,
track_files_keep,
approved_pkgs_folder,
approved_pkgs_url,
self, private, super)
},

#' @description Finalize the whirl R session
finalize = \() {
Expand Down Expand Up @@ -105,9 +105,10 @@ whirl_r_session <- R6::R6Class(
}
),
private = list(
verbose = NULL,
verbosity_level = NULL,
wd = NULL,
track_files = NULL,
out_formats = NULL,
track_files_discards = NULL,
track_files_keep = NULL,
approved_pkgs_folder = NULL,
Expand All @@ -119,23 +120,27 @@ whirl_r_session <- R6::R6Class(
inherit = callr::r_session
)

wrs_initialize <- function(verbose, check_renv, track_files, track_files_discards, track_files_keep, approved_pkgs_folder, approved_pkgs_url,
wrs_initialize <- function(verbosity_level, check_renv, track_files,
out_formats, track_files_discards, track_files_keep,
approved_pkgs_folder, approved_pkgs_url,
self, private, super) {

super$initialize() # uses callr::r_session$initialize()

# TODO: Is there a way to use `.local_envir` to avoid having to clean up the temp dir in finalize?
private$wd <- withr::local_tempdir(clean = FALSE)
private$verbose <- verbose
private$verbosity_level <- verbosity_level
private$check_renv <- check_renv
private$track_files <- track_files
private$out_formats <- out_formats
private$track_files_discards <- track_files_discards
private$track_files_keep <- track_files_keep
private$approved_pkgs_folder <- approved_pkgs_folder
private$approved_pkgs_url <- approved_pkgs_url

# If the stream does not support dynamic tty, which is needed for progress bars to update in place, the verbosity is downgraded.
if (private$verbose == "verbose" && !cli::is_dynamic_tty()) {
private$verbose <- "minimal"
if (private$verbosity_level == "verbose" && !cli::is_dynamic_tty()) {
private$verbosity_level <- "minimal"
}

super$run(func = setwd, args = list(dir = private$wd))
Expand Down Expand Up @@ -168,7 +173,7 @@ wrs_print <- function(self, private, super) {
msg <- c(
utils::capture.output(super$print()),
"Working Directory: {private$wd}",
"Verbose: {private$verbose}"
"Verbose: {private$verbosity_level}"
)

cli::cli_bullets(
Expand Down Expand Up @@ -221,10 +226,10 @@ wrs_check_status <- function(self, private, super) {
wrs_log_script <- function(script, self, private, super) {
private$current_script <- script

if (private$verbose != "quiet") {
if (private$verbosity_level != "quiet") {
private$pb <- pb_script$new(
script = private$current_script,
use_progress = private$verbose == "verbose"
use_progress = private$verbosity_level == "verbose"
)
}

Expand Down Expand Up @@ -363,7 +368,7 @@ wrs_create_outputs <- function(out_dir, format, self, private, super) {
out_dir,
gsub(
pattern = "\\.[^\\.]*$",
replacement = ".json",
replacement = "_log.json",
x = basename(private$current_script)
)
)
Expand Down
Loading
Loading