I want to create a rake task that will run automatically every seven days (sunday at midnight), that will analyse a weeks worth of data (at max 20,000 records per week).
An Outlet has_many :monitorings
A Monitoring belongs_to :outlet
I want to check if at the end of the week an outlet has had a minimum of 4 records created. If not, I want a record to be created inside of the DB in a table called unmonitored.
The record should contain the the number of times it has been monitored and the week start and end dates.
To run a rake task periodically, you can use bare cron jobs or a nice ruby wrapper, Whenever.
Take a look:
every 3.hours do
runner "MyModel.some_process"
rake "my:rake:task"
command "/usr/bin/my_great_command"
end
every 1.day, :at => '4:30 am' do
runner "MyModel.task_to_run_at_four_thirty_in_the_morning"
end
every :hour do # Many shortcuts available: :hour, :day, :month, :year, :reboot
runner "SomeModel.ladeeda"
end
every :sunday, :at => '12pm' do # Use any day of the week or :weekend, :weekday
runner "Task.do_something_great"
end
every '0 0 27-31 * *' do
command "echo 'you can use raw cron syntax too'"
end
Related
config/schedule.rb
def timezoned time
Time.zone = "Pacific Time (US & Canada)"
Time.zone.parse(time).utc
end
every 1.day, at: timezoned('5:30 am') do
runner 'App1::Task1.perform_async'
end
every 1.day, at: timezoned('5:31 am') do
runner 'App1::Task2.perform_async'
end
every 1.day, at: timezoned('5:33 am') do
runner 'App2::Task1.perform_async'
end
every 1.day, at: timezoned('5:34 am') do
runner 'App2::Task2.perform_async'
end
every 1.day, at: timezoned('5:36 am') do
runner 'App3::Task1.perform_async'
end
every 1.day, at: timezoned('5:37 am') do
runner 'App3::Task2.perform_async'
end
every 1.day, at: timezoned('5:40 am') do
runner 'App4::Task1.perform_async'
end
every 1.day, at: timezoned('5:41 am') do
runner 'App4::Task2.perform_async'
end
every 1.day, at: timezoned('5:42 am') do
runner 'App4::Task3.perform_async'
end
every 1.day, at: timezoned('5:43 am') do
runner 'App5::Task4.perform_async'
end
I am using Sidekiq with whenever gem to schedule the jobs in Rails.
Above is my schedule.rb. Is there way to schedule the tasks in a better way than the above? (e.g time + 1.minute or time + 2.minutes)
It will be helpful if any better approach for scheduling task that the above.
Option 1:
since this is a Ruby code you can dynamically iterate over array of Apps, Tasks and dynamically setup scheduler
Option 2:
You can actually make
every :minute do
runner 'AppTaskRunner.perform_async'
end
And in AppTaskRunner start needed task per app depends on your logic
I have a table "Point" with an integer column "monthly_point", and a table "User" with an integer column "Give_Point". I want to update the value of "Give_Point" is value of "monthly_point" every month. How can i do that? Thanks all.
There is a gem for such tasks, called sidetiq
It uses sidekiq for background jobs.
Here is an example
class MonthlyPointWorker
include Sidekiq::Worker
include Sidetiq::Schedulable
recurrence { monthly }
def perform
# do your work here
end
end
Whenever just puts your tasks into the crontab and executes it using cron.
It's up to you which one you will use for your needs.
You should use cron-jobs to achieve this so, use the whenever gem
Just add gem 'whenever', require: false to your Gemfile and run bundle install.
Then within your application’s root directory, run the following command:
`bundle exec wheneverize`
This will add a schedule file to your config folder (config/schedule.rb). This file is where you set up your scheduled tasks. You can use regular cron scheduling, or you can use a more idiomatic DSL for scheduling. The following examples are take from the gem’s README.md:
every 3.hours do
runner "MyModel.some_process"
rake "my:rake:task"
command "/usr/bin/my_great_command"
end
every 1.day, :at => '4:30 am' do
runner "MyModel.task_to_run_at_four_thirty_in_the_morning"
end
every :hour do # Many shortcuts available: :hour, :day, :month, :year, :reboot
runner "SomeModel.ladeeda"
end
every :sunday, :at => '12pm' do # Use any day of the week or :weekend, :weekday
runner "Task.do_something_great"
end
every '0 0 27-31 * *' do
command "echo 'you can use raw cron syntax too'"
end`
Once you’ve set up your scheduled tasks in config/schedule.rb, you still need to apply them to your crontab. From your terminal, use the whenever command to update your crontab:
Change to your application's directory.
cd /my/app
View config/schedule.rb converted to cron syntax
bundle exec whenever
Whenever Gem
I am writing a Rake task. I want to trigger it every last sunday of every month at 11 pm.
How can I schedule this using the Whenever gem in Ruby on Rails?
I have my rake task in the following location: app/lib/tasks/my_task.rake
task :create_entries => :environment do
puts "Hello"
end
I don't think there's a rule for "last x day of the month" but you can always put an extra if test inside the block:
every :sunday, :at => '11pm' do
#if the month is different a week from now, we must be in the last
#sunday of the month
if Time.now.month != 1.week.from_now.month
rake "my:rake:task"
end
end
So, this scheduled code will run every sunday, but it will only go on to call the rake task if the time meets the further conditions.
i am having a rake task. i want that task to be scheduled to run on every 4th Sunday of every month. i am using whenever gem. how can i define logic in config/schedule.rb so that it will run on every 4th Sunday of every month in Rails? please help me.
this is my rake task app/lib/tasks/my_task.rake.
task :do_something => :environment do
end
this is my code in config/schedule.rb
every :sunday, :at => '12pm' do
runner "Employee.make_checkin_out"
end
how can i change the above logic?
whenever gem gives a facility to generate a cron job. This is a scheduler for rake tasks. You can define like
every 1.month, :at => '4:30 am' do
runner "MyModel.task_to_run_at_four_thirty_morning_every_month"
end
If you want something like 1st sunday every month use cron editor like
0 12 1-7 * * [ "$(date '+\%a')" = "Sun" ]
We are using whenever gem with rails on our project. I understand that we can schedule a command using whenever gem like this.
every 1.day, :at => '6:00 am' do
command "echo 'hello'"
end
but my problem is that i want to execute this command only when some condition is met. something like this.
every 1.day, :at => '6:00 am' do
if User.any_new_user?
command "echo 'hello'"
end
end
how can we achieve this with the whenever and rails?
One possible solution i can think of is that i create a runner for this and check that condition there.Something like:
every 1.day, :at => '6:00 am' do
runner "User.say_conditional_hello"
end
and inside my user model:
def self.say_conditional_hello
`echo "Hello"`
end
Any suggestions on this approach or any new approach will be really helpful.
Thanks in advance!.
if you want to only schedule the task if the condition is true then I don't think its possible you can schedule a task then when its time for running the task, the code can decide if it should be run or not, depending on your conditions
One possible solution i can think of is that i create a runner for this and check that condition there.Something like:
Yes this is one way of handeling this, however IMO the best behavior when using whenever is creating a rake task that checks for conditions then executes your code or perform your job
Something like:
Rake Task
namespace :user do
desc "description"
task check_for_new: :environment do
if User.any_new_user?
# code
end
end
end
in your schedule.rb file
every 1.day, :at => '6:00 am' do
rake "user:check_for_new"
end