Ruby on Rails - How to set cron on local - ruby-on-rails

Suppose, we have a rake task, to be run like,
rake RAILS_ENV=development parse_and_insert_feed_in_db
We can set this task to be execute repeatedly on server using crontab's.
1 0 * * * rake RAILS_ENV=development parse_and_insert_feed_in_db > /some/dir/file.log
How can we set a rake task to be executed on local machine like we set cron on server?

Please try like this:
1 0 * * * /bin/bash -l -c 'cd /home/your/Application/path && rake RAILS_ENV=development parse_and_insert_feed_in_db > /some/dir/file.log'

Related

Blazer gem: Scheduled rake tasks not working automatically but work fine when invoked from console

I have set up a couple of rake tasks to run automatically with the whenever gem . The tasks involve running a background Job, performing a database Blazer check and sending failing checks emails.
The last two are provided by the Blazer gem.
This is my schedule.rb file:
set :environment, "development"
every 1.minutes do
rake "health_check:perform"
end
every 1.minutes do
rake 'blazer:run_checks'
end
every 2.minutes do
rake "blazer:send_failing_checks"
end
The "health_check:perform" rake task always performs as expected, and I can see the Job being run in my server log every minute.
The other two, however, don't seem to be executing at all.
I can tell blazer:run_checks is not executing because when a Blazer check is run either manually or automatically, the blazer_checks table last_run_at column value is updated with the timestamp when the last check was run. Also, I can see the update DB transaction in my server log when that happens.
On the other hand "blazer:send_failing_checks" is not working either because well, no emails are ever delivered.
However, if I manually invoke the task from my console via rake "blazer:run_checks" or rake "blazer:send_failing_checks" they perform normally (blazer_checks table is updated and failing check emails are delivered).
I can't spot any error messages in my server log, altough I believe by default rake tasks are set to run in silent mode? Since running whenever -l to list my crontab file returns something like:
* * * * * /bin/bash -l -c 'cd /home/user/code/myapp && RAILS_ENV=development bundle exec rake health_check:perform --silent'
* * * * * /bin/bash -l -c 'cd /home/user/code/myapp && RAILS_ENV=development bundle exec rake blazer:run_checks --silent'
0,2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,48,50,52,54,56,58 * * * * /bin/bash -l -c 'cd /home/user/code/myapp && RAILS_ENV=development bundle exec rake blazer:send_failing_checks --silent'
Any ideas? How can I debug this?

Rails migrate cronjobs to whenever

On server we have the following cronfile:
MAILTO=admin_email#project.com
* * * * * /bin/bash -l -c 'cd /path/to/project/folder;RAILS_ENV=production bundle exec rake some_rake_task'
0 */2 * * * /bin/bash -l -c 'cd /path/to/project/folder;RAILS_ENV=production bundle exec rake another_rake_task'
# and so on
I had no problems with moving it to whenever:
config/schedule.rb:
every '* * * * *' do
rake 'some_rake_task', output: 'log/cron.log'
end
every '0 */2 * * *' do
rake 'another_rake_task', output: 'log/cron.log'
end
# and so on
The question is how do I implement MAIL_TO option in whenever, so that when anything goes wrong admin is getting notified?
Thanks!
If I were you, I would set up error notifications with Airbrake or Rollbar which can both be configured to notify of errors within Rake tasks. That way you'll get notified automatically when something goes wrong in your CRON jobs.

How to run a rake task regularly with using CRON?

I've written a rake task for my Rails application and I'd need to run this tasks regularly with using CRON.
If I have a URL that I need to ping with CRON, I do it like this:
0 */6 * * * curl https://www.website.com/something
But how to "ping" a rake task?
The application is located in /home/deployer/apps/myapp-production/current and is running on DigitalOceal (Ubuntu server - nginx).
Thanks.
EDIT: This is my command:
0 */6 * * * cd /home/deployer/apps/myapp-production/current && RAILS_ENV=production bundle exec rake db:backup
But the output is:
/bin/sh: 1: bundle: not found
When I run just rake db:backup on my laptop (locally), everything works just well.
Do I have incorrect the path in the CRON task?
EDIT2: When I run the command cd /home/deployer/apps/myapp-production/current && RAILS_ENV=production bundle exec rake db:backup manually from the command line, everything is working, but not from the CRON.
Use whenever gem. It provides nice DSL like:
every :day, :at => '12:20am', :roles => [:app] do
rake "app_server:task"
end

Backup a Ruby in Rails app (Gitlab) with Cron job

I tried ti backup my gitlab project (it's based on ROR) with a bash script triggered with a CRON job.
The bash script is ok except the rake migrations wiche returns an error:
gitlabBackUp.sh: 12:
/home/backup/scripts/gitlabBackUp.sh: bundle: not found
Here is the way i did it in my gitlabBackUp.sh
# Export the data
bundle exec rake gitlab:backup:create --trace RAILS_ENV=production
I tried without the bundle exec but it returns
/home/backup/scripts/gitlabBackUp.sh: 14:
/home/backup/scripts/gitlabBackUp.sh: rake: not found
Any tips?
EDIT:
I finally get the cron task get working with:
/usr/local/bin/bundle exec /usr/local/bin/rake gitlab:backup:create --trace RAILS_ENV=production
PATH is probably not well specified in cron. You can:
1) Call (exec) bash with -l e.g. exec bash -l /path/to/real-script.sh
Or perhaps directly in cron like 0 16 * * * /bin/bash -l '/home/backup/scripts/gitlabBackUp.sh'
2) Explicitly specify path for bundle and rake e.g. /usr/local/bin/bundle exec /usr/binrake gitlab:backup:create --trace RAILS_ENV=production
You can always know the location of bundle and rake through which bundle and which rake; or type -P bundle and type -P rake.

rake ts:rebuild doesn't work as a crontab task

0 0,2,4,6,8,10,12,14,16,18,20,22 * * * /bin/bash -l -c 'cd /var/www/rails/xxx/releases/20110105175853 && RAILS_ENV=production rake ts:rebuild --silent'
hi, guys. this is a crontab task generated by whenever. it's rebuilding the sphinx index.
it doesn't work when it run as a crontab task, with no error in the /var/log/cron log. but it works when I run the command manually.
anybody can help? thank you very much.
Your cron task looks alittle bizarre to me. Not sure that you want to be calling ts:rebuild all the time, you only need to rebuild if your server gets rebooted, to update the index you just run ts:index, below is the cron task I use for my rails app, it refreshes the sphinx index every 5 minutes.
if your using the user crontab this should work:
*/5 * * * * cd /home/appuser/rails-app; RAILS_ENV=production rake ts:index >> /dev/null
if your putting your crons in /etc/cron.d/ you will need to add the username, like this:
*/5 * * * * appuser cd /home/appuser/rails-app; RAILS_ENV=production rake ts:index >> /dev/null
These settings are for an Ubuntu box, but should work with most linux distros.
Hope this helps.

Resources