@@ -45,6 +45,7 @@ record_sdd <- function(tutorial_id, tutorial_version, user_id, event, data) {
4545 # }
4646 # Once http request with stitch will be available, we could do something like
4747 # https://stitch.mongodb.com/api/client/v2.0/app/sdd-relay-aizkd/service/sdd-http/incoming_webhook/webhook0
48+ db_injected <- FALSE
4849 m <- try(mongo(" sdd" ,
4950 # url = "mongodb://sdd:sdd@ds125388.mlab.com:25388/sdd-test")$insert(entry)
5051 url = " mongodb://sdd:sdd@sdd-umons-shard-00-00-umnnw.mongodb.net:27017,sdd-umons-shard-00-01-umnnw.mongodb.net:27017,sdd-umons-shard-00-02-umnnw.mongodb.net:27017/test?ssl=true&replicaSet=sdd-umons-shard-0&authSource=admin" ),
@@ -53,18 +54,24 @@ record_sdd <- function(tutorial_id, tutorial_version, user_id, event, data) {
5354 # No run() methods in mongolite 1.5
5455 # m$run(command = "{\"ping\": 1}", simplify = TRUE)$ok == 1) {
5556 m $ count() > - 1 ) {
56- m $ insert(entry )
57+ res <- try( m $ insert(entry ), silent = TRUE )
5758 # If there is something in the biodatascience file, inject it also now
58- if (file.exists(bds_file )) {
59- dat <- readLines(bds_file )
60- unlink(bds_file )
61- if (length(dat ))
62- for (i in 1 : length(dat ))
63- m $ insert(unserialize(base64_dec(dat [i ])))
59+ if (! inherits(res , " try-error" )) {
60+ db_injected <- TRUE
61+ # Check if we also need to inject pending records
62+ if (file.exists(bds_file )) {
63+ dat <- readLines(bds_file )
64+ unlink(bds_file )
65+ if (length(dat ))
66+ for (i in 1 : length(dat ))
67+ m $ insert(unserialize(base64_dec(dat [i ])))
68+ }
6469 }
6570 # No disconnect() method in mongolite 1.5
6671 # m$disconnect()
67- } else {# MongoDB database not available... save locally
72+ }
73+ # Only get rid of the entry if it was actually injected in the database
74+ if (! isTRUE(db_injected )) {# MongoDB database not available, or error... save locally
6875 add_file_base64(entry , file = bds_file )
6976 }
7077}
0 commit comments