Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plumber API works on server but not when I set up with systemd

Tags:

plumber

So I have an API that works fine locally as well as on the server if I run the plumber commands manually, by which I mean ssh-ing in the server and running :

r <- plumb("plumber.R")
r$run(port=8000, host = "0.0.0.0")

It looks like this:

#* @serializer contentType list(type="application/html")
#* @get /test
function(res){

  include_rmd("test.Rmd", res)

}

#* Echo the parameter that was sent in
#* @param msg The message to echo back.
#* @get /echo
function(msg=""){
  list(msg = paste0("The message is: '", msg, "'"))
}

They both work with no problem. But when I keep them alive on the server with systemd only the /echo one works. The other one just says "An exception occurred."

The systemd setup looks like this:

[Unit]
Description=Plumber API
# After=postgresql
# (or mariadb, mysql, etc if you use a DB with Plumber, otherwise leave this commented)

[Service]
ExecStart=/usr/bin/Rscript -e "api <- plumber::plumb('/home/chrisbeeley/api/plumber.R'); api$run(port=8000, host='0.0.0.0')"
Restart=on-abnormal
WorkingDirectory=/home/chrisbeeley/api/
[Install]
WantedBy=multi-user.target

I can't find error logs anywhere and I'm very confused as to why it should work when I run the commands on the server but not when I use systemd.

I'm using Ubuntu 16.04.

Since I posted this last night I've deployed the whole thing on a totally separate server which is also running 16.04 and it shows the exact same behaviour on there.

Edit: I've also tried this, based on code on the plumber documentation that returns a pdf, and that also returns "an exception occurred"

#* @serializer contentType list(type="text/html; charset=utf-8")
#* @get /html
function(){

  tmp <- tempfile()

  render("test_report.Rmd", tmp, output_format = "html_document")

  readBin(tmp, "raw", n=file.info(tmp)$size)
}
like image 689
Chris Beeley Avatar asked Nov 06 '22 19:11

Chris Beeley


1 Answers

Well, I never solved this. Instead I tried it with pm2, as detailed here https://www.rplumber.io/docs/hosting.html#pm2

I was a bit put off by the npm dependency, seemed like baggage, but it works like a charm.

So if anyone does Google this with a similar problem, I advise you to use pm2. It took me approximately 5 minutes to have it up and running :-)

I should add that although I haven't used them yet I gather pm2 will create log files, too, which sounds useful.

like image 183
Chris Beeley Avatar answered Nov 22 '22 05:11

Chris Beeley