5

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)
}
Chris Beeley
  • 591
  • 6
  • 22
  • Can you create some logs by outputting messages in your API functions to a file? Are you sure "test.Rmd" is found in the working directory of the R process when started with systemd? This would be my first suspect. – Ott Toomet Mar 25 '19 at 23:03
  • That's a good theory! I checked to see if the file was available with file.exists() and it is. I've written some text files as well to see if it's to do with write permissions. And it's not that either because I can cat() to a file in the directory. I think it's definitely something to do with render. Can't think what to write to a log file that will help me to diagnose the problem. Thanks, I'll think on it. – Chris Beeley Mar 26 '19 at 20:45
  • I don't know the `include_rmd` function, but whatever it does--can you output it's return value to a log file? What happens if you don't return `include_rmd` but something simpler, say the first line of "test.Rmd" as text? – Ott Toomet Mar 27 '19 at 03:11
  • It renders the file in markdown and returns the response as HTML. I don't think I can write the output to a logfile but I can write the contents back to the server using test <- readLines("test.Rmd") and then cat(test, file = "temp.txt") and this works okay – Chris Beeley Mar 28 '19 at 11:15

1 Answers1

1

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.

Chris Beeley
  • 591
  • 6
  • 22