How should we use whenever gem in rails 3 for cronjob processing? i have one rake task which i need to run after every 5 min ,how can i do this?
Go through below link which explains how to configure and use.
https://github.com/javan/whenever
http://railscasts.com/episodes/164-cron-in-ruby
As per your question below is the code that runs rake task every 5 minutes. This should be in config/schedule.rb file
every 5.minutes do
rake "your_task" or "namespace:your_task"
end
Related
There's a straight forward explanation of why cron jobs and tasked scheduled with whenever gem won't work on heroku
Is there any way to schedule a rake task to run more frequently than every 10 minutes (the minimum frequency heroku scheduler offers), for example every 1 minute?
You can use a combination of Clockwork gem and Heroku Procfile.
in your lib/clock.rb, you can do something like:
every(1.minute, 'Run task') do
Rake::Task['namespace:task'].invoke
end
then add clock: bundle exec clockwork lib/clock.rb to your Heroku Procfile
Clockwork supports a lot of time variations.
I think #ollaollu's method is probably superior, but for future reference what worked for me was to use a Procfile to run an infinite loop on boot, and that effectively runs a process every 5 seconds and even works after heroku restart.
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
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.
I'm working with delayed job for active record gem https://github.com/collectiveidea/delayed_job I'm trying to set up a job to run five minutes after an event occurs in my application. After five minutes passes, I need to make some database updates. I've tried rake jobs:work and RAILS_ENV=development script/delayed_job start. Prior to this all, I have run bundle install, rails generate delayed_job:active_record, and rake db:migrate. I have a lottery website that needs to check winners every five minutes and update tokens for winning players.
I wait five minutes, but no updates are made in my local application.
Here's what I have so far:
Gem File:
gem 'delayed_job_active_record'
gem "daemons"
Job (located in lib)
class WinnersJob < Struct.new(:blast_id)
def perform
...
end
Controller
require 'winners'
Delayed::Job.enqueue(WinnersJob.new(blast.id), 1, 5.minutes.from_now)
end
I think you have to launch the background workers locally using foreman. The following worked on my Mac.
From Heroku docs:
You then need to tell your application to process jobs put into your job queue, you can do that by adding this to your Procfile:
worker: bundle exec rake jobs:work
Now when you start your application using Foreman it will start processing your job queue.
foreman start
Having said all that, unless you are deploying on a Mac, it doesn't really matter if they run locally. (I noticed this after I got it working.) It only matters if it works on your servers. If you are deploying on Heroku, then Delayed Job works well.
Reference:
https://devcenter.heroku.com/articles/delayed-job
https://devcenter.heroku.com/articles/procfile
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.