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
Related
I have a job like this :
class LdapSyncJob < ApplicationJob
queue_as :default
require 'pp'
def perform
users = User.all
users.each do |user|
user.update("Do something")
end
end
end
and I use delayed job for the job processing .
My question is how and where to define my job to be run every night at a specific time ?
Should I call my job in an action or not ? if so how can I do that ?
The delayed job mainly used for processing tasks in queue and in background. It's usually not preferred for scheduling of tasks.
For your use case you should check out whenever a ruby gem, which works with cron jobs to schedule tasks repeatedly.
I would suggest you to move logic or method call LdapSyncJob.perform() to rake task.
In config/schedule.rb, you can do something this way
ENV['RAILS_ENV'] = "#{#pre_set_variables[:environment]}"
env :PATH, ENV['PATH']
require File.expand_path(File.dirname(__FILE__) + "/environment")
set :output, "/logs/cron_log_#{ENV['RAILS_ENV']}.log"
every 1.day, :at => '02:30 am' do
command "cd #{Rails.root}/lib/tasks && rake task_calls_peform_code"
end
Note : Don't forget to update and restart crontab using belong commands.
whenever --update-crontab app --set 'environment=production' #update crontab
service crond restart #restart crontab
I want to use 'whenever' this gem to check my all projects are still not out of deadline. I wrote this code but it didn't work and change status in the database. Can somebody give me some advises. Thank you for helping!
config/schedule.rb
set :environment, :development
every 1.day, at: '11:3 am' do
rake 'project:close_project'
end
app/models/project.rb
def self.close_project(dt)
# 締切日が過ぎているプロジェクトを抽出
Project.where(deadline > dt).each do |project|
# 対象プロジェクトを終了状態に
project.update!(status: 'closed')
end
end
product.rake
namespace :product do
task :close_project => :environment do
Project.close_project(Date.today)
end
end
Whenever creates jobs based on CronJob format. So to run your jobs periodically, you should run whenever command and copy and pasting the results to crontab by running crontab -e or do this task automatically just by running whenever -w.
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
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