Skip to content

Commit ebd2d71

Browse files
committed
Add caching layer for builds file
1 parent 660d068 commit ebd2d71

1 file changed

Lines changed: 41 additions & 10 deletions

File tree

R/init.R

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,19 @@
159159
return("")
160160
}
161161

162+
.defaultBuildsFile <- function(tgt, force=FALSE) {
163+
pkgdir <- tools::R_user_dir(packageName()) # ~/.local/share/R/ + package
164+
if (dir.exists(pkgdir)) {
165+
fname <- file.path(pkgdir, paste0("builds.", tgt, ".rds"))
166+
if (file.exists(fname) || force) {
167+
return(fname)
168+
}
169+
} else {
170+
.debug_message("No builds file for ", tgt, "\n")
171+
}
172+
return("")
173+
}
174+
162175

163176
.loadConfig <- function() {
164177
if (is.na(match("config_file", names(.pkgenv)))) {
@@ -369,24 +382,39 @@
369382
file.exists(tfile)
370383
}
371384

372-
.allBuilds <- function(tgt, pltfrm) {
385+
## The caching layer helps with the build matrix and avoids a possible (if rare) error
386+
## when downloading it in each build run
387+
.allBuilds <- function(tgt, hrs = .pkgenv[["cache_age_hours_cran_db"]]) {
373388
if (missing(tgt)) tgt <- .pkgenv[["distribution_name"]]
374389
if (missing(pltfrm)) pltfrm <- .platform()
375-
tfile <- tempfile(fileext=".rds")
376-
url <- file.path("https://r2u.stat.illinois.edu/ubuntu/pool/dists", tgt, "main/builds.rds")
377-
.retrying.downloader(url, tfile)
378-
B <- readRDS(tfile)
379-
unlink(tfile)
390+
B <- NULL
391+
bfile <- .defaultBuildsFile(tgt)
392+
if (file.exists(bfile) && !is.null(hrs)) {
393+
age <- as.numeric(difftime(Sys.time(), file.info(bfile)$ctime, units="hours"))
394+
if (age < hrs) {
395+
B <- readRDS(bfile)
396+
.debug_message("Cached all builds\n")
397+
}
398+
}
399+
if (is.null(B)) {
400+
.debug_message("Fresh builds file for ", tgt, "\n")
401+
url <- file.path("https://r2u.stat.illinois.edu/ubuntu/pool/dists", tgt, "main/builds.rds")
402+
bfile <- .defaultBuildsFile(tgt, force=TRUE)
403+
download.file(url, bfile, quiet=TRUE)
404+
B <- readRDS(bfile)
405+
.debug_message("Fresh all builds\n")
406+
}
380407
B <- B[arch %in% c("all", pltfrm), ]
381408
B
382409
}
383410

384-
.loadBuilds <- function(tgt, pltfrm) {
411+
.loadBuilds <- function(tgt, pltfrm, force=TRUE) {
385412
if (missing(tgt)) tgt <- .pkgenv[["distribution_name"]]
386413
if (missing(pltfrm)) pltfrm <- .platform()
387414
dd <- file.path(.pkgenv[["deb_directory"]], "dists", tgt, "main")
388415
if (isFALSE(.pkgenv[["in_docker"]]) && isTRUE(nzchar(dd)) && dir.exists(dd)) { ## this is specific to local build with the local pool of builds
389416
## cannot read builds.rds which only exists for noble
417+
.debug_message("Read build files\n")
390418
cwd <- getwd()
391419
setwd(dd)
392420
fls <- list.files(".", pattern="\\.deb$", full.names=FALSE)
@@ -398,13 +426,14 @@
398426
B <- B[arch %in% c("all", pltfrm), ]
399427
.pkgenv[["builds"]] <- B
400428
setwd(cwd)
401-
} else if (nzchar(Sys.getenv("CI", ""))) { ## this is specific to the arm64 build at GH
429+
} else if (nzchar(Sys.getenv("CI", "")) && isTRUE(force)) { # this is specific to the builds at GH
402430
## get packages already Built
403431
#B <- data.table::fread(cmd=r"(links -dump https://r2u.stat.illinois.edu/ubuntu/pool/dists/noble/main/| awk '/r-.*arm64.deb/ { print $1 "," $2 " "$3 "," $4 }')", col.names=c("file","date","size"))
404432
#B[, version := gsub(".*_(.*)_arm64.deb", "\\1", file), by=file]
405433
#B[, r2u := grepl("ca2404", version), by=file] # needed ?
406434
#B[, ver := gsub("(.*)-(\\d\\.ca\\d{4}\\.\\d)$", "\\1", version)][] # upstream
407435
#B[, pkgver := gsub("(.*)-(\\d\\.ca\\d{4}\\.\\d)_(arm64|amd64|all).deb$", "\\1", file)]
436+
.debug_message("Fetching builds.rds\n")
408437
B <- .allBuilds(tgt)
409438
B <- B[arch == pltfrm, ]
410439
.pkgenv[["builds"]] <- B
@@ -474,25 +503,27 @@
474503
repos = c(CRAN="https://cran.r-project.org")) # instead of cloud.r-p.o
475504
}
476505

506+
## execute when r2u functions executed via :: or :::
477507
.onLoad <- function(libname, pkgname) {
478508
.setOptions()
479509
.loadConfig()
480510
.checkSystem()
481511
.loadDB()
482512
.loadAP()
483-
.loadBuilds()
513+
.loadBuilds(force=FALSE)
484514
.loadBuildDepends()
485515
.loadBlacklist()
486516
.loadRuntimedepends()
487517
}
488518

519+
## executed when library(r2u) is called
489520
.onAttach <- function(libname, pkgname) {
490521
.setOptions()
491522
.loadConfig()
492523
.checkSystem()
493524
.loadDB()
494525
.loadAP()
495-
.loadBuilds()
526+
.loadBuilds(force=FALSE)
496527
.loadBuildDepends()
497528
.loadBlacklist()
498529
.loadRuntimedepends()

0 commit comments

Comments
 (0)