Cannot deveolop API using RestRserve on remote server

272 Views Asked by At

Condider that I've built my application as in te example below:

library(RestRserve)
app = Application$new()
app$add_get(
  path = "/hello", 
  FUN = function(request, response) {
    response$set_body("Hello from RestRserve")
  })
backend = BackendRserve$new()
backend$start(app, http_port = 8080)

With the last command, Rserve wakes up and correctly listens and answers to the request on port 8080. Now, I would like to put the commands above in a script on a remote server, launch it with Rscript, and make it listen forever. However, once I disconnect from ssh, it stops working. Am I doing something wrong? Notice that I installed only RestRserve, Rserve come as a dependency, but I did not change anything or customized any configuration file.

2

There are 2 best solutions below

0
On BEST ANSWER

I solved with the help of rexy.ai and karo: The correct script uses backend$start(app, http_port = 8080) (with no background option) but the deployment is made using nohup Rscript app.R &. This let the app run remotely and accept requests!

2
On

Looking at the source code I'd suggest adding the named parameter background=TRUE to backend$start(app, http_port = 8080). This parameter is FALSE by default (line 36), when TRUE Rserve will be started in a new detached R-session (line 93). (Btw, leaving out said parameter you can check that the disconnect kills your running R-session by keeping a second SSH-connection open and listing running processes with a filter: ps aux | grep bin/exec/R before and after the disconnect)

If using said parameter works you might also want to look into server restarts. From the looks of it I'd say RestRserve cannot actually handle that by itself and you might need a small service script.