Saving a new value to the db using the whenever gem - ruby-on-rails

I'm trying to save a value in my db every hour. Let's say it saves a random string of characters to the letter field in my randomizer table. I have the whenever gem installed and my schedule.rb runs every hour calling a class method in my model. It doesn't seem to save a value the way I have it set up.
Here is my schedule.rb:
set :output, "#{path}/log/cron.log"
set :environment, 'development'
every 1.hour do
runner "Randomizer.rand_letters"
command "echo 'random job attempted.'"
end
Here is my Randomizer model:
class Randomizer < ActiveRecord::Base
def self.rand_letters
#random_string = Randomizer.new(letters: 'asdfg')
#random_string.save
end
end
Here is my output when I run whenever:
0 * * * * /bin/bash -l -c 'cd /home/action/workspace/word-vomit && bin/rails runner -e development '\''Randomizer.rand_letters'\'' >> /home/action/workspace/word- vomit/log/cron.log 2>&1'
0 * * * * /bin/bash -l -c 'echo '\''random job attempted.'\'' >> /home/action/workspace/word-vomit/log/cron.log 2>&1'
## [message] Above is your schedule file converted to cron syntax; your crontab file was not updated.
## [message] Run `whenever --help' for more options.

Related

Cronjob is running every hour but not at a specific time

I have a cronjob on my server that suppose to run a rake script on my rails app everyday at specific time (eg. 8.00 am). I use whenever gem to automate this. This is how it appears currently on the cron file:
0 8 * * * /bin/bash -l -c 'cd /home/deploy/apps/myapp/releases/20160121092339 && RAILS_ENV=production bundle exec rake overdue_payments --silent >> /home/deploy/apps/myapp/releases/20160121092339/log/cron.log 2>&1'
And it's not working. But strangely enough, if I edit my cron to run for every hour, it is working just fine! Here is how I did it to make it run for every hour:
0 * * * * /bin/bash -l -c 'cd /home/deploy/apps/myapp/releases/20160121092339 && RAILS_ENV=production bundle exec rake overdue_payments --silent >> /home/deploy/apps/myapp/releases/20160121092339/log/cron.log 2>&1'
I have modified my timezone to the local timezone (UK) but still not working:
We need to specify the timezone to cronjob description. Here is my example:
test_cron_job:
cron: "45 11 * * * Asia/Bangkok"
class: "Schedulers::TestCronJob"
queue: default
Above cronjob will be run at 11:45 am in Bangkok time. Btw you may find out your timezone by using:
ActiveSupport::TimeZone.zones_map.values.map(&:tzinfo).map(&:identifier)
I finally solved it! For the timezone, i just need to add TZ before it. So for my case it will be: TZ="Europe/London".
0 8 * * * TZ="Europe/London" /bin/bash -l -c 'cd /home/deploy/apps/myapp/releases/20160121092339 && RAILS_ENV=production bundle exec rake overdue_payments --silent >> /home/deploy/apps/myapp/releases/20160121092339/log/cron.log 2>&1'
Hope this solution help you
every :day, :at => (6..8).to_a.map { |x| ["#{x}:00","#{x}:30", "#{x+1}:00"] }.flatten do
# Run rake task.
end
# => ["6:00", "6:30", "7:00", "7:00", "7:30", "8:00", "8:00", "8:30", "9:00"]
Adjust initial range and the inner block to cover any range. Its support 24hr clock.

Capistrano Cron Rails - Crontab created, but not running

Capistrano, Whenever, Rails.
Using Whenever and Capistrano I pushed new data to the crontab on my server.
$ crontab -l
# Begin Whenever generated tasks for: myAPP
* * * * * /bin/bash -l -c 'cd /home/.../apps/myAPP/releases/20184..1433 && bin/rails runner -e production '\''myModel.new.HELLO()'\'''
# End Whenever generated tasks for: myAPP
myModel -> HELLO()
def HELLO()
p "HELLO"
end
How do I know if this is working? I don't think it is because the logs are blank. What am I doing wrong?
thanks
p "HELLO" just prints to stdout, which doesn't do much good in a cronjob context. Try this code instead:
def HELLO()
Rails.logger.info "HELLO"
end
This will print the message to the standard Rails log file. By default, this will be logs/production.log in the current release that Capistrano deployed.

Unable to run cronjob using whenever in rails 3

i follow all the steps for whenever with reference to https://github.com/javan/whenever
in schedule.rb
require 'yaml'
set :environment, 'production'
set :output, {
:error => "/log/error.log",
:standard => "/log/cron.log"
}
every 1.minute do
runner "User.weekly_update"
end
in gemfile
gem 'whenever', :require => false
output of some command
localhost:~/project$ whenever -i
[write] crontab file updated
localhost:~/project$ crontab -l
# Begin Whenever generated tasks for: /home/bacancy/project/config/schedule.rb
* * * * * /bin/bash -l -c 'cd /home/bacancy/project && script/rails runner -e production '\''User.weekly_update'\'' >> /log/cron.log 2>> /log/error.log'
# End Whenever generated tasks for: /home/bacancy/project/config/schedule.rb
# Begin Whenever generated tasks for: store
* * * * * /bin/bash -l -c 'cd /home/bacancy/project && script/rails runner -e development '\''User.weekly_update'\'''
# End Whenever generated tasks for: store
# Begin Whenever generated tasks for: lapulguilla
# End Whenever generated tasks for: lapulguilla
and then i type
localhost:~/project$ whenever
* * * * * /bin/bash -l -c 'cd /home/bacancy/project && script/rails runner -e production '\''User.weekly_update'\'' >> /log/cron.log 2>> /log/error.log'
## [message] Above is your schedule file converted to cron syntax; your crontab file was not updated.
## [message] Run `whenever --help' for more options.
In User model i have definition self.weekly_update
def self.weekly_update
puts "cronjobs is called in every minutes"
end
Your everything is correct, its just that you are saying to cron job to print something in background, so it is doing that and you are able to observe it.
Instead you should try create or destroy some records to feel the changes i think :)

Whenever rails gem not working with cronjob.. why?

I am using Whenever gem with rails. For some reason the first cronjob does not work. If i copy paste the command into the shell manually, it works as it should.
The second "touch testing123.txt" also works fine.
I have no idea why the GiftPackage.do_scheduled_deliveries rails method does not run. I'm really at a loss right now.. any help would be greatly appreciated!
# Begin Whenever generated tasks for: /var/www/mysite/releases/20130131200554/config/schedule.rb
*/1 * * * * /bin/bash -l -c 'cd /var/www/mysite/releases/20130131200554 && script/rails runner -e staging '\''GiftPackage.do_scheduled_deliveries'\'''
*/1 * * * * /bin/bash -l -c 'cd /var/www/mysite/releases/20130131200554 && touch testing123.txt'
# End Whenever generated tasks for: /var/www/mysite/releases/20130131200554/config/schedule.rb
Thanks!
:)
Most likely you are getting an error within the app itself.
First step would be, run the task in the console using the staging environment.
Second would be to check the cron logs and see if it is outputting any errors.
You can set whenever to log output by adding the following to your schedule.rb file;
set :output, "/[path to log]/cron.log"
This should help you get closer to finding the solution.
Try setting your PATH variable before the jobs, your cron is probably not able to find ruby :
For instance :
PATH=/usr/local/rvm/gems/ruby-1.9.3-p194/bin:/usr/local/rvm/gems/ruby-1.9.3-p194#global/bin:/usr/local/rvm/rubies/ruby-1.9.3-p194/bin:/usr/local/rvm/bin:/usr/local/sbin:/usr/local/$
# Begin Whenever generated tasks for: /var/www/mysite/releases/20130131200554/config/schedule.rb
*/1 * * * * /bin/bash -l -c 'cd /var/www/mysite/releases/20130131200554 && script/rails runner -e staging '\''GiftPackage.do_scheduled_deliveries'\'''
*/1 * * * * /bin/bash -l -c 'cd /var/www/mysite/releases/20130131200554 && touch testing123.txt'
# End Whenever generated tasks for: /var/www/mysite/releases/20130131200554/config/schedule.rb

whenever gem command not running on intervals

on the whenever gem I am getting everything right . and can udpate the schedule.rb file but not getting logs after every time interval . Here is the code
this is schedule file
every 2.minutes do
command "echo 'you can use raw cron sytax too'"
rake "ftp"
end
This is ftp.rake file in lib/tasks/
require 'net/ftp'
require 'timeout'
require 'pathname'
require 'fileutils'
task :ftp do
command "echo 'you can use raw cron sytax too'"
ftp = nil
f = File.open("/home/hb/demo_haml/log/ftp_transfer.log","w+")
f.write("\n#{Time.now}")
ftp = Net::FTP::new("192.168.x.x")
ftp.login("demo", "demo")
ftp.chdir("/home/hd")
ftp.getbinaryfile("one.conf", "/home/hb/demo_haml/conf/one.conf", 1024)
ftp.close if ftp
f.close
end
After this i get the time on the log for the first time when i fire
whenever --update-crontab store
Running crontab -l
# Begin Whenever generated tasks for: demo_haml
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 'echo '\''you can use raw cron sytax too'\'''
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 /home/hb/applications/other_old_apps/Arpit/demo_haml && RAILS_ENV=production bundle exec rake ftp --silent'
What to do to keep on executing after every interval?

Resources