箱のプログラミング日記。

えんじにあ奮闘記

Capistranoのデプロイが急にコケる【delayed_job】

f:id:y_hakoiri:20191102121842j:plain

デプロイが急にコケるようになり、色々試行錯誤して解決したのでメモ。

結論から言うとdelayed_jobのpidが悪さをしていて、うまくworkerを再起動できないのが原因だった。

  • Ruby 2.4.3
  • Bundler 1.16.1
  • Rails 5.1.5
  • Capistrano 3.9.0
  • delayed_job 4.1.5

発生したエラー

いつも通りCapistranoでデプロイしていると、デプロイがコケた

Caused by:
SSHKit::Command::Failed: bundle exit status: 1
bundle stdout: bundler: failed to load command: bin/delayed_job (bin/delayed_job)
bundle stderr: Errno::EPERM: Operation not permitted
  /var/www/hako/shared/bundle/ruby/2.4.0/gems/daemons-1.3.1/lib/daemons/application.rb:383:in `kill'
  /var/www/hako/shared/bundle/ruby/2.4.0/gems/daemons-1.3.1/lib/daemons/application.rb:383:in `stop'
  /var/www/hako/shared/bundle/ruby/2.4.0/gems/daemons-1.3.1/lib/daemons/application_group.rb:181:in `block (2 levels) in stop_all'

この辺りのエラー文って何回も見たけど本質をついていないこと多いよなぁ。笑

とりあえずdelayed_jobでコケているのはわかった。そして権限がないっぽい?

config/deploy.rb

task :worker_start do
    on roles(:job) do
      with rails_env: fetch(:rails_env) do
        within release_path do
          execute :bundle, :exec, 'bin/delayed_job --queue=hoge -i first restart'
          execute :bundle, :exec, 'bin/delayed_job --queue=fuga-i second restart'
          execute :bundle, :exec, 'bin/delayed_job --queue=piyo-i third restart'
        end
      end
    end
  end

デプロイの過程でworkerを再起動しているのでそこで何かが起こっているようだけど、なんだろう。

プロセスの確認・workerの起動

delayed_jobで動かしているworker3つのうち最後の1つ(queue=piyo)でのみ失敗しているようだったので、サーバーに入ってプロセスを見てみる

$ ps aux | grep job
hako 15844  0.0  5.8 1328040 235992 ?      Sl   13:57   0:00 delayed_job.first
hako 15930  0.8  9.3 1471764 379404 ?      Sl   13:57   0:08 delayed_job.second

やっぱり3つめのプロセスが動いてないみたいだ。

$ cat ~/start.sh
#!/bin/sh
cd /var/www/hako/current
RAILS_ENV=staging bundle exec bin/delayed_job --queue=hoge -i first start
RAILS_ENV=staging bundle exec bin/delayed_job --queue=fuga -i second start
RAILS_ENV=staging bundle exec bin/delayed_job --queue=piyo -i third start

サーバーの起動時にworkerを起動するようにスクリプトを書いてあるけど、3つめだけうまく起動しなかったっぽい。

ということで手動で起動してみたものの

$ RAILS_ENV=staging bundle exec bin/delayed_job --queue=piyo -i third start
ERROR: there is already one or more instance(s) of the program running

もう起動されてまっせ、とのこと。んん??

ここですぐピンとくれば良かったんだけど、さっきのエラーで権限がウンタラカンタラとか出ていたので、色々回り道をしてしまった。

解決:pidを削除

だいたいこれなんだ。プロセスが起動していないのに「already running」の時はだいたいこれなんだ!!(自戒)

$ ls tmp/pids
delayed_job.first.pid  delayed_job.second.pid  delayed_job.third.pid  puma.state

起動していないのにpidが残っている。これがあるせいで「already running」になり起動ができないので、pidを削除すればいけるはず。

$ rm tmp/pids/delayed_job.third.pid
$ RAILS_ENV=staging bundle exec bin/delayed_job --queue=piyo -i third start
delayed_job.third: process with pid 28857 started.

いけた!ちゃんと起動した。

$ ps aux | grep job
hako 15844  0.0  5.8 1328040 235992 ?      Sl   13:57   0:00 delayed_job.first
hako 15930  0.8  9.3 1471764 379404 ?      Sl   13:57   0:08 delayed_job.second
hako 28857 41.5  8.7 1379856 353900 ?      Rl   14:13   0:04 delayed_job.third

これでデプロイしたら今後は通りました。

まとめ

  • Capistranoのデプロイがコケる原因は本当に色々あるので冷静に判断する
  • エラー文はあまり頼りにならないことも多いので総合的に判断する
  • まずはプロセスを確認
  • プロセスが立ち上がっていないのに「already running」の時はpidファイルの残存を疑う

参考

ruby on rails - DelayedJob ERROR: there is already one or more instance(s) of the program running - Stack Overflow