On my local I am generating sitemaps with the help of sitemap_generator gem. I have set up everything and now when I run rake sitemap:generate it is generating sitemap. Now I am trying to schedule this process using whenever gem. My schedule.rb is as follows:
schedule.rb
every 1.minutes do
rake "sitemap:refresh"
end
I keep my rails server running but nothing seems to happen after 1 minute. I might be missing something here. Before I push it on production server I wanted to check it. Can someone please tell me what is the issue here?
Can you please run crontab -l and check if cronjob is scheduled.
You need to convert your schedule.rb to cron syntax.
Try
whenever --update-crontab
And then check crontab.
crontab -l
Whenever command
Related
I'm trying to execute a simple rake task using whenever gem but the code isn't being executed.
I already set the environment to development, I updated the cron using the whenever --update-crontab command and the rake task works well if I run the command on console. But, when I run the server the log file is not being generated.
I saw a question here too with the same problem but it was solved setting the environment to development, but didn't work out for me.
My rake task:
namespace :testando do
task :consulta => :environment do
produto = Produto.first
puts produto.nm_produto
end
end
My schedule.rb:
set :output, "#{path}/log/cron_log.log"
set :environment, 'development'
every 1.minute do
rake "testando:consulta"
end
I'm using rails 5.0.0.1 and I'm programing in Cloud9, so I think the OS is Ubuntu.
What's missing ?
Update:
I followed the instructions of the main answer in this topic Cron job not working in Whenever gem
And it worked! The task is running even with the server not being started (with "rails s" command).
please run crontab -l to see if you have updated the crontab successfully
I'm new to rails and want to run a batch file/schedule task daily once at midnight that checks which entries have expired. Every record in the table has a closing_date and after that time, such records must be inactive.(status active=false on DB). so basically it will run 2 SQL queries to fetch all records and then flag another field to inactive for records that are outdated.I'm working with Rails 5.
How should I go about this-gem(rufus,whatever,clockwork or any other gem) or simply some system tool for cronjob?I'm going to change my DB to PostgreSQL so will that impact? Any suggestions or sample code anyone can share to get an idea.
In brief: You can use whenever Gem for this purpose.
You need to create a rake task for where you will write your SQL.
Then schedule it as cron job using whenever
Detailed explanation
First step will be creating a rake task. You can do this using the console
rails g task my_namespace my_task1
This will create a file named my_namespace.rake in the lib/tasks folder with initial content like
namespace :my_namespace do
desc "TODO"
task task1: :environment do
# Your code will go here
end
end
You can check whether your task is running properly by running
rake my_namespace:task1
in the console.
Now you need to schedule the job using the whenever gem.
gem 'whenever', :require => false in Gemfile.
Run bundle install
run wheneverize command in terminal. This will create a schedule.rb file in config folder .
Add the following code to schedule your rake task:
every 1.day, at: '8:00 pm' do
rake "my_namespace:task1"
end
Run this command: whenever --update-crontab. In case your environment is development, use command: whenever --update-crontab --set environment='development' (see this question.
run crontab -l to check whether cron job has been added.
Could anyone give me a tip how to set up CRON jobs for a Rails app that's running on Amazon EC2 (Ubuntu)?
Trying to find some tutorials or tips how to make it work, but still without any success.
Thank you
Try creating a model method which execute a task for your crondjob. You can execute the script by using rails runner.
rails runner 'User.deliver_reminder_emails!'
The whenever gem can even do fancier stuff: https://github.com/javan/whenever
You should have a look at rake tasks. Check this, this and this.
Running them is as simple as
rake foo:bar
You can schedule the same in your crontab; You might need to setup the correct PATH variable in case you are managing gems locally using rvm and bundler.
This worked out to be rather tricky for me, as the ELB-EC2 instance was trying to run a cron job as root, which seemed to result in a different set of permissions, bundle, etc.
It's a bit hacky, but this worked:
sudo su ec2-user bin/bash -lc "cd /var/app/current && /opt/rubies/ruby-2.1.8/bin/bundle exec/bundle exec rails runner -e your_env \"Model.do_something\""
Basically, su to the user that usually runs rails, and explicitly state the paths for bundle.
I want run whenever gem and I have this task in schedule.rb file:
every 5.minutes do
rake "directory:cleanup"
end
I'm in environment development with Mongrel.
Why this task is not fired every 5 minutes?
What am I doing wrong?
PD: The task works fine from console.
You need to run the whenever --update-crontab command from the root directory of your application. This will add the task to your crontab.
However, if you're using OSX, there probably isn't a crontab to begin with so this won't work.
If you are constantly needing to cleanup some directories in development, I think there might be a better solution. Look into using something like Guard perhaps.
I have no problems with running it in development mode via rake jobs:work. However, I'm somehow unable to figure out how to use it in production. I'm using Capistrano for deployment.
Thanks for any advice!
If you install delayed_job as a gem you need to run the generator in order to create the script scripts/delayed_job and set run permissions.
Then you can follow the instructions on How to configure Capistrano for Delayed Job to hook it up in your Capistrano file.
See this answer. In a nutshell, use the Collective Idea fork of delayed_job. It contains a script called delayed_job that can be used.
You can run the generated delayed_job script as follows:
RAILS_ENV=production script/delayed_job start
Hope this helps
My first thought will be to add a after deploy task in capistrano to run the rake jobs:work task. you might need to check if the process is already running and restart it.
If you are running it via rake then couldn't you just run however often you wanted via cron? The whenever gem is a great interface to this from ruby.