A cron job that creates objects - rails - ruby-on-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

Related

Run rails cron job with a life span

My Rails cron job:
every 3.hours do
runner "MyModel.some_process"
rake "my:rake:task"
command "/usr/bin/my_great_command"
end
I only want this triggered when an attribute in MyModel is set to true and stop when it is set to false.
For triggered when an attribute (e.g. availability) in MyModel set to true and stop if it is set to be false, You need to create a method in MyModel like this -
def other_process
if attr
MyModel.some_process
rake "my:rake:task"
command "/usr/bin/my_great_command"
end
end
every 3.hours do
MyModel.other_process
end
The new Rails Active Job is designed for this very use case.
Active Job is a framework for declaring jobs and making them run on a variety of queuing backends. These jobs can be everything from regularly scheduled clean-ups, to billing charges, to mailings. Anything that can be chopped up into small units of work and run in parallel, really.
http://edgeguides.rubyonrails.org/active_job_basics.html

In Rails, how can I have a list of commands automatically run say every 24 hours?

I have a backend Rails JSON API.
Every X amount of hours, there is some data I would like to purge.
So I need to be able to run some commands every X hours.
How can I accomplish this in Rails?
Whenever is a Ruby gem that provides a clear syntax for writing and deploying cron jobs.
It is an easy way to write cron jobs to a cron tab file which is later on executed by the system. It provides you a nice DSL to schedule your tasks.
Let's assume you have a test action in YourModel that you want to run every 24 hours, so you will be doing this:
your_model.rb
def test
# Do Something...
end
schedule.rb
every 24.hours do
runner 'YourModel.test'
end
You need to run the following command on your project to do create the crontab:
whenever -i
You can use this gem whenever (https://github.com/javan/whenever), which will help you manage scheduling using crontab.

How to schedule whenever task based on a table column?

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.

Using Whenever with RedMine

I am trying to set up a cron task for a model in a redmine plugin. The Whenever gem looked like the best option. Alas, it does not seem to do anything.
I have set up the gem, both in the config folder of the plugin and the primary one. I added it to the crontab (using the whenever -i command) have reset the server.
The action was not run, and no log was generated
here is the schedule.rb file.
set :output, "/path/to/my/cron_log.log"
every 1.day, :at => '9:00 am' do
runner "pluginMailMethod.send_emails"
end
every :thursday, :at => '4:20 pm' do
runner "pluginMailMethod.send_emails"
end

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.

Resources