我如何使用独角兽作为“rails s”?

时间:2013-04-07 04:33:24

标签: ruby-on-rails ruby unicorn

新的Rails项目Gemfile显示:

# Use unicorn as the app server
gem 'unicorn'

rails s --help显示:

Usage: rails server [mongrel, thin, etc] [options]

然而,做:

rails s unicorn

我明白了:

/Users/patrick/.rvm/gems/ruby-1.9.3-head@keynote/gems/rack-1.4.5/lib/rack/handler.rb:63:in `require': cannot load such file -- rack/handler/unicorn (LoadError)
from /Users/patrick/.rvm/gems/ruby-1.9.3-head@keynote/gems/rack-1.4.5/lib/rack/handler.rb:63:in `try_require'
from /Users/patrick/.rvm/gems/ruby-1.9.3-head@keynote/gems/rack-1.4.5/lib/rack/handler.rb:16:in `get'
from /Users/patrick/.rvm/gems/ruby-1.9.3-head@keynote/gems/rack-1.4.5/lib/rack/server.rb:272:in `server'
from /Users/patrick/.rvm/gems/ruby-1.9.3-head@keynote/gems/railties-3.2.13/lib/rails/commands/server.rb:59:in `start'
from /Users/patrick/.rvm/gems/ruby-1.9.3-head@keynote/gems/railties-3.2.13/lib/rails/commands.rb:55:in `block in <top (required)>'
from /Users/patrick/.rvm/gems/ruby-1.9.3-head@keynote/gems/railties-3.2.13/lib/rails/commands.rb:50:in `tap'
from /Users/patrick/.rvm/gems/ruby-1.9.3-head@keynote/gems/railties-3.2.13/lib/rails/commands.rb:50:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'

我过去曾在其他项目中使用过独角兽,但总是必须运行unicorn命令并指定一个有点痛苦的配置文件。我想知道如何通过rails s...

简单地使其工作

这可能吗?

5 个答案:

答案 0 :(得分:57)

看起来@Dogbert提到的unicorn-rails gem实际上可以用来使Unicorn成为rails server处理程序。

只需在gem "unicorn-rails"中添加gem "rack-handlers"(对于Rails 4.2.4,Gemfile),运行bundle install即可安装gem,然后就可以运行:

$ rails server unicorn

虽然安装了unicorn-rails,但Unicorn应该是默认的应用服务器,所以你也可以运行rails server并且它应该使用Unicorn(假设你的{还没有Thin或Mongrel) {1}},在这种情况下,它们可能会发生冲突,您可能希望删除您未使用的那些。

答案 1 :(得分:24)

更好的选择可能只是直接运行unicorn服务器。

bundle exec unicorn -p 3000 # default port is 8080

答案 2 :(得分:17)

gem 'rack-handlers'

rails server unicorn

答案 3 :(得分:1)

我认为不可能将独角兽用作'rails s'。使用此 -

将gem'unicorn'添加到gem文件并运行bundle install。

然后运行以下任何命令 -

$ unicorn -p 3000

$ unicorn_rails -p 3000

答案 4 :(得分:0)

然而,Steven的答案是最简单的方法。

我通过rake任务在开发环境上运行unicorn

LIB /任务/ dev_unicorn.rake:

task :server do
  # optional port parameter
  port = ENV['PORT'] ? ENV['PORT'] : '3000'
  puts 'start unicorn development'
  # execute unicorn command specifically in development
  # port at 3000 if unspecified
  sh "cd #{Rails.root} && RAILS_ENV=development unicorn -p #{port}"
end
# an alias task
task :s => :server

运行:

rake s

参考http://jing.io