Search code examples
ruby-on-rails-4sidekiq

Rails Sidekiq Worker Won't Execute


I am attempting to run a cron job on a rails site to execute a daily task in background. I am using Sidekiq to run the job.

It seems like everything is running correctly up to the point that it should execute the "perform" method in the worker, except that none of the code in this method executes. It is added to the queue on schedule and sidekiq logs that the task was run, but nothing happens. I know that the code in the perform method works because I have loaded it into the rails console and run the method there and it does exactly what is expected. It just doesn't do it when Sidekiq runs it.

Output from crontab -l

0 1 * * * /bin/bash -l -c 'cd /home/xxxx/yyy && environment_variable=production bundle exec sidekiq-client push ReviewWorker'

0,5,10,15,20,25,30,35,40,45,50,55 * * * * /bin/bash -l -c 'cd /home/xxxx/yyy && environment_variable=production bundle exec sidekiq-client push ReviewWorker'

Worker Code

class ReviewWorker
    include Sidekiq::Worker
    def perform
        containers = Container.where(display_listing: true, review_trigger_date: Time.zone.now.beginning_of_day..Time.zone.now.end_of_day)
        containers.each do |container|
            unless container.reviews.size > 0
                review = Review.new(container_id: container.id)
                review.save
                ReviewMailer.initial_review(container).deliver
            end
        end
        containers = Container.where(display_listing: true, review_trigger_date: 7.days.ago.beginning_of_day..7.days.ago.end_of_day)
        containers.each do |container|
            unless container.reviews.size > 1
                review = Review.new(container_id: container.id)
                review.save
                ReviewMailer.followup_review(container).deliver
            end
        end
        containers = Container.where(display_listing: true, review_trigger_date: 14.days.ago.beginning_of_day..14.days.ago.end_of_day)
        containers.update_all(display_listing: false)
    end
end

Sidekiq Log

2016-01-13T14:55:03.028Z 22748 TID-qvycc ReviewWorker JID-7d1ae22cd06a1e0515de4aef INFO: start
2016-01-13T14:55:03.270Z 22748 TID-qvycc ReviewWorker JID-7d1ae22cd06a1e0515de4aef INFO: done: 0.243 sec

Let me know if you need to see anything else to diagnose. I'm not getting any errors or retries. But it doesn't save the review and it doesn't send the mailer. Yet it does when I run it via console. And yes, I have reset the data so it should catch it and confirmed this via console as well.

Any help is greatly appreciated


Solution

  • The problem was with how sidekiq was running. I was running it on the server while testing by simply executing:

    bundle exec sidekiq
    

    Once I set it up on the server to run as a system service, it worked, though. Part of the command to set it up as a system service was to set the environment to production, so I suspect that that was the problem as the original command didn't do that.