rails whenver gem , running a command only if certain condition passes. - ruby-on-rails

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

Related

How to set whenever gem crontab fail mail notification in rails application?

i have one crontab in the shedule.rb like below. i want to capture the crontab below rake task crontab error log in a email instead of error.log.
schedule.rb:
env :PATH, ENV['PATH']
set :output, {:error => 'log/error.log', :standard => 'log/cron.log'}
every 1.day, :at => '8:00 am' do
rake "att:upload_data"
end
How to handle/capture the error logs in a email?
You could use a class instead of a rake task and do error handling there:
every 1.day, :at => '8:00 am' do
runner 'DomainName.upload_data'
end
If you need to reuse the rake task you can execute it from your class:
require 'rake'
class DomainName
def self.upload_data
Rake::Task['att:upload_data'].execute
rescue SomeError => e
MyMailer.rake_error(e)
end
end
Also, if you just want to get notified of errors by email you could consider using a 3rd party service.

ruby - use gem 'whenever' to check the deadline

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.

Update database monthly ruby on rails?

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

how can i trigger a rake task on every 4th sunday of every month in rails?

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" ]

Cron job in ruby on rails not work

I followed the railscast http://railscasts.com/episodes/164-cron-in-ruby but I cant seem to make it worked.
I have schedule.rb in my config.
I wished to refresh my Database.count everyday in my homepage.
I cannot find the deploy.rb in my folder. Where is it?
For testing purpose, I changed it to every 2 seconds.
[schedule.rb]
every '2 * * * *' do
rake "pages_controller:home"
end
[pages_controller.rb]
class PagesController < ApplicationController
def home
#title = "Home"
#companies = Company.find(:all, :limit => 20)
#count = Company.count
end
I have put
gem 'whenever', :require => false
in my gemfile. What have gone missing?
I have used cron job but i run it as rake task please you can try it
every 3.minutes do
set :environment, 'development'
rake "daily",:output => {:error => 'error.log', :standard => 'cron.log'}
end
And my task is like
require 'rubygems'
task :daily => :environment do
puts "i am fine"
# do your code
end
If cron job run fine then there will be nothing in cron.log.Otherwise it will show you if
any error occurs and this file will be generate in your app root directory.
try it..
So... Couple things.
You need a Capfile and a config/deploy.rb to deploy your code using Capistrano and Whenver. You get this by running capify . ... You should most likely watch the railscast on capistrano deployment
Whenever is using configured like:
schedule.rb
every 1.day, :at => '4:30 am' do
runner 'Rails.cache.clear'
end
I really don't think rake "pages_controller:home is going to work unless this is something you've already created elsewhere. I'm further assuming you are caching this page, and that's why you need to refresh the cache.
Finally, you're setting your environment to development, which makes me think you are not deploying this, but instead just wanting to reset the cached home page... So just run rake cache:clear

Resources