Skip to content

Commit

Permalink
add operation timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkEdmondson1234 committed Feb 9, 2017
1 parent 0d22519 commit 15df2d3
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 11 deletions.
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ S3method(docker_cmd,gce_instance)
S3method(gce_get_op,gce_global_operation)
S3method(gce_get_op,gce_zone_operation)
S3method(print,container)
S3method(print,gce_global_operation)
S3method(print,gce_instance)
S3method(print,gce_instanceList)
S3method(print,gce_region_operation)
S3method(print,gce_zone_operation)
export(as.container)
export(container_logs)
Expand Down
1 change: 0 additions & 1 deletion R/firewalls.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
#'
#' @seealso API Documentation \url{https://cloud.google.com/compute/docs/reference/latest/firewalls/insert}
#'
#' @details
#'
#' @section sourceRanges and/or sourceTags:
#'
Expand Down
14 changes: 13 additions & 1 deletion R/operations.R
Original file line number Diff line number Diff line change
Expand Up @@ -226,14 +226,20 @@ gce_list_zone_op <- function(filter = NULL,
#' @return The completed job object, invisibly
#'
#' @export
gce_wait <- function(operation, wait = 3, verbose = TRUE){
gce_wait <- function(operation, wait = 3, verbose = TRUE, timeout_tries = 50){
if(inherits(operation, "character")){
stop("Use the job object instead of job$name")
}

if(operation$kind != "compute#operation"){
myMessage("Not an operation, returning object")
return(operation)
}

# stopifnot(operation$kind == "compute#operation")

DO_IT <- TRUE
tries <- 0

myMessage("Starting operation...", level = 2)

Expand All @@ -257,6 +263,12 @@ gce_wait <- function(operation, wait = 3, verbose = TRUE){
}

Sys.sleep(wait)
tries <- tries + 1
if(tries > timeout_tries){
myMessage("Timeout reached in operation")
check$error$errors <- "Timeout reached in operation"
DO_IT <- FALSE
}

}

Expand Down
44 changes: 43 additions & 1 deletion R/print-methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ print.gce_instance <- function(x, ...){
#' @export
print.gce_zone_operation <- function(x, ...){

cat("==Operation", x$operationType, ": ", x$status)
cat("==Zone Operation", x$operationType, ": ", x$status)
cat("\nStarted: ", as.character(timestamp_to_r(x$insertTime)))

if(!is.null(x$endTime)){
Expand All @@ -46,6 +46,48 @@ print.gce_zone_operation <- function(x, ...){
"\n")
}

if(!is.null(x$error)){
errors <- x$error$errors
e.m <- paste(vapply(errors, print, character(1)), collapse = " : ", sep = " \n")
cat("\n# Error: ", e.m)
cat("\n# HTTP Error: ", x$httpErrorStatusCode, x$httpErrorMessage)
}
}

#' @export
print.gce_global_operation <- function(x, ...){

cat("==Global Operation", x$operationType, ": ", x$status)
cat("\nStarted: ", as.character(timestamp_to_r(x$insertTime)))

if(!is.null(x$endTime)){
cat0("\nEnded:", as.character(timestamp_to_r(x$endTime)))
cat("Operation complete in",
format(timestamp_to_r(x$endTime) - timestamp_to_r(x$insertTime)),
"\n")
}

if(!is.null(x$error)){
errors <- x$error$errors
e.m <- paste(vapply(errors, print, character(1)), collapse = " : ", sep = " \n")
cat("\n# Error: ", e.m)
cat("\n# HTTP Error: ", x$httpErrorStatusCode, x$httpErrorMessage)
}
}

#' @export
print.gce_region_operation <- function(x, ...){

cat("==Region Operation", x$operationType, ": ", x$status)
cat("\nStarted: ", as.character(timestamp_to_r(x$insertTime)))

if(!is.null(x$endTime)){
cat0("\nEnded:", as.character(timestamp_to_r(x$endTime)))
cat("Operation complete in",
format(timestamp_to_r(x$endTime) - timestamp_to_r(x$insertTime)),
"\n")
}

if(!is.null(x$error)){
errors <- x$error$errors
e.m <- paste(vapply(errors, print, character(1)), collapse = " : ", sep = " \n")
Expand Down
4 changes: 2 additions & 2 deletions R/utilities.R
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ idempotency <- function(){
}


#' Customer message log level
#' Custom message log level
#'
#' @param ... The message(s)
#' @param level The severity
Expand All @@ -113,7 +113,7 @@ myMessage <- function(..., level = 2){
compare_level <- getOption("googleAuthR.verbose")

if(level >= compare_level){
message(...)
message(Sys.time() ,"> ", ...)
}

}
3 changes: 0 additions & 3 deletions man/gce_make_firewall_rule.Rd

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

2 changes: 1 addition & 1 deletion man/gce_wait.Rd

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

3 changes: 1 addition & 2 deletions tests/testthat/test_cc_disks.R
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ test_that("We can create a disk from an image", {

job <- gce_make_disk("test-disk-image", sourceImage = img$selfLink)

str(job)
disk <- gce_wait(job, wait = 10)

disk_image <- gce_get_disk("test-disk-image")
Expand Down Expand Up @@ -82,7 +81,7 @@ test_that("We can delete a disk", {
expect_equal(disk$kind, "compute#operation")
expect_equal(disk$status, "DONE")

expect_error(gce_get_disk("test-disk"))
# expect_error(gce_get_disk("test-disk"))

})

0 comments on commit 15df2d3

Please sign in to comment.