How to schedule whenever task based on a table column? - ruby-on-rails

I am using whenever gem to run rake tasks. My code in schedule.rb is as follow.
every 1.day, :at => '11:00 am' do
rake "notifications:run_mailer"
end
Above code is executing the rake task on 11am of every day. Now i want to change this. I want to run this rake task based on a table column. For that i have created a table called scheduler.rb and a column run_at. based on run_at column date and time i need to run that rake task. How to do that?

Step 1:
First we have to load all the files in the schedule.rb Then only we can execute the ActiveRecord queries.
That could be done by the below command
require File.expand_path(File.dirname(__FILE__) + "/environment")
Step 2:
Execute the ActiveRecord query and get the time.
run_at=Scheduler.last.run_at
time=[run_at.hour.to_s,run_at.min.to_s].join(":")
every 1.day, :at => time do
rake "notifications:run_mailer"
end
step 3:
We have to update the whenever crontab using below command.
system "whenever --update-crontab"
system keyword is used to run linux terminal commands inside rails code.

Assuming the table name is Scheduler:
Scheduler.find(:all).each { |scheduler|
every 1.day, :at => scheduler.run_at do
rake scheduler.task_to_run
end
}
Please note, that this code will add cron tasks on every run. This will lead to enormously huge amount of cronjobs after a while. The additional check for “was the task already scheduled” must be added as well.
Hope it helps.

Related

'Whenever' gem Scheduler doesn't run tasks

I'm seeking help concerning the whenever gem. Here my case:
I have the task I generated and that works when I run it through command line as such rake dashboard_data_t:collect.
namespace :dashboard_data_t do
desc "TODO"
task collect: :environment do
#task...
end
end
I then followed the documentation provided here in such a way that my config/schedule.rb looks like so:
# config/schedule.rb
every :day, at: '10:43 am' do
rake "dashboard_data_t:collect"
end
Happily done with that, I thought it would go on and run itself alone, without me needing to do anything more. But I noticed it didn't. I thought it might come from my task so I created an other one, this time way more simple than the 1st. Its purpose was solely to experiment and find what was going wrong:
namespace :test_name do
desc "TODO"
task test_task: :environment do
sh('echo', 'test task runned successfully')
end
end
I then added the following to my config/schedule.rb:
# config/schedule.rb
every 1.minutes do
rake "test_name:test_task"
end
Once again, the task didn't execute (periodically), but was still working manually.
I noticed by running the crontab -e command that RAILS_ENV was set to production, I understood why my dashboard_data_t:collect task wasn't working, because it relied on the development db. So I did the following:
# config/schedule.rb
set :environment, 'development'
Unfortunately, this didn't change anything as both tasks still don't execute. Now I'm stuck here with no ideas whatsoever. Can anyone help me.
Cheers.

how to use Whenever gem properly

I am just trying to create a record every 5 minutes using whenever gem but it doesn't create anything on the production.
config/schedule.rb
every 5.minutes do
runner "Post.new_post"
end
lib/tasks/post.rb
class Post
def new_post
Post.create(title: "cron testing")
end
end
and I ran whenever command then pushed to the live server but It doesn't create any records on the production.
Which steps did I miss?
I'm assuming that you're using rails, so — files placed in lib/tasks are loaded as custom rake tasks by default, you can check it here.
Brief instruction:
Run rails generate task posts create_post
Modify lib/tasks/posts.rake
Update config/schedule.rb from
runner "Post.new_post" to rake 'posts:create_post'
Run whenever --update-crontab somehow on the server ;)

A cron job that creates objects - rails

I am working on an event app, that showcases the events near my area.
A Boolean is_weekly_event is set for each entry.
If it is set to true I need to show that event in my app every week on the same data and time.
Therefore, I need to write a cron job, that re-creates this event every time it passes it's end_time, for the same time next week!
Thanks in advance!
you can use whenever gem to add cron job. I have used this in one of my projects and works very fine.
You can define rake tasks in lib/ and in your schedule.rb file which will be generated when you will do whenervize your project.
every 3.hours do # 1.minute 1.day 1.week 1.month 1.year is also supported
# your task here
end
every 1.day, :at => '4:30 am' do
# your task here
end
After finishing all you need is to update your crontab file for your machine using this command.
$ whenever --update-crontab
Note - just a take care to load proper environment(dev, prod) for the rake tasks, it took me hard to find out when i was trying to run the cron.
Thanks

rake task scheduling with whenever

Hello I was trying to run a rake task every 5 minutes with this schedule code using the whenever gem
set :output, "#{path}/log/cron.log"
every 10.minutes do
rake "delete:old_offers"
end
But the code never execute. If I try to run my task with:
rake delete:old_offers
everything works great, so the problem is in the schedule file. Please I need your help to solve this issues.
I'm planning to execute this task every 60 days in my heroku app, so I could the schedule has to work also on heroku.
Thanks in advance.
UPDATE
Sorry guys, I have to set the environment to development like this
set :environment, 'development'
After you write your schedule, you need to actually update your crontab:
whenever --update-crontab
Simply running the whenever command by itself will only show you the schedule in cron format.

How can I create a rake task that will always run when any Rake task is ran?

From what I remember, in the documentation is specified that in the test environment, the database is always cleared even when you run rake ( with no arguments ). I'd like to achieve such a thing, so that it doesn't matter if I run a task or not, when I run rake, there's always a Rake task being executed. Is this possible? Is this where the default task kicks in?
Create a file called rakefile in the directory you want to run the task from.
This code will make it so that if you just type "rake" my_default_task will run:
task :default => 'my_default_task'
task :my_default_task do
puts "Now I am doing the task that Tempus wants done when he/she types 'rake' in the console."
end
task :my_not_default_task do
puts "This isn't the default task."
end
However, if you typed rake my_not_default_task, then my_default_task would NOT run. If you want it to run regardless here is one thing you can do:
task :default => 'my_default_task'
task :my_default_task do
puts "This is the default task"
end
task :my_not_default_task do
puts "This isn't the default task."
end
Rake::Task['my_default_task'].invoke
The last line in this code ensures that my_default_task runs even when you call some other task, so if you typed rake my_not_default_task the my_default_task'would also run.
EDIT:
When you're working with rails you can put the tasks above in a file in the lib/tasks folder with an extension of .rake and rails will automagically run them when you do rake
Jason Seifer has a real nice tutorial on rake.

Resources