Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adiciona comportamento de back-off exponencial para quando houver exceções de código #181

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions R/cache.R
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ setup_cache <- function (name) {
}

if (is.null(value)) {
print(c("Not in cache", .cache_file_path))
cat("\nNot in cache")
} else {
print(c("In cache", .cache_file_path))
cat("\nIn cache")
}
return(value)
}
39 changes: 26 additions & 13 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ if (getRversion() >= "2.15.1") utils::globalVariables(".")
status_code = 1000
resp_in_cache = FALSE
resp <- NULL
error_msg <- ''

if (is.null(base_url) || base_url == '') {
warning("URL deve ser não-nula e não-vazia.")
Expand All @@ -53,23 +54,35 @@ if (getRversion() >= "2.15.1") utils::globalVariables(".")
}

if (num_tries > 0) {
cat("\n","Error on Calling URL:",url," - Status Code:",status_code)
cat("\nError on Calling URL:", url,
" - Status Code:", status_code,
" - Attempt no.", num_tries + 1)
if (error_msg != '') cat("\nDetailed Error:",error_msg,"\n")
sleep_time <- base_sleep_time^(num_tries)
Sys.sleep(sleep_time)
}

resp <- .get_from_cache(api_url)

if (is.null(resp)) {
if (accept_json) resp <- httr::GET(url, httr::accept_json())
else resp <- httr::GET(url)
Sys.sleep(.DEF_POST_REQ_SLEEP_TIME)
status_code <- httr::status_code(resp)

error_msg <- tryCatch({
resp <- .get_from_cache(url)

if (is.null(resp)) {
if (accept_json) resp <- httr::GET(url, httr::accept_json())
else resp <- httr::GET(url)
Sys.sleep(.DEF_POST_REQ_SLEEP_TIME)
status_code <- httr::status_code(resp)
} else {
resp_in_cache <- TRUE
status_code <- 200
}

}, warning = function(w) {
message("\n","Warning while fetching: ", url)
warning(w)
}, error = function(e) {
return(e$message)
}, finally = {
num_tries <- num_tries + 1
} else {
resp_in_cache <- TRUE
status_code <- 200
}
})
}

if ((status_code >= .COD_ERRO_CLIENTE)) {
Expand Down
26 changes: 17 additions & 9 deletions tests/testthat/test_utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -79,18 +79,26 @@ test_that(".unnest_df_column returns correct result", {

test_that(".get_with_exponential_backoff_cached does not apply exponential backoff when response code is 404", {
start_time <- Sys.time()
tryCatch({
.get_with_exponential_backoff_cached(base_url="https://dadosabertos.camara.leg.br",

expect_error(.get_with_exponential_backoff_cached(base_url="https://dadosabertos.camara.leg.br",
path='/api/v2/proposicoes/-1',
base_sleep_time=1,
max_attempts=1)
}, warning = function(w) {
}, error = function(e) {
}, finally = {
end_time <- Sys.time()
})

max_attempts=1))
end_time <- Sys.time()
elapsed_time <- end_time - start_time

expect_true(elapsed_time < 3)
})

test_that(".get_with_exponential_backoff_cached applies exponential backoff when there is an exception during request", {
start_time <- Sys.time()

expect_error(.get_with_exponential_backoff_cached(base_url="https://-1",
path='',
base_sleep_time=2,
max_attempts=3))
end_time <- Sys.time()
elapsed_time <- end_time - start_time

expect_true(elapsed_time > 4)
})