I have an application that contains a bunch of tasks, and every 3 minutes I want to run a cron job that sends a mail for some test. I'm using the whenever gem but it doesn't seem to be running at all.Any ideas?
config/schedule.rb
every 3.minutes do
runner "MailerClass.some_method"
end
MailerClass.rb
def some_method
mail(:to => "some email", :cc => 'some email', :subject => "Regular Email by rake task #{Time.now.strftime("%H : %m")}", :from => "default_sender#udk.com") do |format|
end
end
What I have Tried after modifing config/schedule.rb are,
whenever --update-crontab --set environment=development
sudo service cron restart
When I run
crontab -l
this is the output
0,3,6,9,12,15,18,21,24,27,30,33,36,39,42,45,48,51,54,57 * * * * /bin/bash -l -c 'cd /home/my_app && script/rails runner -e development '\''MailerClass.some_method'\'' >> log/notification.log 2>&1'
I have checked the log. The problem is
script/rails: line 6: syntax error near unexpected token ('
script/rails: line 6:APP_PATH = File.expand_path('../../config/application', FILE)'
I am not getting this. I am new to rails. Thats why may be :(.
Any Solution!!!
It looks like you are forgetting to actually send the email
Instead of
MailerClass.some_method
try
MailerClass.some_method.deliver
Based on your updated info i am updaing my answer
Remove last single quote form end of parenthesis form this line APP_PATH = File.expand_path('../../config/application', FILE)'
Related
I need to send some emails weekly.
I installed the mailer and did several tests. Is working properly.
But I can not use Whenever to automatically send emails.
Have searched in various forums and still can not fix.
Model/HrCurriculumIntern
def self.send_reply_interns
#users = HrCurriculumIntern.where(:answer_sent => t('labels.n'))
InternMailer.send_reply_interns(#users).deliver
end
Mailer/InternMailer
default :from => "username.mailer#gmail.com"
def send_reply_interns(users)
#users = users
mail(:to => "<lorenadgb#gmail.com>", :subject => t('subjects.send_reply_interns'), :from => "username.mailer#gmail.com")
end
COnfig/schedule.rb
set :environment, :development
every 2.minutes do
runner "HrCurriculumInterns.send_reply_interns"
end
I followed this steps:
Eu segui estes passos:
wheneverize .
whenever
0,2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,48,50,52,54,56,58 * * * * /bin/bash -l -c 'cd /var/www/form/3216/email/trunk && script/rails runner -e development '\''HrCurriculumInterns.send_reply_interns'\'''
## [message] Above is your schedule file converted to cron syntax; your crontab file was not updated.
## [message] Run `whenever --help' for more options.
whenever –update-crontab
0,2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,48,50,52,54,56,58 * * * * /bin/bash -l -c 'cd /var/www/form/3216/email/trunk && script/rails runner -e development '\''HrCurriculumInterns.send_reply_interns'\'''
## [message] Above is your schedule file converted to cron syntax; your crontab file was not updated.
## [message] Run `whenever --help' for more options.
I can't see the problem, any suggestion please?
Use the shortcut "whenever -w" to write the crontab. It looks like you used "whenever -update-crontab" instead of "whenever --update-crontab". So neither of your commands actually wrote the crontab file. The response should be
[write] crontab file updated
After that use "crontab -l" to verify that the correct cron was written.
I found the solution.
My code was wrong. I did some alterations:
Mailer/intern_mailer.rb
def send_reply_interns
#users = HrCurriculumIntern.where(:answer_sent => t('labels.n'))
mail(:to => "<lorenadgb#gmail.com>", :subject => t('subjects.send_reply_interns'), :from => "username.mailer#gmail.com")
end
Models/hr_curriculum_intern.rb
def self.send_reply_interns
InternMailer.send_reply_interns.deliver
end
schedule.rb
set :environment, :development
every 2.minutes do
runner "HrCurriculumIntern.send_reply_interns"
end
Its works now \ o /
Thanks for reply
Does anyone have experience/success using the whenever gem on aws opsworks? Is there a good recipe? Can I put that recipe on a separate layer and associate one instance with that additional layer? Or is there a better way to do it? Thanks!!!
EDIT:
We ended up doing it a bit differently...
Code:
Can’t really post the real code, but it’s like this:
in deploy/before_migrate.rb:
[:schedule].each do |config_name|
Chef::Log.info("Processing config for #{config_name}")
begin
template "#{release_path}/config/#{config_name}.rb" do |_config_file|
variables(
config_name => node[:deploy][:APP_NAME][:config_files][config_name]
)
local true
source "#{release_path}/config/#{config_name}.rb.erb"
end
rescue => e
Chef::Log.error e
raise "Error processing config for #{config_name}: #{e}"
end
end
in deploy/after_restart.rb:
execute 'start whenever' do
cwd release_path
user node[:deploy][:APP_NAME][:user] || 'deploy'
command 'bundle exec whenever -i APP_NAME'
end
in config/schedule.rb.erb:
<% schedule = #schedule || {} %>
set :job_template, "bash -l -c 'export PATH=/usr/local/bin:${PATH} && :job'"
job_type :runner, 'cd :path && bundle exec rails runner -e :environment ":task" :output'
job_type :five_runner, 'cd :path && timeout 300 bundle exec rails runner -e :environment ":task" :output'
set :output, 'log/five_minute_job.log'
every 5.minutes, at: <%= schedule[:five_minute_job_minute] || 0 %> do
five_runner 'Model.method'
end
We have a whenever cookbook in our repo we use that you would be more than welcome to use here: https://github.com/freerunningtech/frt-opsworks-cookbooks. I assume you're familiar with adding custom cookbooks to your opsworks stacks.
We generally run it on its own layer that also includes the rails cookbooks required for application deployment (while not the app server):
Configure: rails::configure
Deploy: deploy::rails whenever
Undeploy: deploy::rails-undeploy
However, we usually also deploy this instance as an application server, meaning we do end up serving requests from the box we're using for whenever as well.
There is one "gotcha", in that you must set your path in the env at the top of the schedule.rb like this:
env :PATH, ENV['PATH']
I tried to a records cleanup after certain period of time (6 month) using gem 'whenever'.
In my whenever scheduler :
every 1.month, at: '1am' do
rake 'lib/tasks/cleanup_user.rake'
end
In the lib/tasks/cleanup_user.rake
#user = User.all.where(:created_at > 'Time.6.month.ago').delete
It seems about right. However, I got error 'uninitialized constant User'. I am relatively new in rails. Please assist me.
EDIT : I changed the game by run clean one line command :
set :output, "log/cron.log"
every 1.minutes, :environment => :development do
command 'User.where("confirmed = 0 AND created_at <= ?", 6.months.ago).delete'
end
I set the specific environment,and run this in command :
whenever --set environment=development --update-crontab userscleaning
Checking at crontab, its there but still not work. Any thought?
Try adding the environment dependency to your task.
task :cleanup_users => :environment do
User.where(:created_at > 'Time.6.month.ago').delete_all
end
If you want the callbacks to trigger, use destroy_all
task :cleanup_users => :environment do
User.where(:created_at > 'Time.6.month.ago').destroy_all
end
Here is a relevant Railscasts.
According to the answers here:
# lib/tasks/delete_old_records.rake
namespace :delete do
desc 'Delete records older than 6 months'
task old_records: :environment do
User.where('created_at > ?', 6.month.ago).destroy_all
end
end
Run with:
RAILS_ENV=production rake delete:old_records
In whenever:
every 1.minutes do
rake "delete:old_records"
end
Or in cron:
0 8 * * * /bin/bash -l -c 'cd /my/project/releases/current && RAILS_ENV=production rake delete:old_records 2>&1'
Rails 3.1 + Whenever gem.
I've got 2 jobs scheduled in Whenever. However, apparently nothing happens.
I have tried these methods through Heroku console, and they work. So I reckon that they do not get fired at any point. Am I missing something?
config/schulde.rb
every 1.day, :at => '03:30' do
runner "Alarm.proba"
end
every 1.day, :at=> '3:32 am' do
runner "Alarm.proba2"
end
Alarm model
def self.proba
#event = Event.find(10)
#user =User.find(12)
EventNotifier.alarm(#event, #user).deliver
end
def self.proba2
#event = Event.find(10)
#user =User.find(13)
EventNotifier.alarm(#event, #user).deliver
end
$ crontab -l
# Begin Whenever generated tasks for: quasi
5 4 * * * /bin/bash -l -c 'cd /Users/sergioabendivar/railsProjects/Tutos/quasi && script/rails runner -e production '\''Alarm.proba'\'''
5 4 * * * /bin/bash -l -c 'cd /Users/sergioabendivar/railsProjects/Tutos/quasi && script/rails runner -e production '\''Alarm.proba2'\'''
I assume that it woks in development because my console now says "You have an email". Actually if I check the email I got quite a lot.
Use Heroku Scheduler instead: https://devcenter.heroku.com/articles/scheduler
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