无法在生产环境中 运行 sidekiq(Procfile + systemd)

Unable to run sidekiq in production (Procfile + systemd)

我正在使用 ForemanCapistrano 来部署我的 rails 应用程序。 web(即 Puma)进程正确启动,但我无法将我的 worker(即 sidekiq)作为 production环境。

我调整了 SystemD 导出中生成的 app-worker-1.service 文件,同时试图强制正确设置环境,但没有成功:

[Unit]
StopWhenUnneeded=true

[Service]
User=deployment_user
WorkingDirectory=/var/www/app_name/current
ExecStart=/bin/bash -lc "RAILS_ENV=production bundle exec sidekiq -e production"

RestartSec=1
Restart=on-failure

StandardOutput=journal
StandardError=journal

SyslogIdentifier=%n

KillMode=process

我尝试了一切...我添加了 Logger.debug 语句,显示 RACK_ENVRAILS_ENV 都错误地设置为 development

这是我从流程日志中得到的错误:

I, [2015-09-19T13:56:14.813528 #31187]  INFO -- : Celluloid 0.17.1.2 is running in BACKPORTED mode. [ http://git.io/vJf3J ]
D, [2015-09-19T13:56:16.765338 #31187] DEBUG -- : RACK_ENV=development
D, [2015-09-19T13:56:16.766057 #31187] DEBUG -- : RAILS_ENV=development
'development' database is not configured. Available: ["production"]
/var/www/app_name/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.4/lib/active_record/connection_adapters/connection_specification.rb:248:in `resolve_symbol_connection'
/var/www/app_name/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.4/lib/active_record/connection_adapters/connection_specification.rb:211:in `resolve_connection'
/var/www/app_name/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.4/lib/active_record/connection_adapters/connection_specification.rb:139:in `resolve'
/var/www/app_name/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.4/lib/active_record/connection_adapters/connection_specification.rb:169:in `spec'
/var/www/app_name/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.4/lib/active_record/connection_handling.rb:50:in `establish_connection'
/var/www/app_name/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.4/lib/active_record/railtie.rb:120:in `block (2 levels) in <class:Railtie>'
/var/www/app_name/shared/bundle/ruby/2.2.0/gems/activesupport-4.2.4/lib/active_support/lazy_load_hooks.rb:38:in `instance_eval'
/var/www/app_name/shared/bundle/ruby/2.2.0/gems/activesupport-4.2.4/lib/active_support/lazy_load_hooks.rb:38:in `execute_hook'
/var/www/app_name/shared/bundle/ruby/2.2.0/gems/activesupport-4.2.4/lib/active_support/lazy_load_hooks.rb:28:in `block in on_load'
/var/www/app_name/shared/bundle/ruby/2.2.0/gems/activesupport-4.2.4/lib/active_support/lazy_load_hooks.rb:27:in `each'
/var/www/app_name/shared/bundle/ruby/2.2.0/gems/activesupport-4.2.4/lib/active_support/lazy_load_hooks.rb:27:in `on_load'
/var/www/app_name/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.4/lib/active_record/railtie.rb:116:in `block in <class:Railtie>'
/var/www/app_name/shared/bundle/ruby/2.2.0/gems/railties-4.2.4/lib/rails/initializable.rb:30:in `instance_exec'
/var/www/app_name/shared/bundle/ruby/2.2.0/gems/railties-4.2.4/lib/rails/initializable.rb:30:in `run'
/var/www/app_name/shared/bundle/ruby/2.2.0/gems/railties-4.2.4/lib/rails/initializable.rb:55:in `block in run_initializers'
/usr/local/rvm/rubies/ruby-2.2.3/lib/ruby/2.2.0/tsort.rb:226:in `block in tsort_each'
/usr/local/rvm/rubies/ruby-2.2.3/lib/ruby/2.2.0/tsort.rb:348:in `block (2 levels) in each_strongly_connected_component'
/usr/local/rvm/rubies/ruby-2.2.3/lib/ruby/2.2.0/tsort.rb:429:in `each_strongly_connected_component_from'
/usr/local/rvm/rubies/ruby-2.2.3/lib/ruby/2.2.0/tsort.rb:347:in `block in each_strongly_connected_component'
/usr/local/rvm/rubies/ruby-2.2.3/lib/ruby/2.2.0/tsort.rb:345:in `each'
/usr/local/rvm/rubies/ruby-2.2.3/lib/ruby/2.2.0/tsort.rb:345:in `call'
/usr/local/rvm/rubies/ruby-2.2.3/lib/ruby/2.2.0/tsort.rb:345:in `each_strongly_connected_component'
/usr/local/rvm/rubies/ruby-2.2.3/lib/ruby/2.2.0/tsort.rb:224:in `tsort_each'
/usr/local/rvm/rubies/ruby-2.2.3/lib/ruby/2.2.0/tsort.rb:203:in `tsort_each'
/var/www/app_name/shared/bundle/ruby/2.2.0/gems/railties-4.2.4/lib/rails/initializable.rb:54:in `run_initializers'
/var/www/app_name/shared/bundle/ruby/2.2.0/gems/railties-4.2.4/lib/rails/application.rb:352:in `initialize!'
/var/www/app_name/shared/bundle/ruby/2.2.0/gems/railties-4.2.4/lib/rails/railtie.rb:194:in `public_send'
/var/www/app_name/shared/bundle/ruby/2.2.0/gems/railties-4.2.4/lib/rails/railtie.rb:194:in `method_missing'
/var/www/app_name/releases/20150918131757/config/environment.rb:12:in `<top (required)>'
...

Sidekiq 提供了一个示例 systemd 单元文件,可能会有帮助。它显示了如何使用 -e:

正确设置环境

https://github.com/mperham/sidekiq/blob/master/examples/systemd/sidekiq.service

ExecStart=/usr/local/bin/bundler exec sidekiq -e production

找到了!

首先我在 SystemD 上启用了调试:

Environment=SYSTEMD_LOG_LEVEL=debug

然后在查看我的服务日志时,我的注意力被吸引到一条消息,该消息起初看起来无关紧要,提示我查看服务状态本身...

capistrano@droplet2:/lib/systemd/system$ systemctl status app_name-worker-1.service
● alagarderie-web-1.service
   Loaded: error (Reason: Invalid argument)
   Active: inactive (dead) since Sat 2015-09-19 20:25:14 EDT; 35min ago
 Main PID: 32292 (code=exited, status=0/SUCCESS)

...
... the first line are not important ... 
...
Warning: alagarderie-web-1.service changed on disk. Run 'systemctl daemon-reload' to reload units.

警告,什么!我知道当我发现问题时我会讨厌自己...

SystemD 脚本文件的更改不会自动同步。我重新加载了我的应用程序目标,然后启动了它;瞧瞧!