Skip to content

Commit

Permalink
1) decrease sensitivity of update check to only look at date instead …
Browse files Browse the repository at this point in the history
…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)
  • Loading branch information
oweno-tfwm committed Sep 8, 2023
1 parent 0c47eb7 commit 87210d6
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
19 changes: 13 additions & 6 deletions R/extdata.R
Original file line number Diff line number Diff line change
Expand Up @@ -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){

Expand Down Expand Up @@ -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()
Expand All @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion R/zzz.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 87210d6

Please sign in to comment.