Sidekiq工作不会在rails应用程序上保留数据

时间:2014-05-27 20:36:30

标签: ruby-on-rails ruby jobs sidekiq upstart

我有以下设置,一个带nginx的ubuntu服务器 - passenger,postgres db,redis和sidekiq。

我已经设立了一个新的工作来关注sidekiq流程。

description "Sidekiq Background Worker"

# no "start on", we don't want to automatically start
stop on (stopping workers or runlevel [06])

# change to match your deployment user
setuid deploy
setgid deploy

respawn
respawn limit 3 30

# TERM and USR1 are sent by sidekiqctl when stopping sidekiq.  Without declaring these as normal exit codes, it just respawns.
normal exit 0 TERM USR1

script
# this script runs in /bin/sh by default
# respawn as bash so we can source in rbenv
exec /bin/bash <<EOT
  # use syslog for logging
  exec &> /dev/kmsg

  # pull in system rbenv
  export HOME=/home/deploy
  export RAILS_ENV=production
  source /home/deploy/.rvm/scripts/rvm

  cd /home/deploy/myapp/current
  bundle exec sidekiq -c 25 -L log/sidekiq.log -P tmp/pids/sidekiq.pid -q default -q payment -e production
EOT
end script

这实际上可以启动,停止并重启过程。我可以在sidekiq web http监视器上看到这个过程。但当我运行这些工人时,他们会得到处理。以下是工人:

class EventFinishedWorker
  include Sidekiq::Worker

  def perform(event_id)
    event = Event.find(event_id)
    score_a = event.team_A_score
    score_b = event.team_B_score
    event.bets.each do |bet|
      if (score_a == bet.team_a_score) && (score_b == bet.team_b_score)
        bet.guessed = true
        bet.save
      end
    end
  end

end

class PayPlayersBetsWorker
  include Sidekiq::Worker
  sidekiq_options :queue => :payment

  def perform(event_id)
    event = Event.find(event_id)
    bets = event.bets.where(guessed: true)
    total_winning_bets = bets.count
    unless total_winning_bets == 0
      pot = event.pot
      amount_to_pay = pot / total_winning_bets

       unless bets.empty?
          bets.each do |bet|
            account = bet.user.account
            user = User.find_by_id(bet.user.id)
            bet.payed = true
            bet.save
            account.wincoins += amount_to_pay
            account.accum_wincoins.increment(amount_to_pay)
            account.save
            user.set_score(user.current_score)
          end
       end
    end
  end

end


2014-05-27T18:48:34Z 5985 TID-7agy8 EventFinishedWorker JID-33ef5d7e7de51189d698c1e7 INFO: start
2014-05-27T18:48:34Z 5985 TID-5s4rg PayPlayersBetsWorker JID-e8473aa1bc59f0b958d23509 INFO: start
2014-05-27T18:48:34Z 5985 TID-5s4rg PayPlayersBetsWorker JID-e8473aa1bc59f0b958d23509 INFO: done: 0.07 sec
2014-05-27T18:48:35Z 5985 TID-7agy8 EventFinishedWorker JID-33ef5d7e7de51189d698c1e7 INFO: done: 0.112 sec

运行两个工作进程时没有错误,但是......

查询我的数据,EventFinishedWorker,完成它的工作。没有任何问题。第二份工作PayPlayersBetsWorker不起作用。但是当我进入控制台并做类似的事情时。

worker = PayPlayersBetsWorker.new

worker.perform(1)  

它可以完美地运行工作。我还检测到如果我直接从控制台运行sidekiq而不是使用upstart它也可以。

bundle exec sidekiq -P tmp/pids/sidekiq.pid -q default -q payment -e production
这是有效的。问题似乎是与upstart运行sidekiq。帮助将不胜感激:D

1 个答案:

答案 0 :(得分:0)

看起来你在两个工作之间有竞争条件。在PPBW查询之前,EFW尚未完成,没有找到任何内容并立即退出。