Cron job in ruby on rails not work - ruby-on-rails

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

Related

How to set rake task for delete worker using whenever gem

I've got worker which deletes all admin logs older than 1 year. I want it run once a day using gem whenever as a rake task however I'm not follow the whole logic of rake tasks. I was trying to follow this article https://medium.com/#sampatbadhe/rake-task-invoke-or-execute-419cd689c3bd how ever the big question is should I move my worker to rake file or I can I refer directly to the file where this worker is?
my worker below:
class AdminPanelLogRemoverWorker
include Sidekiq::Worker
def perform
expired_logs = AdminPanelLog.where('created_at < ?', 1.year.ago).select(:id)
expired_logs.find_in_batches do |logs_batch|
AdminPanelLog.where(id: logs_batch.map(&:id)).delete_all
end
end
end
As far I understand my schedule.rb in whenever gem should be like below:
every 24.hours do
rake 'myproject:AdminPanelLogRemoverWorker'
end
It's a worker, not a rake task, so you can do it with
every 24.hours, at: '0:00am' do
runner 'AdminPanelLogRemoverWorker.perform_async'
end

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.

Rails Whenever set environment

I'm using whenever to generate the cronjobs for my Rails application.
Rails 4.2.1
In the schedule.rb I have something like that:
every 13.minutes do
rake "crons:dosomething"
end
This generates a cronjoblike this:
RAILS_ENV=production bundle exec rake crons:dosomething
Problem is that this one only runs cronjobs for production environment ... but I need the cronjob as well in test and development
I tried this:
every 13.minutes do
rake "crons:dosomething", :environment => :development
rake "crons:dosomething", :environment => :test
rake "crons:dosomething", :environment => :production
end
problem here is that the second rake runs before the first one is completely finished is there any solution for this problem?

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

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

Resources