我在R中使用Rook创建了一个服务器 - http://cran.r-project.org/web/packages/Rook Code如下
#!/usr/bin/Rscript
library(Rook)
s <- Rhttpd$new()
s$add(
name="pingpong",
app=Rook::URLMap$new(
'/ping' = function(env){
req <- Rook::Request$new(env)
res <- Rook::Response$new()
res$write(sprintf('<h1><a href="%s">Pong</a></h1>',req$to_url("/pong")))
res$finish()
},
'/pong' = function(env){
req <- Rook::Request$new(env)
res <- Rook::Response$new()
res$write(sprintf('<h1><a href="%s">Ping</a></h1>',req$to_url("/ping")))
res$finish()
},
'/?' = function(env){
req <- Rook::Request$new(env)
res <- Rook::Response$new()
res$redirect(req$to_url('/pong'))
res$finish()
}
)
)
## Not run:
s$start(port=9000)
$ ./Rook.r
Loading required package: tools
Loading required package: methods
Loading required package: brew
starting httpd help server ... done
Server started on host 127.0.0.1 and port 9000 . App urls are:
http://127.0.0.1:9000/custom/pingpong
Server started on 127.0.0.1:9000
[1] pingpong http://127.0.0.1:9000/custom/pingpong
Call browse() with an index number or name to run an application.
$
Run Code Online (Sandbox Code Playgroud)
这个过程就在这里结束.
它在R shell中运行良好,但后来我想在系统启动时将其作为服务器运行.因此,一旦调用了start,R就不应该退出,而是等待端口上的请求.我如何说服R只是等待或睡觉而不是退出?我可以使用R中的等待或睡眠功能等待N秒,但这并不完全适合该法案
这里有一个建议:
首先将您提供的示例拆分为(至少)两个文件:一个文件包含应用程序的定义,在您的示例中是函数app参数的值Rhttpd$add()。另一个文件是启动第一个文件中定义的应用程序的RScript 。
例如,如果应用程序函数的名称pingpong在名为 的文件中定义Rook.R,则Rscript可能类似于:
#!/usr/bin/Rscript --default-packages=methods,utils,stats,Rook
# This script takes as a single argument the port number on which to listen.
args <- commandArgs(trailingOnly=TRUE)
if (length(args) < 1) {
cat(paste("Usage:",
substring(grep("^--file=", commandArgs(), value=T), 8),
"<port-number>\n"))
quit(save="no", status=1)
} else if (length(args) > 1)
cat("Warning: extra arguments ignored\n")
s <- Rhttpd$new()
app <- RhttpdApp$new(name='pingpong', app='Rook.R')
s$add(app)
s$start(port=args[1], quiet=F)
suspend_console()
Run Code Online (Sandbox Code Playgroud)
如您所见,该脚本采用一个参数来指定侦听端口。现在,您可以创建一个 shell 脚本,该脚本将多次调用此Rscript,以启动侦听不同端口的服务器的多个实例,以便在响应 HTTP 请求时启用一定的并发性。
例如,如果上面的Rscript位于名为 then 的文件中,start.r这样的 shell 脚本可能类似于:
#!/bin/sh
if [ $# -lt 2 ]; then
echo "Usage: $0 <start-port> <instance-count>"
exit 1
fi
start_port=$1
instance_count=$2
end_port=$((start_port + instance_count - 1))
fifo=/tmp/`basename $0`$$
exit_command="echo $(basename $0) exiting; rm $fifo; kill \$(jobs -p)"
mkfifo $fifo
trap "$exit_command" INT TERM
cd `dirname $0`
for port in $(seq $start_port $end_port)
do ./start.r $port &
done
# block until interrupted
read < $fifo
Run Code Online (Sandbox Code Playgroud)
上面的 shell 脚本采用两个参数:(1) 要侦听的最低端口号和 (2) 要启动的实例数。例如,如果 shell 脚本位于名为start.shthen的可执行文件中
./start.sh 9000 3
Run Code Online (Sandbox Code Playgroud)
将启动Rook应用程序的三个实例,分别侦听端口 9000、9001 和 9002。
您会看到 shell 脚本的最后一行从 fifo 读取,这会阻止脚本退出,直到收到信号为止。当指定的信号之一被捕获时,shell 脚本会终止它在退出之前启动的所有Rook服务器进程。
现在,您可以配置反向代理以将传入请求转发到任何服务器实例。例如,如果您使用Nginx,您的配置可能类似于:
upstream rookapp {
server localhost:9000;
server localhost:9001;
server localhost:9002;
}
server {
listen your.ip.number.here:443;
location /pingpong/ {
proxy_pass http://rookapp/custom/pingpong/;
}
}
Run Code Online (Sandbox Code Playgroud)
然后您的服务就可以在公共互联网上使用。
最后一步是创建一个控制脚本,其中包含start(调用上述 shell 脚本)和stop(向其发送信号TERM以停止服务器)等选项。这样的脚本将处理一些事情,例如使 shell 脚本作为守护进程运行并跟踪其进程 ID 号。将此控制脚本安装在适当的位置,它将在计算机启动时启动Rook应用程序服务器。如何做到这一点取决于您的操作系统,您的问题中缺少操作系统的标识。
有关如何使用 shell 脚本中的 fifo 根据接收到的信号采取不同操作的示例,请参阅此堆栈溢出问题。
您将看到上面的示例 shell 脚本仅捕获陷阱INT和TERM信号。我选择这些是因为INT在终端键入 control-C 的结果,并且TERM是我的操作系统上的控制脚本用于停止服务的信号。您可能需要根据您的情况调整要捕获的信号的选择。