Cron jobs with whenever not working in production - ruby-on-rails

I'm trying to implement the gem "whenever" so that I can run automatic tasks every day.
This is my schedule.rb file:
env :PATH, ENV['PATH']
set :output, "log/cron_log.log"
every :day, at: '10:10 am' do
runner "Task.new.get_drivers"
end
every :day, at: '10:15 am' do
runner "Task.new.get_deliveries"
end
every :day, at: '10:20 am' do
runner "Task.new.update_deliveries"
end
In production, I ran this command line "whenever --update-crontab" as explained in the doc.
So far, everything seems to work fine. When I run the command line "whenever", I have this result:
# Begin Whenever generated tasks for: /home/total/site/config/schedule.rb
PATH=/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/total/.local/bin:/home/total/bin
10 10 * * * /bin/bash -l -c 'cd /home/total/site && bundle exec bin/rails runner -e production '\''Task.new.get_drivers'\'' >> log/cron_log.log 2>&1'
15 10 * * * /bin/bash -l -c 'cd /home/total/site && bundle exec bin/rails runner -e production '\''Task.new.get_deliveries'\'' >> log/cron_log.log 2>&1'
20 10 * * * /bin/bash -l -c 'cd /home/total/site && bundle exec bin/rails runner -e production '\''Task.new.update_deliveries'\'' >> log/cron_log.log 2>&1'
# End Whenever generated tasks for: /home/total/site/config/schedule.rb
But finally, nothing happens (except when I run myself the cron jobs in my console). And my logs display nothing. Have you got any idea of what could be the problem ?
Thank you for your help.
Best regards,
Virginie

Try crontab -l after deploy. This will open your crontab and list all tasks.
Then run whenever --update-crontab and check crontab -l again.
It could be that whenever --update-crontab didn't really update your crontab.
Also try to change :day to 1.day
Also check if your Capfile contains require 'whenever/capistrano', if you deploy via capistrano.

Related

Whenever Gem and cron Job

I followed the instruction on Whenever gems website, and tried following it in both development and on production mode on server.
set :output, "/file/to/path/cron.log"
every 1.day, at: '11:59 pm' do
command "sudo touch /file/to/path"
runner "Location.transfer_data",:environment => 'production'
end
It doesn't create the file(which is a test for now since I have no data on production).
I have spent hours trying to debug it and crontab -l gives me this output
59 23 * * * /bin/bash -l -c 'sudo touch /file/to/path >> /file/to/path/cron.log 2>&1'
59 23 * * * /bin/bash -l -c 'cd /var/www/name && script/rails runner -e production '\''Location.transfer_data'\'' >> /file/to/path/cron.log 2>&1'
Regards,
Babar Rehman
I solved the issue, don't know which one solved it in particular but these were the step that I took
Added job name to whenever --update-cron command
whenever --update-cron jobName
Restarted the cron service
sudo service cron restart
Gave full access rights to the log file
sudo chmod 777 /path/to/file.log
Hope it will come in handy for others

"whenever" gem is generating illegal crontab syntax

I'm using the whenever gem for my crontab scheduling. The syntax being generated is incorrect. Below you will notice it is adding these quotes and forward slashes'\''AddressGeocodeWorker.perform_async'\''
Incorrect crontab
0 4 * * * /bin/bash -l -c 'cd /var/lib/streetcred/agency/releases/streetcred && bin/rails runner -e production '\''AddressGeocodeWorker.perform_async'\'''
Correct crontab would look like this
0 4 * * * /bin/bash -l -c 'cd /var/lib/streetcred/agency/releases/streetcred && bin/rails runner -e production AddressGeocodeWorker.perform_async'
schedule.rb
every 1.day, :at => '4:00 am' do
runner "AddressGeocodeWorker.perform_async"
end
How would I get whenever to generate the correct crontab entry?

Whenever does not trigger Sidekiq job

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?
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.

Rails hourly cron jobs using Whenever fails to run hourly

On Redhat, using Whenever. My cron jobs fail to run hourly. I need help as to why.
Schedule.rb
every 1.hours do
rake "deathburrito:all", :environment => "development"
rake "bamboo:all", :environment => "development"
rake "jira:grab_data", :environment => "development"
end
Crontab -l
0 * * * * /bin/bash -l -c 'cd /var/www/qadashboard && RAILS_ENV=production bundle exec rake deathburrito:all --silent'
0 * * * * /bin/bash -l -c 'cd /var/www/qadashboard && RAILS_ENV=development bundle exec rake bamboo:all --silent'
0 * * * * /bin/bash -l -c 'cd /var/www/qadashboard && RAILS_ENV=development bundle exec rake jira:grab_data --silent'
Can anyone help me? I am not even sure what else I should be checking.
Add
MAILTO=your#email.com
to your crontab. Then enjoy the error reports from cron.
If that won't solve the issue, post the error report here.
bundle will have to be in that subshell's path. Try specifying a full-blown /usr/bin/bundle (or whatever it is).
Append log to config/schedule.rb
set :output, "/var/log/cron"
and create this file 'cron' in /var/log and give it write permission.
Execute
bundle exec whenever --update-crontab
sudo /etc/init.d/cron restart
To see the logs:
tail -f /var/log/cron
will give you more insight on the error.

cronjob with rails - whenever gem is doing nothing, why? (development mode)

I am trying to auto backup my database every night. I want to use the whenever gem and my application runs still in development environment.
i used wheneverize . command in my app folder to create the config/schedule.rb file, swhcih looks like this:
set :output, "/User/eveadmin/rails/feebacker/log/cron_log.log"
set :environment, "development"
every 1.day, :at => '2:30 am' do
dump_path = "/User/eveadmin/rails/db_backups/dump#{Date.today.to_s}"
command "sudo mysqldump -u user -p secretpasswort feedbacker_development > #{dump_path}.sql"
command "tar -zcvf #{dump_path}.tar.gz #{dump_path}.sql"
command "rm #{dump_path}.sql"
runner "Storage.store_dump '#{dump_path}.tar.gz'"
end
After deploying it to my server, also currently running in development mode, I used the command
whenever --update-crontab feedbacker --set environment=development
After this my crontab looks like the following:
# Begin Whenever generated tasks for: feedbacker
30 2 * * * /bin/bash -l -c 'sudo mysqldump -uroot -prootz feedbacker_development > /User/eveadmin/rails/db_backups/dump2011-07-07.sql >> /User/eveadmin/rails/feebacker/log/cron_log.log 2>&1'
30 2 * * * /bin/bash -l -c 'tar -zcvf /User/eveadmin/rails/db_backups/dump2011-07-07.tar.gz /User/eveadmin/rails/db_backups/dump2011-07-07.sql >> /User/eveadmin/rails/feebacker/log/cron_log.log 2>&1'
30 2 * * * /bin/bash -l -c 'rm /User/eveadmin/rails/db_backups/dump2011-07-07.sql >> /User/eveadmin/rails/feebacker/log/cron_log.log 2>&1'
30 2 * * * /bin/bash -l -c 'cd /Users/thomasmaximini/Sites/rails_projekte/feedbacker && script/rails runner -e development '\''Storage.store_dump '\''\'\'''\''/User/eveadmin/rails/db_backups/dump2011-07-07.tar.gz'\''\'\'''\'''\'' >> /User/eveadmin/rails/feebacker/log/cron_log.log 2>&1'
# End Whenever generated tasks for: feedbacker
It simply does nothing, no cron.log is generated, no backups are done, nothing. So I dont know what I am doing wrong as there are no errors raised to debug. I hope anybody can help, thanks.

Resources