Skip to content

Commit

Permalink
log printing with writeLines()
Browse files Browse the repository at this point in the history
  • Loading branch information
wlandau committed Jan 30, 2025
1 parent 4dbd263 commit e81303b
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 70 deletions.
14 changes: 12 additions & 2 deletions R/crew_monitor_aws_batch.R
Original file line number Diff line number Diff line change
Expand Up @@ -293,9 +293,18 @@ crew_class_monitor_aws_batch <- R6::R6Class(
#' @return A `tibble` with log information.
#' @param id Character of length 1, job ID. This is different
#' from the user-supplied job name.
#' @param tibble `TRUE` to return a `tibble`, `FALSE` to print the
#' lines of text using `writeLines()`.
#' @param path Character string, file path to pass to `writeLines()`
#' if `tibble` is `FALSE`.
#' @param start_from_head Logical of length 1, whether to print earlier
#' log events before later ones.
log = function(id, start_from_head = FALSE) {
log = function(
id,
tibble = FALSE,
path = stdout(),
start_from_head = FALSE
) {
# Covered in tests/interactive/jobs.R
# nocov start
crew::crew_assert(
Expand Down Expand Up @@ -344,7 +353,8 @@ crew_class_monitor_aws_batch <- R6::R6Class(
if (!length(out)) {
return(null_log)
}
do.call(what = vctrs::vec_rbind, args = out)
out <- do.call(what = vctrs::vec_rbind, args = out)
if_any(tibble, out, writeLines(text = out$message, con = path))
# nocov end
},
#' @description List all the jobs in the given job queue
Expand Down
59 changes: 29 additions & 30 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -182,66 +182,65 @@ Method `status()` checks the status of an individual job.
```{r}
monitor$status(id = job2$id)
#> # A tibble: 1 × 8
#> name id arn status reason created started stopped
#> <chr> <chr> <chr> <chr> <chr> <dbl> <dbl> <dbl>
#> 1 job2 c38d55ad-4a86-43… arn:… runnable NA 1.70e12 NA NA
#> name id arn status reason created started stopped
#> <chr> <chr> <chr> <chr> <chr> <dbl> <dbl> <dbl>
#> 1 job2 c38d55ad-4a86-43… arn:… runnable NA 2025-01-30 16:29:00 NA NA
```

The `jobs()` method gets the status of all the jobs within the job queue and job definition you originally supplied to `crew_monitor_aws_batch()`. This may include many more jobs than the ones you submitted during the life cycle of the current `monitor` object.

```{r}
monitor$jobs()
monitor$jobs()[, c("name", "id", "arn", "status", "reason")]
#> # A tibble: 2 × 8
#> name id arn status reason created started stopped
#> <chr> <chr> <chr> <chr> <chr> <dbl> <dbl> <dbl>
#> 1 job1 653df636-ac74-43… arn:… succeeded Essen… 1.70e12 1.70e12 1.70e12
#> 2 job2 c38d55ad-4a86-43… arn:… runnable NA 1.70e12 NA NA
#> name id arn status reason
#> <chr> <chr> <chr> <chr> <chr>
#> 1 job1 653df636-ac74-43… arn:… succeeded Essen…
#> 2 job2 c38d55ad-4a86-43… arn:… runnable NA
```

The [job state](https://docs.aws.amazon.com/batch/latest/userguide/job_states.html) can be `"submitted"`, `"pending"`, `"runnable"`, `"starting"`, `"running"`, `"succeeded"`, or `"failed"`. The monitor has a method for each job state to get only the jobs with that state.

```{r}
monitor$succeeded()
monitor$succeeded()[, c("name", "id", "arn", "status", "reason")]
#> # A tibble: 1 × 8
#> name id arn status reason created started stopped
#> <chr> <chr> <chr> <chr> <chr> <dbl> <dbl> <dbl>
#> 1 job1 653df636-ac74-43… arn:… succeeded NA 1.70e12 1.70e12 1.70e12
#> name id arn status reason
#> <chr> <chr> <chr> <chr> <chr>
#> 1 job1 653df636-ac74-43… arn:… succeeded NA
```

In addition, there is an `active()` method for just states `"submitted"`, `"pending"`, `"runnable"`, `"starting"`, and `"running"`, and there is an `inactive()` method for just the `"succeeded"` and `"failed"` states.

```{r}
monitor$inactive()
monitor$inactive()[, c("name", "id", "arn", "status", "reason")]
#> # A tibble: 1 × 8
#> name id arn status reason created started stopped
#> <chr> <chr> <chr> <chr> <chr> <dbl> <dbl> <dbl>
#> 1 job1 653df636-ac74-43… arn:… succeeded NA 1.70e12 1.70e12 1.70e12
```
#> name id arn status reason
#> <chr> <chr> <chr> <chr> <chr>
#> 1 job1 653df636-ac74-43… arn:… succeeded NA
``
To terminate a job, use the `terminate()` method. This has the effect of both canceling and terminating the job, although you may not see the change right away if the job is currently `"runnable"`. Manually terminated jobs are listed as failed.
```{r}
monitor$terminate(id = job2$id)
```

To get the CloudWatch logs of a job, use the `log()` method. This method returns a `tibble` with the log messages and numeric timestamps.
To get the CloudWatch logs of a job, use the `log()` method. This method writes the log messages with `writeLines()`.

```{r}
log <- monitor$log(id = job1$id)
log
#> # A tibble: 2 × 3
#> message timestamp ingestion_time
#> <chr> <dbl> <dbl>
#> 1 hello 1702068378163 1702068378245
#> 2 world 1702068378163 1702068378245
```{}
monitor$log(id = job1$id, tibble = TRUE)
#> hello
#> world
```

If the log messages are too long to conveniently view in the `tibble`, you can print them to your screen with `cat()` or `writeLines()`.
To print the result as a `tibble` with time stamps alongside the log message text, set `tibble = TRUE` in `log()`.

```{r}
writeLines(log$message)
#> hello
#> world
monitor$log(id = job1$id, tibble = TRUE)
#> # A tibble: 2 × 3
#> message timestamp ingestion_time
#> <chr> <dbl> <dbl>
#> 1 hello 2025-01-30 16:29:00 2025-01-30 16:29:03
#> 2 world 2025-01-30 16:29:00 2025-01-30 16:29:03
```

# Using `crew` with AWS Batch workers
Expand Down
71 changes: 33 additions & 38 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -238,9 +238,9 @@ Method `status()` checks the status of an individual job.
``` r
monitor$status(id = job2$id)
#> # A tibble: 1 × 8
#> name id arn status reason created started stopped
#> <chr> <chr> <chr> <chr> <chr> <dbl> <dbl> <dbl>
#> 1 job2 c38d55ad-4a86-43… arn:… runnable NA 1.70e12 NA NA
#> name id arn status reason created started stopped
#> <chr> <chr> <chr> <chr> <chr> <dbl> <dbl> <dbl>
#> 1 job2 c38d55ad-4a86-43… arn:… runnable NA 2025-01-30 16:29:00 NA NA
```

The `jobs()` method gets the status of all the jobs within the job queue
Expand All @@ -250,12 +250,12 @@ ones you submitted during the life cycle of the current `monitor`
object.

``` r
monitor$jobs()
monitor$jobs()[, c("name", "id", "arn", "status", "reason")]
#> # A tibble: 2 × 8
#> name id arn status reason created started stopped
#> <chr> <chr> <chr> <chr> <chr> <dbl> <dbl> <dbl>
#> 1 job1 653df636-ac74-43… arn:… succeeded Essen… 1.70e12 1.70e12 1.70e12
#> 2 job2 c38d55ad-4a86-43… arn:… runnable NA 1.70e12 NA NA
#> name id arn status reason
#> <chr> <chr> <chr> <chr> <chr>
#> 1 job1 653df636-ac74-43… arn:… succeeded Essen…
#> 2 job2 c38d55ad-4a86-43… arn:… runnable NA
```

The [job
Expand All @@ -265,11 +265,11 @@ can be `"submitted"`, `"pending"`, `"runnable"`, `"starting"`,
each job state to get only the jobs with that state.

``` r
monitor$succeeded()
monitor$succeeded()[, c("name", "id", "arn", "status", "reason")]
#> # A tibble: 1 × 8
#> name id arn status reason created started stopped
#> <chr> <chr> <chr> <chr> <chr> <dbl> <dbl> <dbl>
#> 1 job1 653df636-ac74-43… arn:… succeeded NA 1.70e12 1.70e12 1.70e12
#> name id arn status reason
#> <chr> <chr> <chr> <chr> <chr>
#> 1 job1 653df636-ac74-43… arn:… succeeded NA
```

In addition, there is an `active()` method for just states
Expand All @@ -278,42 +278,37 @@ and there is an `inactive()` method for just the `"succeeded"` and
`"failed"` states.

``` r
monitor$inactive()
monitor$inactive()[, c("name", "id", "arn", "status", "reason")]
#> # A tibble: 1 × 8
#> name id arn status reason created started stopped
#> <chr> <chr> <chr> <chr> <chr> <dbl> <dbl> <dbl>
#> 1 job1 653df636-ac74-43… arn:… succeeded NA 1.70e12 1.70e12 1.70e12
```
#> name id arn status reason
#> <chr> <chr> <chr> <chr> <chr>
#> 1 job1 653df636-ac74-43… arn:… succeeded NA
``

To terminate a job, use the `terminate()` method. This has the effect of
both canceling and terminating the job, although you may not see the
change right away if the job is currently `"runnable"`. Manually
terminated jobs are listed as failed.
To terminate a job, use the `terminate()` method. This has the effect of both canceling and terminating the job, although you may not see the change right away if the job is currently `"runnable"`. Manually terminated jobs are listed as failed.
```

``` r
monitor$terminate(id = job2$id)
```

To get the CloudWatch logs of a job, use the `log()` method. This method
returns a `tibble` with the log messages and numeric timestamps.
writes the log messages with `writeLines()`.

``` r
log <- monitor$log(id = job1$id)
log
#> # A tibble: 2 × 3
#> message timestamp ingestion_time
#> <chr> <dbl> <dbl>
#> 1 hello 1702068378163 1702068378245
#> 2 world 1702068378163 1702068378245
```
monitor$log(id = job1$id, tibble = TRUE)
#> hello
#> world

If the log messages are too long to conveniently view in the `tibble`,
you can print them to your screen with `cat()` or `writeLines()`.
To print the result as a `tibble` with time stamps alongside the log
message text, set `tibble = TRUE` in `log()`.

``` r
writeLines(log$message)
#> hello
#> world
monitor$log(id = job1$id, tibble = TRUE)
#> # A tibble: 2 × 3
#> message timestamp ingestion_time
#> <chr> <dbl> <dbl>
#> 1 hello 2025-01-30 16:29:00 2025-01-30 16:29:03
#> 2 world 2025-01-30 16:29:00 2025-01-30 16:29:03
```

# Using `crew` with AWS Batch workers
Expand Down Expand Up @@ -421,7 +416,7 @@ citation("crew.aws.batch")
To cite package 'crew.aws.batch' in publications use:

Landau WM (????). _crew.aws.batch: A Crew Launcher Plugin for AWS
Batch_. R package version 0.0.7,
Batch_. R package version 0.0.8,
https://github.com/wlandau/crew.aws.batch,
<https://wlandau.github.io/crew.aws.batch/>.

Expand All @@ -430,7 +425,7 @@ A BibTeX entry for LaTeX users is
@Manual{,
title = {crew.aws.batch: A Crew Launcher Plugin for AWS Batch},
author = {William Michael Landau},
note = {R package version 0.0.7,
note = {R package version 0.0.8,
https://github.com/wlandau/crew.aws.batch},
url = {https://wlandau.github.io/crew.aws.batch/},
}
Expand Down

0 comments on commit e81303b

Please sign in to comment.