-
Notifications
You must be signed in to change notification settings - Fork 0
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
Mrc 5114 Add a submit function to the queue object #5
Merged
Merged
Changes from 24 commits
Commits
Show all changes
38 commits
Select commit
Hold shift + click to select a range
9a98d7e
update lintr, add packages and add redis script
M-Kusumgar c8cb377
add queue R6 class
M-Kusumgar 1e1a990
tests
M-Kusumgar e3417ae
documentation to finalizer
M-Kusumgar 255f58e
add docs for Queue
M-Kusumgar fd2e375
refactor object so only has view on queue and does not manage workers
M-Kusumgar e767d0c
add redis to workflow
M-Kusumgar 0699a33
actually start redis
M-Kusumgar 62a98e6
update docs
M-Kusumgar cb5de4a
fix cleanup
M-Kusumgar d696102
add runner and submit function to queue
M-Kusumgar 855877d
change env var names to match other PRs
M-Kusumgar 3a6de8b
got worker running with queue
M-Kusumgar 4979cd5
queue tests done
M-Kusumgar 6b34b85
remove debug
M-Kusumgar f3d0ecd
Merge branch 'main' into mrc-5114
M-Kusumgar b1a83c9
clean up diff
M-Kusumgar ff7a8a3
fix tests by using git helpers
M-Kusumgar 0fab722
fix test
M-Kusumgar f48f338
rob's comments
M-Kusumgar 74d2ac0
cov, git clean test
M-Kusumgar cb4a5e5
Merge branch 'main' into mrc-5114
M-Kusumgar 4bc17a2
fix tests referencing orderly.R
M-Kusumgar 7e09a01
fix helper function
M-Kusumgar 70533e1
try to fix test
M-Kusumgar 441f844
tmate
M-Kusumgar 6264648
add user name and email to worker git config
b1daffd
remove tmate
c7d03e6
test
17a32aa
error handling
f4655eb
refactor git stash for better error handling
8c88f39
add repo arg
53db6cb
revert to tryCatch for now
070805a
not running redis on mac
11b747b
better if
b69faac
change identity to me
M-Kusumgar 7f8f52f
Update to work with latest rrq, make branch tests work with any defau…
r-ash ca6ec00
cleanup before report run
M-Kusumgar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,53 @@ | ||
runner_run <- function(orderly_root, reportname, parameters, branch, ref, ...) { | ||
# Setup | ||
worker_id <- Sys.getenv("RRQ_WORKER_ID") | ||
worker_path <- file.path(orderly_root, ".packit", "workers", worker_id) | ||
point_head_to_ref(worker_path, branch, ref) | ||
|
||
# Run | ||
withr::with_envvar( | ||
c(ORDERLY_SRC_ROOT = file.path(worker_path, "src", reportname)), | ||
orderly2::orderly_run(reportname, parameters = parameters, | ||
root = orderly_root, ...) | ||
) | ||
|
||
# Cleanup | ||
git_clean(worker_path) | ||
} | ||
|
||
point_head_to_ref <- function(worker_path, branch, ref) { | ||
gert::git_fetch(repo = worker_path) | ||
gert::git_branch_checkout(branch, repo = worker_path) | ||
gert::git_reset_hard(ref, repo = worker_path) | ||
} | ||
|
||
add_dir_parent_if_empty <- function(files_to_delete, path) { | ||
contained_files <- list.files(path, full.names = TRUE) | ||
if (length(setdiff(contained_files, files_to_delete)) > 0) { | ||
return(files_to_delete) | ||
} | ||
add_dir_parent_if_empty(c(files_to_delete, path), dirname(path)) | ||
} | ||
|
||
get_empty_dirs <- function(worker_path) { | ||
dirs <- fs::dir_ls(worker_path, recurse = TRUE, type = "directory") | ||
Reduce(add_dir_parent_if_empty, c(list(character()), dirs)) | ||
} | ||
|
||
git_clean <- function(worker_path) { | ||
# gert does not have git clean but this should achieve the same thing | ||
res <- tryCatch( | ||
gert::git_stash_save( | ||
include_untracked = TRUE, | ||
include_ignored = TRUE, | ||
repo = worker_path | ||
), | ||
error = function(e) NULL | ||
) | ||
if (!is.null(res)) { | ||
gert::git_stash_drop(repo = worker_path) | ||
} | ||
# however git ignores all directories, only cares about files, so we may | ||
# have empty directories left | ||
unlink(get_empty_dirs(worker_path), recursive = TRUE) | ||
} |
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,5 @@ | ||
orderly2::orderly_artefact("Some data", "data.rds") | ||
d <- data.frame(a = 1:10, x = runif(10), y = 1:10 + runif(10)) | ||
write.table("test", file = file.path("..", "..", "inside_draft.txt")) | ||
write.table("test", file = file.path("..", "..", "..", "outside_draft.txt")) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. naughty! |
||
saveRDS(d, "data.rds") |
File renamed without changes.
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 |
---|---|---|
|
@@ -67,15 +67,63 @@ copy_examples <- function(examples, path_src) { | |
} | ||
|
||
|
||
helper_add_git <- function(path) { | ||
helper_add_git <- function(path, add = ".") { | ||
gert::git_init(path) | ||
sha <- git_add_and_commit(path) | ||
sha <- git_add_and_commit(path, add) | ||
branch <- gert::git_branch(repo = path) | ||
url <- "https://example.com/git" | ||
gert::git_remote_add(url, repo = path) | ||
list(path = path, branch = branch, sha = sha, url = url) | ||
} | ||
|
||
new_queue_quietly <- function(root, ...) { | ||
suppressMessages(Queue$new(root, ...)) | ||
} | ||
|
||
make_worker_dirs <- function(orderly_root, ids) { | ||
packit_path <- file.path(orderly_root, ".packit") | ||
dir.create(packit_path) | ||
workers <- file.path(packit_path, "workers") | ||
dir.create(workers) | ||
lapply(ids, function(id) { | ||
worker_path <- file.path(workers, id) | ||
dir.create(worker_path) | ||
gert::git_clone(orderly_root, path = worker_path) | ||
}) | ||
|
||
} | ||
|
||
start_queue_workers_quietly <- function(n_workers, | ||
controller, env = parent.frame()) { | ||
worker_manager <- suppressMessages( | ||
rrq::rrq_worker_spawn2(n_workers, controller = controller) | ||
) | ||
withr::defer(rrq::rrq_worker_stop(controller = controller), env = env) | ||
worker_manager | ||
} | ||
|
||
start_queue_with_workers <- function(root, n_workers, env = parent.frame()) { | ||
q <- new_queue_quietly(root) | ||
worker_manager <- start_queue_workers_quietly(n_workers, q$controller, | ||
env = env) | ||
make_worker_dirs(root, worker_manager$id) | ||
q | ||
} | ||
|
||
skip_if_no_redis <- function() { | ||
available <- redux::redis_available() | ||
if (!available) { | ||
testthat::skip("Skipping test as redis is not available") | ||
} | ||
invisible(available) | ||
} | ||
|
||
expect_worker_task_complete <- function(task_id, controller, n_tries) { | ||
is_task_successful <- rrq::rrq_task_wait( | ||
task_id, controller = controller, timeout = n_tries | ||
) | ||
expect_equal(is_task_successful, TRUE) | ||
} | ||
|
||
initialise_git_repo <- function() { | ||
t <- tempfile() | ||
|
@@ -85,16 +133,25 @@ initialise_git_repo <- function() { | |
} | ||
|
||
|
||
git_add_and_commit <- function(path) { | ||
gert::git_add(".", repo = path) | ||
create_new_commit <- function(path, new_file = "new", message = "new message", | ||
add = ".") { | ||
writeLines("new file", file.path(path, new_file)) | ||
gert::git_add(add, repo = path) | ||
user <- "author <[email protected]>" | ||
gert::git_commit("new commit", author = user, committer = user, repo = path) | ||
} | ||
|
||
|
||
git_add_and_commit <- function(path, add = ".") { | ||
gert::git_add(add, repo = path) | ||
user <- "author <[email protected]>" | ||
gert::git_commit("new commit", author = user, committer = user, repo = path) | ||
} | ||
|
||
|
||
create_new_commit <- function(path, new_file = "new") { | ||
create_new_commit <- function(path, new_file = "new", add = ".") { | ||
writeLines("new file", file.path(path, new_file)) | ||
git_add_and_commit(path) | ||
git_add_and_commit(path, add) | ||
} | ||
|
||
|
||
|
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
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,62 @@ | ||
test_that("runner runs as expected", { | ||
orderly_root <- test_prepare_orderly_example("data") | ||
helper_add_git(orderly_root, c("src", "orderly_config.yml")) | ||
|
||
worker_id <- "worker1" | ||
make_worker_dirs(orderly_root, worker_id) | ||
worker_root <- file.path(orderly_root, ".packit", "workers", worker_id) | ||
|
||
suppressMessages(withr::with_envvar( | ||
c(RRQ_WORKER_ID = worker_id), | ||
runner_run(orderly_root, "data", NULL, "master", "HEAD", echo = FALSE) | ||
)) | ||
|
||
# report has been run with data in archive | ||
expect_equal(length(list.files(file.path(orderly_root, "archive"))), 1) | ||
# cleanup has deleted draft folder | ||
expect_equal(file.exists(file.path(worker_root, "draft")), FALSE) | ||
}) | ||
|
||
test_that("runner runs as expected with parameters", { | ||
orderly_root <- test_prepare_orderly_example("parameters") | ||
helper_add_git(orderly_root, c("src", "orderly_config.yml")) | ||
|
||
worker_id <- "worker1" | ||
make_worker_dirs(orderly_root, worker_id) | ||
worker_root <- file.path(orderly_root, ".packit", "workers", worker_id) | ||
|
||
parameters <- list(a = -1, b = -2, c = -3) | ||
suppressMessages(withr::with_envvar( | ||
c(RRQ_WORKER_ID = worker_id), | ||
runner_run(orderly_root, "parameters", parameters, | ||
"master", "HEAD", echo = FALSE) | ||
)) | ||
|
||
report_archive <- file.path(orderly_root, "archive", "parameters") | ||
rds_path <- file.path(report_archive, list.files(report_archive), "data.rds") | ||
output <- readRDS(rds_path) | ||
|
||
expect_equal(output, parameters) | ||
expect_equal(file.exists(file.path(worker_root, "draft")), FALSE) | ||
}) | ||
|
||
test_that("git clean clears unnecessary files", { | ||
# git-clean.R spawns a file in draft folder and one in worker root folder | ||
# and there will also be an empty folder draft/git-clean so we test | ||
# all components of git_clean | ||
orderly_root <- test_prepare_orderly_example("git-clean") | ||
helper_add_git(orderly_root, c("src", "orderly_config.yml")) | ||
|
||
worker_id <- "worker1" | ||
make_worker_dirs(orderly_root, worker_id) | ||
worker_root <- file.path(orderly_root, ".packit", "workers", worker_id) | ||
|
||
suppressMessages(withr::with_envvar( | ||
c(RRQ_WORKER_ID = worker_id), | ||
runner_run(orderly_root, "git-clean", NULL, "master", "HEAD", echo = FALSE) | ||
)) | ||
|
||
expect_equal(length(list.files(file.path(orderly_root, "archive"))), 1) | ||
expect_equal(file.exists(file.path(worker_root, "draft")), FALSE) | ||
expect_equal(file.exists(file.path(worker_root, "outside_draft.txt")), FALSE) | ||
}) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In what case do we need to cleanup?
Just tried locally and I was able to use
point_head_to_ref
to switch between commits without cleaning up anything in between.Is this for if orderly fails to run and writes to the
draft
dir? I think that will be gitignored?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
at the end of a successful run, orderly deletes the file from the draft folder and leaves an empty folder in the directory, the most important bit of this git clean function is the removing of those empty dirs, just to keep a clean worktree after every run, otherwise that draft folder would just keep accumulating lots of empty folders after many runs
the stash and drop is just a safety in case someone has somehow produced something that is untracked by git, we can guarantee that it is removed and doesnt accumulate, people could also change their gitignore files which could also lead to these problems i think
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if it might also be worth (or worth doing in addition) the clean before running the report?