Skip to content

Commit

Permalink
Terminate multiple jobs
Browse files Browse the repository at this point in the history
  • Loading branch information
wlandau-lilly committed Jan 5, 2024
1 parent 9714d49 commit d7e9417
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 21 deletions.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Description: In computationally demanding analysis projects,
'clustermq' by Schubert (2019) <doi:10.1093/bioinformatics/btz284>),
and 'batchtools' by Lang, Bischl, and Surmann (2017).
<doi:10.21105/joss.00135>.
Version: 0.0.1.9001
Version: 0.0.1.9002
License: MIT + file LICENSE
URL: https://wlandau.github.io/crew.aws.batch/,
https://github.com/wlandau/crew.aws.batch
Expand All @@ -33,6 +33,7 @@ Authors@R: c(
Depends:
R (>= 4.0.0)
Imports:
cli (>= 3.1.0),
crew (>= 0.7.0),
paws.common (>= 0.7.0),
paws.compute,
Expand Down
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ export(crew_controller_aws_batch)
export(crew_launcher_aws_batch)
export(crew_monitor_aws_batch)
importFrom(R6,R6Class)
importFrom(cli,cli_progress_bar)
importFrom(cli,cli_progress_done)
importFrom(cli,cli_progress_update)
importFrom(crew,crew_assert)
importFrom(crew,crew_class_launcher)
importFrom(crew,crew_deprecate)
Expand Down
3 changes: 2 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# crew.cluster 0.0.1.9001
# crew.cluster 0.0.1.9002

* Use `paws.common::paginate()` to get the full log of a job (#5). Requires `paws.common` >= 0.7.0 due to https://github.com/paws-r/paws/issues/721.
* Rename `crew_aws_batch_monitor()` to `crew_monitor_aws_batch()` for syntactic consistency.
* Allow `terminate()` method of the monitor to terminate multiple job IDs. Also add a `cli` progress bar.

# crew.cluster 0.0.1

Expand Down
26 changes: 18 additions & 8 deletions R/crew_monitor_aws_batch.R
Original file line number Diff line number Diff line change
Expand Up @@ -629,24 +629,28 @@ crew_class_monitor_aws_batch <- R6::R6Class(
)
# nocov end
},
#' @description Terminate an AWS Batch job.
#' @description Terminate one or more AWS Batch jobs.
#' @return `NULL` (invisibly).
#' @param id Character of length 1, ID of the AWS Batch job to terminate.
#' @param ids Character vector with the IDs of the AWS Batch jobs
#' to terminate.
#' @param reason Character of length 1, natural language explaining
#' the reason the job was terminated.
#' @param verbose Logical of length 1, whether to show a progress bar
#' if the R process is interactive and `length(ids)` is greater than 1.
terminate = function(
id,
reason = "terminated by crew.aws.batch monitor"
ids,
reason = "terminated by crew.aws.batch monitor",
verbose = TRUE
) {
# Covered in tests/interactive/jobs.R
# nocov start
crew::crew_assert(
id,
ids,
is.character(.),
length(.) > 0L,
!anyNA(.),
length(.) == 1L,
nzchar(.),
message = "job ID must be a valid character of length 1"
message = "'ids' must be a valid nonempty character"
)
crew::crew_assert(
reason,
Expand All @@ -656,8 +660,14 @@ crew_class_monitor_aws_batch <- R6::R6Class(
nzchar(.),
message = "'reason' must be a valid character of length 1"
)
crew::crew_assert(verbose, isTRUE(.) || isFALSE(.))
client <- private$.client()
client$terminate_job(jobId = id, reason = reason)
progress <- progress_init(verbose = verbose, total = length(ids))
for (id in ids) {
client$terminate_job(jobId = id, reason = reason)
progress_update(progress)
}
progress_terminate(progress)
invisible()
# nocov end
},
Expand Down
1 change: 1 addition & 0 deletions R/crew_package.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#' [`rrq`](https://mrc-ide.github.io/rrq/),
#' [`clustermq`](https://mschubert.github.io/clustermq/),
#' and [`batchtools`](https://mllg.github.io/batchtools/).
#' @importFrom cli cli_progress_bar cli_progress_done cli_progress_update
#' @importFrom crew crew_assert crew_class_launcher crew_deprecate
#' crew_launcher crew_random_name crew_tls
#' @importFrom paws.common get_config paginate
Expand Down
13 changes: 9 additions & 4 deletions man/crew_class_monitor_aws_batch.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

48 changes: 41 additions & 7 deletions tests/monitor/test-jobs.R
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,6 @@ test_that("job list", {
expect_true(tibble::is_tibble(succeeded <- x$succeeded()))
expect_true(tibble::is_tibble(failed <- x$failed()))
expect_true(nrow(jobs) > 0L)
exp <- nrow(submitted) +
nrow(pending) +
nrow(runnable) +
nrow(running) +
nrow(succeeded) +
nrow(failed)
expect_equal(nrow(jobs), exp)
expect_true(job$name %in% jobs$name)
expect_true(job$id %in% jobs$id)
expect_true(job$arn %in% jobs$arn)
Expand Down Expand Up @@ -128,3 +121,44 @@ test_that("job logs", {
expect_true(tibble::is_tibble(log))
expect_equal(log$message, c("done with container", "done with job"))
})

test_that("job terminate", {
x <- crew_monitor_aws_batch(
job_definition = "crew-aws-batch-test",
job_queue = "crew-aws-batch-job-queue",
region = "us-east-2"
)
x$register(
image = "alpine:latest",
platform_capabilities = "EC2",
memory_units = "mebibytes",
memory = 128,
cpus = 1,
seconds_timeout = 600,
tags = c("crew_aws_batch_1", "crew_aws_batch_2"),
propagate_tags = TRUE
)
on.exit(x$deregister())
n <- 2L
for (index in seq_len(n)) {
x$submit(
command = c("sleep", "600"),
memory_units = "mebibytes",
memory = 128,
cpus = 1,
seconds_timeout = 600,
tags = c("crew_aws_batch_1", "crew_aws_batch_2"),
propagate_tags = TRUE
)
}
while (nrow(x$active()) < n) {
message(nrow(x$active()))
Sys.sleep(0.5)
}
x$terminate(ids = x$active()$id, verbose = TRUE)
while (nrow(x$active()) > 0L) {
message(paste(x$active()$status, collapse = " "))
Sys.sleep(0.5)
}
expect_equal(nrow(x$active()), 0L)
})

0 comments on commit d7e9417

Please sign in to comment.