Open fissehab opened 2 years ago
You can try something like.
start_tunnel_process <- function(user, server, port, stderr = nullfile()) {
proc <- callr::r_bg(
function(user, server, port) {
ssh_host <- paste(user, server, sep = "@")
while (TRUE) {
ssh_session <- ssh::ssh_connect(ssh_host, keyfile = "~/.ssh/id_rsa", verbose = FALSE)
ssh::ssh_tunnel(ssh_session, port = port, target = "localhost:27017")
ssh::ssh_disconnect(ssh_session)
}
},
args = list(user, server, port),
stdout = nullfile(),
stderr = stderr
)
proc
}
I currently evaluating this for my use case with mongolite.
url <- sprintf("mongodb://%s:%s@%s:%s", user, password, host, as.integer(port))
con <- mongo(collection, db = database, url = url)
con$find()
con$disconnect()
You need the while loop since after you disconnect the tunnel will close. The while will create a new tunnel. This also means it will fail, if the time between two connections is to short for the 2nd process to create a new tunnel. You can hack this by putting a fault tolerant loop around your connect function.
for (i in seq_len(100)) {
status <- try(con <- mongo(collection, db = database, url = url), silent = TRUE)
if (!inherits(status, "try-error")) break
Sys.sleep(0.2)
}
This works, but is also kind of a hack since the background R process runs at 100% CPU all the time which is to much.
I am using the below Python code to get data from a remote Redshift database. I am using this function with the reticulate package in R. I want to convert the Python code to R so that I do not face the hassle of setting up an Anaconda environment in the cloud when I push my code (shiny app) to shipapps.io. I read about the SSH package and its capabilities but I could not successfully convert my code using it.
Then I am sourcing the Python function above to get data from the database.
But I want to avoid the Python part and use R only.