问题描述
我试图在Web服务器上作为服务运行。运行 sudo Thin Install后,Thin在/etc/init.d/thin
I am trying to run thin as a service on my web server. After running "sudo thin install", thin created the following file in /etc/init.d/thin
#!/bin/sh
DAEMON=/usr/local/lib/ruby/gems/1.9.1/bin/thin
SCRIPT_NAME=/etc/init.d/thin
CONFIG_PATH=/etc/thin
# Exit if the package is not installed
[ -x "$DAEMON" ] || exit 0
case "$1" in
start)
$DAEMON start --all $CONFIG_PATH
;;
stop)
$DAEMON stop --all $CONFIG_PATH
;;
restart)
$DAEMON restart --all $CONFIG_PATH
;;
*)
echo "Usage: $SCRIPT_NAME {start|stop|restart}" >&2
exit 3
;;
esac
启动精简服务时,会运行以下命令:
When the thin service is started, the following is ran
thin start --all /etc/thin
这将扫描所有yaml配置文件,以定义如何为定义的每个应用程序运行瘦程序。
This will scan all of yaml config files defining how to run thin for each app defined. This does not work.
我在日志中看到:
/usr/local/lib/ruby/gems/1.9.1/gems/bundler-1.0.0/lib/bundler/runtime.rb:27:in `block in setup': You have already activated eventmachine 0.12.6, but your Gemfile requires eventmachine 0.12.11. Consider using bundle exec. (Gem::LoadError)
from /usr/local/lib/ruby/gems/1.9.1/gems/bundler-1.0.0/lib/bundler/spec_set.rb:12:in `block in each'
from /usr/local/lib/ruby/gems/1.9.1/gems/bundler-1.0.0/lib/bundler/spec_set.rb:12:in `each'
from /usr/local/lib/ruby/gems/1.9.1/gems/bundler-1.0.0/lib/bundler/spec_set.rb:12:in `each'
from /usr/local/lib/ruby/gems/1.9.1/gems/bundler-1.0.0/lib/bundler/runtime.rb:17:in `setup'
from /usr/local/lib/ruby/gems/1.9.1/gems/bundler-1.0.0/lib/bundler.rb:100:in `setup'
from /srv/app/current/config/boot.rb:8:in `<top (required)>'
from <internal:lib/rubygems/custom_require>:29:in `require'
from <internal:lib/rubygems/custom_require>:29:in `require'
from /srv/app/current/config/application.rb:1:in `<top (required)>'
from <internal:lib/rubygems/custom_require>:29:in `require'
from <internal:lib/rubygems/custom_require>:29:in `require'
from /srv/app/current/config/environment.rb:2:in `<top (required)>'
from <internal:lib/rubygems/custom_require>:29:in `require'
from <internal:lib/rubygems/custom_require>:29:in `require'
from /srv/app/current/config.ru:3:in `block in <main>'
from /usr/local/lib/ruby/gems/1.9.1/gems/rack-1.2.1/lib/rack/builder.rb:46:in `instance_eval'
from /usr/local/lib/ruby/gems/1.9.1/gems/rack-1.2.1/lib/rack/builder.rb:46:in `initialize'
from /srv/app/current/config.ru:1:in `new'
from /srv/app/current/config.ru:1:in `<main>'
from /usr/local/lib/ruby/gems/1.9.1/gems/thin-1.2.7/lib/rack/adapter/loader.rb:36:in `eval'
from /usr/local/lib/ruby/gems/1.9.1/gems/thin-1.2.7/lib/rack/adapter/loader.rb:36:in `load'
from /usr/local/lib/ruby/gems/1.9.1/gems/thin-1.2.7/lib/rack/adapter/loader.rb:45:in `for'
from /usr/local/lib/ruby/gems/1.9.1/gems/thin-1.2.7/lib/thin/controllers/controller.rb:163:in `load_adapter'
from /usr/local/lib/ruby/gems/1.9.1/gems/thin-1.2.7/lib/thin/controllers/controller.rb:67:in `start'
from /usr/local/lib/ruby/gems/1.9.1/gems/thin-1.2.7/lib/thin/runner.rb:177:in `run_command'
from /usr/local/lib/ruby/gems/1.9.1/gems/thin-1.2.7/lib/thin/runner.rb:143:in `run!'
from /usr/local/lib/ruby/gems/1.9.1/gems/thin-1.2.7/bin/thin:6:in `<top (required)>'
from /usr/local/lib/ruby/gems/1.9.1/bin/thin:19:in `load'
from /usr/local/lib/ruby/gems/1.9.1/bin/thin:19:in `<main>'
部署capistrano时,我将我的捆绑包缓存在$ APP_PATH / shared / bundle目录中;因此,这解释了为什么精简版抱怨没有安装gems,因为精简版服务不在$ APP_PATH / shared / bundle
When capistrano deploys, I am caching my bundle in in the $APP_PATH/shared/bundle directory; so, this explains why thin complains about gems not being installed as the thin service does not look in the $APP_PATH/shared/bundle
这行得通:
cd $APP_PATH/current; bundle exec thin start -d -C /etc/thin/app_x.yml
但这不是/etc/init.d/thin中的瘦服务文件的工作方式。我想我可以自己写。我只是不想解决已经解决的问题。
but that is not the way the thin service file in /etc/init.d/thin works. I guess I could write my own. I just don't want to solve a problem that has already been solved.
推荐答案
我想到了这个,但是我不知道感觉不到这是最好的解决方案,因为它没有利用Thin的 --all选项来从目录中读取配置文件。取而代之的是,我修改了启动/停止/重新启动瘦服务的文件,因此对于每个应用程序,我都给它指定了启动/停止/重新启动的特定命令。我确定可以改进此命令,但是现在它可以满足我的需要。
I came up with this, but I don't feel like it is the best solution as it does not take advantage of thin's "--all" option where it reads the config files from a directory. Instead I modified the file that starts/stops/restarts the thin service so for each app I give it a specific command for starting/stopping/restarting. I'm certain this command could be improved, but for now it works for my needs.
#!/bin/sh
# This is a pretty bad, but effective workaround for starting thin as a service per application.
DAEMON=/usr/local/lib/ruby/gems/1.9.1/bin/thin
# DAEMON=/usr/local/bin/bundler thin
SCRIPT_NAME=/etc/init.d/thin
CONFIG_PATH=/etc/thin
# Exit if the package is not installed
[ -x "$DAEMON" ] || exit 0
case "$1" in
start)
cd /srv/hub/current && bundle exec thin start -d -C /etc/thin/hub.yml
;;
stop)
cd /srv/hub/current && bundle exec thin stop -d -C /etc/thin/hub.yml
;;
restart)
cd /srv/hub/current && bundle exec thin restart -d -C /etc/thin/hub.yml
;;
*)
echo "Usage: $SCRIPT_NAME {start|stop|restart}" >&2
exit 3
;;
esac
这篇关于以Thin即服务运行Rails应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!