【发布时间】:2019-03-24 22:33:10
【问题描述】:
所以如果我手动运行管道工命令,我的 API 可以在本地和服务器上正常工作,我的意思是在服务器中 ssh 并运行:
r <- plumb("plumber.R")
r$run(port=8000, host = "0.0.0.0")
看起来像这样:
#* @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, "'"))
}
它们都可以正常工作。但是,当我使用 systemd 让它们在服务器上存活时,只有 /echo 可以工作。另一个只是说“发生了异常”。
systemd 设置如下所示:
[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
我在任何地方都找不到错误日志,我很困惑为什么当我在服务器上运行命令时它应该起作用,但在我使用 systemd 时却不起作用。
我使用的是 Ubuntu 16.04。
自从我昨晚发布这篇文章以来,我已经将整个东西部署在一个完全独立的服务器上,该服务器也运行 16.04,它在那里显示完全相同的行为。
编辑:我也试过这个,基于返回 pdf 的管道工文档上的代码,并且还返回“发生异常”
#* @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)
}
【问题讨论】:
-
您可以通过将 API 函数中的消息输出到文件来创建一些日志吗?您确定使用systemd启动时在R进程的工作目录中找到“test.Rmd”吗?这将是我的第一个嫌疑人。
-
这是一个很好的理论!我检查了文件是否可用 file.exists() 并且它是可用的。我也写了一些文本文件,看看它是否与写权限有关。也不是因为我可以 cat() 到目录中的文件。我认为这绝对与渲染有关。想不出要写什么到日志文件来帮助我诊断问题。谢谢,我会考虑的。
-
我不知道
include_rmd函数,但不管它做什么——你能把它的返回值输出到日志文件吗?如果您不返回include_rmd而是返回更简单的内容,例如将“test.Rmd”的第一行作为文本返回,会发生什么? -
它将文件呈现在 Markdown 中并以 HTML 格式返回响应。我不认为我可以将输出写入日志文件,但我可以使用 test
标签: plumber