Search code examples
ruby-on-railsrubyjobssidekiqupstart

Sidekiq jobs not persisting data on a rails app


I have the following setup, an ubuntu server with nginx - passenger, postgres db, redis and sidekiq.

i have set up an upstart job to keep an eye on the sidekiq process.

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

This actually work i can start, stop and restart the process. i can see the process over at the sidekiq web http monitor. but when i run this workers workers they get processed. Here are both workers:

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

and

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

I get no errors running both workers processes, but ...

cheking my data, EventFinishedWorker, does its job. no problems whatsoever. the second job PayPlayersBetsWorker doesnt work. but when i go into the console and do something like.

worker = PayPlayersBetsWorker.new

worker.perform(1)  

it works it runs the jobs flawlessly. i have detected also that if i run sidekiq from the console directly not using upstart it works too.

bundle exec sidekiq -P tmp/pids/sidekiq.pid -q default -q payment -e production

this works. the problem seems to be running sidekiq with upstart. help would be appreciated :D


Solution

  • It looks like you have a race condition between the two jobs. EFW isn't finished before PPBW queries, doesn't find anything and immediately exits.