Skip to content

Commit

Permalink
Check internet connexion also for learnr tutorials, with explicit err…
Browse files Browse the repository at this point in the history
…or in case of failure
  • Loading branch information
phgrosjean committed Sep 14, 2020
1 parent 07705bf commit c5ea926
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
2 changes: 1 addition & 1 deletion R/config.R
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ debug = Sys.getenv("LEARNDOWN_DEBUG", 0) != 0) {
if (!res)
stop("This computer does not seems to have access to the Internet, ", "
impossible to record events in the database ",
"(but they are temporary saved on this computer).", call. = FALSE)
"(but they are saved on this computer for now).", call. = FALSE)
}
res <- try(check_internet_access(), silent = TRUE)
if (inherits(res, "try-error")) {
Expand Down
18 changes: 18 additions & 0 deletions R/learndown_learnr.R
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,24 @@ record_learnr <- function(tutorial_id, tutorial_version, user_id, event, data) {
# Only test we can open the database... otherwise set the system to only
# record locally (otherwise, it will be too slow to retest each time).
if (is.null(data)) {
# First look of there is an internet connexion
check_internet_access <- function() {
cmd <- switch(.Platform$OS.type, "windows" = "ipconfig", "ifconfig")
res <- any(grep("(\\d+(\\.|$)){4}", system(cmd, intern = TRUE)))
if (!res)
stop("This computer does not seems to have access to the Internet, ", "
impossible to record events in the database ",
"(but they are saved on this computer for now).", call. = FALSE)
}
res <- try(check_internet_access(), silent = TRUE)
if (inherits(res, "try-error")) {
options(learndown_learnr_record = FALSE)
if (debug)
message("Testing Internet access failed: ", as.character(res))
return(res)
}

# Try to access the database
m <- try(mongo(collection = "learnr", db = db, url = glue(url)),
silent = TRUE)
if (inherits(m, "try-error")) {
Expand Down

0 comments on commit c5ea926

Please sign in to comment.