Skip to content

Commit

Permalink
Test keyboard interrupt
Browse files Browse the repository at this point in the history
  • Loading branch information
DyfanJones authored Feb 24, 2021
2 parents bd67c42 + 7b5f31a commit 7820a7b
Show file tree
Hide file tree
Showing 11 changed files with 382 additions and 23 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ exportMethods(dbFetch)
exportMethods(dbGetInfo)
exportMethods(dbGetPartition)
exportMethods(dbGetQuery)
exportMethods(dbGetStatement)
exportMethods(dbGetTables)
exportMethods(dbHasCompleted)
exportMethods(dbIsValid)
Expand Down
8 changes: 5 additions & 3 deletions R/Connection.R
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,11 @@ setClass(
setMethod(
"show", "AthenaConnection",
function(object){
cat("AthenaConnection \n")
}
)
cat("<AthenaConnection>\n")
if (!dbIsValid(object)) {
cat(" DISCONNECTED\n")
}
})

#' Disconnect (close) an Athena connection
#'
Expand Down
36 changes: 36 additions & 0 deletions R/Result.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ AthenaResult <- function(conn,
stopifnot(is.character(statement))

response <- new.env(parent = emptyenv())
response[["Query"]] <- statement
if (athena_option_env$cache_size > 0)
response[["QueryExecutionId"]] <- check_cache(statement, conn@info$work_group)
if (is.null(response[["QueryExecutionId"]])) {
Expand Down Expand Up @@ -441,3 +442,38 @@ setMethod(

return(res@info[["Statistics"]])
})


#' Get the statement associated with a result set
#'
#' Returns the statement that was passed to [dbSendQuery()]
#' or [dbSendStatement()].
#' @name dbGetStatement
#' @inheritParams DBI::dbGetStatement
#' @return \code{dbGetStatement()} returns a character.
#' @seealso \code{\link[DBI]{dbGetStatement}}
#' @examples
#' \dontrun{
#' # Note:
#' # - Require AWS Account to run below example.
#' # - Different connection methods can be used please see `noctua::dbConnect` documnentation
#'
#' library(DBI)
#'
#' # Demo connection to Athena using profile name
#' con <- dbConnect(noctua::athena())
#'
#' rs <- dbSendQuery(con, "SHOW TABLES in default")
#' dbGetStatement(rs)
#' }
#' @docType methods
NULL

#' @rdname dbGetStatement
#' @export
setMethod(
"dbGetStatement", "AthenaResult",
function(res, ...){
con_error_msg(res, msg = "Result already cleared.")
return(res@info[["Query"]])
})
30 changes: 15 additions & 15 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,7 @@ is.s3_uri <- function(x) {
poll <- function(res){
tryCatch(
.poll(res),
interrupt = function(i){
if(res@connection@info$keyboard_interrupt){
msg = sprintf(
"Query '%s' has been cancelled by user.",
res@info[["QueryExecutionId"]])
dbClearResult(res)
stop(msg, call. = F)
} else {
msg = sprintf(
"Query '%s' has been cancelled by user but will carry on running in AWS Athena",
res@info[["QueryExecutionId"]])
stop(msg, call. = F)
}
}
interrupt = function(i) interrupt_athena(res)
)
}

Expand All @@ -44,7 +31,6 @@ poll <- function(res){
tryCatch(query_execution <- res@connection@ptr$Athena$get_query_execution(QueryExecutionId = res@info$QueryExecutionId))
if (query_execution$QueryExecution$Status$State %in% c("SUCCEEDED", "FAILED", "CANCELLED")){
# update info environment
res@info[["Query"]] <- query_execution[["QueryExecution"]][["Query"]]
res@info[["Status"]] <- query_execution[["QueryExecution"]][["Status"]][["State"]]
res@info[["StateChangeReason"]] <- query_execution[["QueryExecution"]][["Status"]][["StateChangeReason"]]
res@info[["StatementType"]] <- query_execution[["QueryExecution"]][["StatementType"]]
Expand All @@ -56,6 +42,20 @@ poll <- function(res){
}
}

interrupt_athena <- function(res){
if(res@connection@info[["keyboard_interrupt"]]){
msg = sprintf(
"Query '%s' has been cancelled by user.",
res@info[["QueryExecutionId"]])
dbClearResult(res)
} else {
msg = sprintf(
"Query '%s' has been cancelled by user but will carry on running in AWS Athena",
res@info[["QueryExecutionId"]])
}
stop(msg, call. = F)
}

# added a random poll wait time
rand_poll <- function() {runif(n = 1, min = 50, max = 100) / 100}

Expand Down
8 changes: 4 additions & 4 deletions cran-comments.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ This is hot fix to fix issue of keyboard interrupt failing to raise interrupt er
### Bug Fix:
* fix issue were keyboard interrupt didn't raise interrupt error due to 2.0.0 release

### Unit test:
* check if interrupt function successfully interrupts Athena when user manually triggers a keyboard interrupt

## Examples Note:
* All R examples with `\dontrun` have been given a note warning users that `AWS credentials` are required to run

Expand All @@ -28,7 +31,4 @@ Apologises for the quick re-release, this is a hot fix to fix "keyboard interrup
`devtools::check_rhub(env_vars=c(R_COMPILE_AND_INSTALL_PACKAGES = "always", LIBARROW_BINARY="true"))`

## unit tests (using testthat) results
* OK: 221
* Failed: 0
* Warnings: 0
* Skipped: 0
[ FAIL 0 | WARN 0 | SKIP 0 | PASS 227 ]
2 changes: 1 addition & 1 deletion docs/pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ articles:
convert_and_save_cost: convert_and_save_cost.html
getting_started: getting_started.html
how_to_retry: how_to_retry.html
last_built: 2021-02-24T11:27Z
last_built: 2021-02-24T16:57Z

225 changes: 225 additions & 0 deletions docs/reference/dbGetStatement.html

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

6 changes: 6 additions & 0 deletions docs/reference/index.html

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

Loading

0 comments on commit 7820a7b

Please sign in to comment.