Whenever does not trigger Sidekiq job - ruby-on-rails

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.

Related

cron jobs not working aws whenever gem rails

I know this is a very common question but I feel like I need a specific answer to help find out where I am going wrong...
Loaded whenever gem to manage cron jobs - it works fine in development but when I loaded the app into AWS I can't seem to it work...
When I SSH into the instance I can run crontab -l and it lists the whenever tasks but it just doesn't seem to actually execute them. I also cant find any log files to read into why it's not firing.
This is what I pulled from the eb activity log..
+++ GEM_ROOT=/opt/rubies/ruby-2.3.6/lib/ruby/gems/2.3.0
++ (( 0 != 0 ))
+ cd /var/app/current
+ su -c 'bundle exec whenever --update-cron'
[write] crontab file updated
+ su -c 'crontab -l'
# Begin Whenever generated tasks for:
/var/app/current/config/schedule.rb at: 2018-01-10 06:08:24 +0000
0 * * * * /bin/bash -l -c 'cd /var/app/current && bundle exec
bin/rails runner -e production '\''Trendi.home'\'' >>
app/views/pages/cron.html.erb 2>&1'
# End Whenever generated tasks for:
/var/app/current/config/schedule.rb at: 2018-01-10 06:08:24 +0000
[2018-01-10T06:08:24.705Z] INFO [15603] - [Application update app-
a3a0-180109_230627#16/AppDeployStage1/AppDeployPostHook] : Completed
activity. Result:
Successfully execute hooks in directory
/opt/elasticbeanstalk/hooks/appdeploy/post.
This is my config file from ebextensions folder
files:
"/opt/elasticbeanstalk/hooks/appdeploy/post/01_cron.sh":
mode: "000755"
owner: root
group: root
content: |
#!/usr/bin/env bash
# Using similar syntax as the appdeploy pre hooks that is
managed by AWS
set -xe
EB_SCRIPT_DIR=$(/opt/elasticbeanstalk/bin/get-config container
-k script_dir)
EB_SUPPORT_DIR=$(/opt/elasticbeanstalk/bin/get-config container
-k support_dir)
EB_DEPLOY_DIR=$(/opt/elasticbeanstalk/bin/get-config container
-k app_deploy_dir)
. $EB_SUPPORT_DIR/envvars
. $EB_SCRIPT_DIR/use-app-ruby.sh
cd $EB_DEPLOY_DIR
su -c "bundle exec whenever --update-cron"
su -c "crontab -l"
My Schedule.rb
ENV['RAILS_ENV'] = "production"
set :output, "app/views/pages/cron.html.erb"
every 1.hour, at: ":00"do # 1.minute 1.day 1.week 1.month 1.year
is also supported
runner "Trendi.home", :environment => 'production'
end
And my task that is stored at /lib/
module Trendi
def self.home
#exectuted task code here
end
You could try a simpler command in your crontab and change it to fire every minute. E.g.
/bin/echo "test" >> /home/username/testcron.log
That way you can quickly rule out if it's the cronjob that's the culprit. Keep in mind to use full paths to each command. So in your case you might want to change the "bundle" command to use the full path. You can find the path with the "which" command.
Also, are you sure you are correctly escaping here?
-e production '\''Trendi.home'\''
Wouldn't this be more adequate?
-e production "Trendi.home"

Cron jobs with whenever not working in production

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.

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

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