From 87210d668704f5f99c567cf2238e36ff1c6bd275 Mon Sep 17 00:00:00 2001 From: oweno-tfwm Date: Fri, 8 Sep 2023 11:15:48 +0100 Subject: [PATCH] 1) decrease sensitivity of update check to only look at date instead of date+time (this means if github can't be reached should only nag once per day to update instead of on every single package load) 2) add timeout to update check on package load and make it short (but make default when explicitly called long) --- R/extdata.R | 19 +++++++++++++------ R/zzz.R | 2 +- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/R/extdata.R b/R/extdata.R index ea2dd4b..f16e88b 100644 --- a/R/extdata.R +++ b/R/extdata.R @@ -9,9 +9,9 @@ #' #' @export #' -update_data <- function(){ +update_data <- function( timeout=60 ){ - check <- check_data() + check <- check_data( timeout=timeout ) if(check$date_package != check$date){ @@ -72,12 +72,12 @@ download_data <- function(tag_name, package_location, date){ #' @return TRUE if data is up-to-date or if unable to check #' @noRd -check_data <- function(default_tag = "v0.1.2"){ +check_data <- function( timeout = 60, default_tag = "v0.1.2"){ # Try not to hammer the API Sys.sleep(5) # Check date on data repo - res = try(httr::GET("https://api.github.com/repos/ITSleeds/UK2GTFS-data/releases"), - silent = TRUE) + res = try(httr::GET("https://api.github.com/repos/ITSleeds/UK2GTFS-data/releases", httr::timeout(get("timeout")), + silent = TRUE )) if(inherits(res, "try-error")){ message("Unable to check for latest data") date = Sys.time() @@ -94,13 +94,20 @@ check_data <- function(default_tag = "v0.1.2"){ } } + date = as.Date(date) #make it less sensitive by only comparing date rather than date+time + #Check if date.txt in package package_location <- system.file(package = "UK2GTFS") if(!file.exists(file.path(package_location, "extdata/date.txt"))){ writeLines("nodata", file.path(package_location, "extdata/date.txt")) } - date_package <- readLines(file.path(package_location, "extdata/date.txt")) + tryCatch({ + date_package <- as.Date( readLines(file.path(package_location, "extdata/date.txt")) ) + }, error = function(err) { + date_package = "nodata" + }) + return(list(date_package = date_package, date = date, tag_name = tag_name, package_location = package_location)) diff --git a/R/zzz.R b/R/zzz.R index 8a36620..1681d77 100644 --- a/R/zzz.R +++ b/R/zzz.R @@ -5,7 +5,7 @@ tryCatch({ if( TRUE == UK2GTFS_option_updateCachedDataOnLibaryLoad() ) { - update_data() + update_data( timeout=10 ) } }, error = function(err) { warning(Sys.time(), " Process id=", Sys.getpid(), " threw errors during package load while calling update_data() :", err)