4

I am trying to setup a Sidekiq scheduler to run recurring jobs.

I can get the Sidekiq job to run fine in Rails console, or Rails app. I can get Whenever to trigger command. However, I CANNOT get Whenever to trigger Sidekiq job.

I replicated my work in different machines, and in some machine, I get it to work. In most other machine, it is not working.

Here is my setup:

# app/workers/create_random_product.rb
class CreateRandomProduct
  include Sidekiq::Worker

  def perform
    new_product = Product.new
    new_product.name = "Product #{Time.now}"
    new_product.price = 5.5
    new_product.save
  end
end

# config/schedule.rb
every 1.minute do
  runner "CreateRandomProduct.perform_async"
  command "echo 'hello' >> /home/vagrant/output.txt"
end

As you can see in the schedule.rb, I can get the second command to run because I can see the output updated, but the first runner command does not seem to do anything. Sidekiq dashboard does not show any activity either.

Running "whenever" returns these cron jobs:

* * * * * /bin/bash -l -c 'cd /home/vagrant/sidekiqdemo && bin/rails runner -e production '\''CreateRandomProduct.perform_async'\'''

* * * * * /bin/bash -l -c 'echo '\''hello'\'' >> /home/vagrant/output.txt'

I ran the first command manually and it DOES trigger the Sidekiq job.

I did run the "whenever -i" command to update the crontab file. I check the crontab log, and I can see that it tried to trigger the Sidekiq job:

Oct 16 16:15:01 vagrant-ubuntu-trusty-64 CRON[13062]: (vagrant) CMD (/bin/bash -l -c 'cd /home/vagrant/sidekiqdemo && bin/rails runner -e production '\''CreateRandomProduct.perform_async'\''')
Oct 16 16:15:01 vagrant-ubuntu-trusty-64 CRON[13063]: (vagrant) CMD (/bin/bash -l -c 'echo '\''hello'\'' >> /home/vagrant/output.txt')

Is there anything that I am missing?

Khanetor
  • 11,595
  • 8
  • 40
  • 76
  • any solution to this problem? – knowbody Mar 03 '15 at 11:57
  • I haven't found one. I ended up using Sidetiq gem instead. It is not a perfect solution, but what it does is to have a job reschedule itself. – Khanetor Mar 03 '15 at 15:16
  • thanks @Khanetor, I actually did the same thing. will try to debug it and get back here – knowbody Mar 03 '15 at 15:34
  • I just run successfully your code, one thing to get it working was update crontab by cleaning it: `crontab -r` and then `whenever --update-crontab --set environment='development'` to test on localhost. – luzny Jun 15 '16 at 08:28

1 Answers1

1

I solved this defining a new job_type as suggested here. To let this work you have to install sidekiq-client-cli as well.

Moreover I discovered thanks to this post that when a command is executed from the cron process only a set of the environment variables are set so in my case I the command was not working because my BUNDLE_PATH wasn't set. at the end my working job_type is something like this:

job_type :sidekiq, "cd :path && BUNDLE_PATH=/bundle /usr/local/bin/bundle exec sidekiq-client :task :output"

every 1.minute do
  sidekiq "push GenerateExportsWorker"
end

this is the reason why in my case a command like

every 1.minute do
  command "echo 'you can use raw cron syntax too' >> /home/log/myscript.log 2>&1 "
end

despite the other.

TWONEKSONE
  • 3,918
  • 3
  • 19
  • 26