Skip to content

Commit 5bf7f59

Browse files
authored
feat: add get_log(job) (#356)
1 parent c7d59b8 commit 5bf7f59

36 files changed

+349
-15
lines changed

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ export(get_groups)
8282
export(get_image)
8383
export(get_job)
8484
export(get_jobs)
85+
export(get_log)
8586
export(get_my_permission)
8687
export(get_oauth_content_credentials)
8788
export(get_oauth_credentials)

NEWS.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
# connectapi (development version)
22

3+
## New features
4+
5+
- New `get_log()` function which lets you get the log for a given job.
6+
(#341)
7+
- New `get_job_list()` function returns a list of jobs for a content item.
8+
(#341)
9+
10+
## Newly deprecated
11+
12+
- `get_job()` (singular) is now deprecated, its functionality taken care of by
13+
other functions, including `get_log()`.
14+
315
# connectapi 0.5.0
416

517
## Breaking changes

R/content.R

Lines changed: 87 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ Content <- R6::R6Class(
111111
x
112112
})
113113
}
114-
parsed
114+
purrr::map(parsed, ~ purrr::list_modify(.x, app_guid = self$content$guid))
115115
},
116116
#' @description Return a single job for this content.
117117
#' @param key The job key.
@@ -619,17 +619,34 @@ content_ensure <- function(
619619
#' Note that Connect versions below 2022.10.0 use a legacy endpoint, and will
620620
#' not return the complete set of information provided by newer versions.
621621
#'
622+
#' `get_jobs()` returns job data as a data frame, whereas `get_jobs_list()`
623+
#' returns job data in a list.
624+
#'
625+
#' You might get job data as a data frame if you want to perform some
626+
#' calculations about job data (e.g. counting server processes over time), or if
627+
#' you want to filter jobs to find a specific key.
628+
#'
629+
#' The objects in list returned by `get_jobs_list()` are useful if you want to
630+
#' take an action on a job, such as getting its process log with
631+
#' `get_log()`.
632+
#'
622633
#' @param content A Content object, as returned by `content_item()`
623634
#'
624-
#' @return A data frame with a row for each job, with the following columns:
635+
#' @return
636+
#'
637+
#' - `get_jobs()`: A data frame with a row representing each job.
638+
#' - `get_job_list()`: A list with each element representing a job.
639+
#'
640+
#' Jobs contain the following fields:
625641
#'
626642
#' - `id`: The job identifier.
627643
#' - `ppid`: The job's parent process identifier (see Note 1).
628644
#' - `pid`: The job's process identifier.
629645
#' - `key`: The job's unique key identifier.
630646
#' - `remote_id`: The job's identifier for off-host execution configurations
631647
#' (see Note 1).
632-
#' - `app_id`: The job's parent content identifier
648+
#' - `app_id`: The job's parent content identifier.
649+
#' - `app_guid`: The job's parent content GUID.
633650
#' - `variant_id`: The identifier of the variant owning this job.
634651
#' - `bundle_id`: The identifier of a content bundle linked to this job.
635652
#' - `start_time`: The timestamp (RFC3339) indicating when this job started.
@@ -658,10 +675,19 @@ content_ensure <- function(
658675
#' - `run_as`: The UNIX user that executed this job.
659676
#'
660677
#' @note
661-
#' 1. On Connect instances earlier than 2022.10.0, these columns will contain `NA` values.
678+
#' 1. On Connect instances earlier than 2022.10.0, these fields will contain `NA` values.
679+
#'
680+
#' @examples
681+
#' \dontrun{
682+
#' client <- connect()
683+
#' item <- content_item(client, "951bf3ad-82d0-4bca-bba8-9b27e35c49fa")
684+
#' jobs <- get_jobs(item)
685+
#' job_list <- get_job_list(item)
686+
#' }
662687
#'
663688
#' @family job functions
664689
#' @family content functions
690+
#' @rdname get_jobs
665691
#' @export
666692
get_jobs <- function(content) {
667693
validate_R6_class(content, "Content")
@@ -682,7 +708,7 @@ get_jobs <- function(content) {
682708
#' @family content functions
683709
#' @export
684710
get_job <- function(content, key) {
685-
warn_experimental("get_job")
711+
lifecycle::deprecate_warn("0.6", "get_job()", "get_log()")
686712
scoped_experimental_silence()
687713
validate_R6_class(content, "Content")
688714

@@ -704,7 +730,7 @@ get_job <- function(content, key) {
704730
#' @param keys Optional. One or more job keys, which can be obtained using
705731
#' `get_jobs(content)`. If no keys are provided, will terminate all active
706732
#' jobs for the provided content item.
707-
733+
#'
708734
#' @return A data frame with the status of each termination request.
709735
#'
710736
#' - `app_id`: The content item's identifier.
@@ -718,6 +744,13 @@ get_job <- function(content, key) {
718744
#' Note that `app_id`, `app_guid`, `job_id`, and `result` are `NA` if the
719745
#' request returns an error.
720746
#'
747+
#' @examples
748+
#' \dontrun{
749+
#' client <- connect()
750+
#' item <- content_item(client, "951bf3ad-82d0-4bca-bba8-9b27e35c49fa")
751+
#' result <- terminate_jobs(item)
752+
#' }
753+
#'
721754
#' @family job functions
722755
#' @family content functions
723756
#' @export
@@ -747,6 +780,54 @@ terminate_jobs <- function(content, keys = NULL) {
747780
res_df
748781
}
749782

783+
#' @rdname get_jobs
784+
get_job_list <- function(content) {
785+
validate_R6_class(content, "Content")
786+
787+
purrr::map(content$jobs(), ~ purrr::list_modify(.x, client = content$connect))
788+
}
789+
790+
#' Get Job Log
791+
#'
792+
#' Get the log output for a job. Requires Connect 2022.10.0 or newer.
793+
#'
794+
#' Note: The output of `get_jobs()` cannot be used with `get_log()`.
795+
#' Please use an object from the list returned by `get_job_list()`.
796+
#'
797+
#' @param job A job, represented by an element from the list returned by `get_job_list()`.
798+
#' @param max_log_lines Optional. An integer indicating the maximum number of
799+
#' log lines to return. If `NULL` (default), Connect returns a maximum of 5000
800+
#' lines.
801+
#'
802+
#' @return A data frame with the requested log. Each row represents an entry.
803+
#'
804+
#' - `source`: `stdout` or `stderr`
805+
#' - `timestamp`: The time of the entry.
806+
#' - `data`: The logged text.
807+
#'
808+
#' @examples
809+
#' \dontrun{
810+
#' client <- connect()
811+
#' item <- content_item(client, "951bf3ad-82d0-4bca-bba8-9b27e35c49fa")
812+
#' jobs <- get_job_list(item)
813+
#' log <- get_log(jobs[[1]])
814+
#' }
815+
#'
816+
#'
817+
#' @family job functions
818+
#' @family content functions
819+
#' @export
820+
get_log <- function(job, max_log_lines = NULL) {
821+
error_if_less_than(job$client$version, "2022.10.0")
822+
823+
query <- list(maxLogLines = max_log_lines)
824+
res <- job$client$GET(
825+
v1_url("content", job$app_guid, "jobs", job$key, "log"),
826+
query = query
827+
)
828+
parse_connectapi_typed(res$entries, connectapi_ptypes$job_log)
829+
}
830+
750831
#' Set RunAs User
751832
#'
752833
#' Set the `RunAs` user for a piece of content.

R/parse.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,8 @@ parse_connect_rfc3339 <- function(x) {
169169
# Times with and without offsets require different formats.
170170
format_string <- ifelse(
171171
grepl("Z$", .x),
172-
"%Y-%m-%dT%H:%M:%SZ",
173-
"%Y-%m-%dT%H:%M:%S%z"
172+
"%Y-%m-%dT%H:%M:%OSZ",
173+
"%Y-%m-%dT%H:%M:%OS%z"
174174
)
175175
as.POSIXct(strptime(.x, format = format_string, tz = "UTC"))
176176
})

R/ptype.R

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ connectapi_ptypes <- list(
167167
key = NA_character_,
168168
remote_id = NA_character_,
169169
app_id = NA_character_,
170+
app_guid = NA_character_,
170171
variant_id = NA_character_,
171172
bundle_id = NA_character_,
172173
start_time = NA_datetime_,
@@ -238,5 +239,10 @@ connectapi_ptypes <- list(
238239
content_guid = NA_character_,
239240
path = NA_character_,
240241
created_time = NA_datetime_
242+
),
243+
job_log = tibble::tibble(
244+
source = NA_character_,
245+
timestamp = NA_datetime_,
246+
data = NA_character_
241247
)
242248
)

man/content_delete.Rd

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/content_item.Rd

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/content_title.Rd

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/content_update.Rd

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/create_random_name.Rd

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)