Rails: Cron jobs with whenever gem - ruby-on-rails

I'm new to cron job as well as rails. I have installed whenever gem. Now I want to delete a file from tmp/cache/foo.txt.
schedule.rb
every 5.minutes do
command "rm '#{path}/tmp/cache/foo.txt' "
end
And when i run whenever command it says:
0,5,10,15,20,25,30,35,40,45,50,55 * * * * /bin/bash -l -c 'rm '\''/home/techbirds/shivam/tmp/cache/foo.txt'\'''
## [message] Above is your schedule file converted to cron syntax; your crontab file was not updated.
## [message] Run `whenever --help' for more options
So how to do it from starting?

You should update crontab (configuration file that defines commands to run periodically on a desired schedule) according to you new schedule.rb rules, so you should run the following command (if this job is set to a specific environment in your schedule.rb you should also --set environment):
whenever --update-crontab
To view your current jobs use crontab -e (jobs generated by whenever would be enclosed in # Begin Whenever and # End Whenever blocks).

Related

Unable to run 2 dependent commands in the same whenever cron job

I work on a Rails based website for which I have to create a sitemap using the sitemap_generator gem.
So far so good, until I reached the part where I have to automate the sitemap generation.
The 2 commands that I need to run in order to refresh the sitemap are direnv allow && rake sitemap:refresh.
I am trying to create a cron job by using the whenever gem and this is what I have inside config/schedule.rb:
# config/schedule.rb
set :output, "tmp/cron.log"
set :environment, "development"
every 1.minutes do
command "/Users/<user>/<project>/generate_sitemap.sh"
end
The generate_sitemap.sh file is in the project root and has the following content:
# generate_sitemap.sh
#!/bin/bash
direnv allow
rake sitemap:refresh
In order to activate the cron job, I execute whenever --update-crontab in the terminal. By running crontab -l, I have the following output:
# Begin Whenever generated tasks for: /Users/<user>/<project>/config/schedule.rb at: 2022-01-27 18:51:18 +0200
* * * * * /bin/bash -l -c '/Users/<user>/<project>/generate_sitemap.sh >> tmp/cron.log 2>&1'
# End Whenever generated tasks for: /Users/<user>/<project>/config/schedule.rb at: 2022-01-27 18:51:18 +0200
I have to mention that if I execute ./generate_sitemap.sh from the command line, it works. Furthermore, if I create a cron job as follows:
config/schedule.rb:
# config/schedule.rb
set :output, "tmp/cron.log"
set :environment, "development"
every 1.minutes do
rake "sitemap:refresh"
end
it is executed, but it results into an error since direnv allow was not executed beforehand.
My question is: how can I create a cron job which executes both direnv allow and rake sitemap:refresh?

Ruby on Rails - Whenever Gem + Cron Not Running

TL;DR: My Whenever Gem scheduled task does not run at all automatically but will manually.
I posted about this earlier in the week but got no responses, I'm attempting to implement the Whenever gem into my RoR 5 application. Once a year I want it to run a method in my 'User' model, but for testing purposes I have made it once every minute, like so:
Schedule.rb:
set :output, "/home/ubuntu/workspace/log/cron.log"
set :environment, 'development'
every 1.minute do
runner "User.(methodhere)"
end
User.rb:
def self.(methodhere)
User.all.each do |user|
user.update(remaining_days: user.total_days)
end
end
In multiple places I have read that sometimes cronjobs dont run properly in development mode, so I jumped through all of the hoops to put my application into production mode, and that did not help.
I then found that you can manually run these jobs in the command line, which I then tried to do using the command found doing:
whenever --update-cron
then
crontab -l
which showed
# Begin Whenever generated tasks for:
* * * * * /bin/bash -l -c 'cd /home/ubuntu/workspace && bundle exec
bin/rails runner -e development '\''User.new.(methodhere)'\'' >>
/home/ubuntu/workspace/log/cron.log 2>&1'
Running this manually:
/bin/bash -l -c 'cd /home/ubuntu/workspace && bundle exec
bin/rails runner -e development '\''User.(methodhere)'\'' >>
/home/ubuntu/workspace/log/cron.log 2>&1'
makes it work, and executes the (methodhere). Whenever just does not make it run automatically at the set interval.
Another thing I found was to try and restart cron, through cron restart, but I am receiving:
cron: can't open or create /var/run/crond.pid: Permission denied
I'm not sure if that has anything to do with the IDE I'm using, Cloud 9.
Many google searches have left me with nothing.
NOTE: I'm a very new developer with RoR, so any guidance on the matter would be greatly appreciated, thanks!

How to put filename in cron job (rails)?

I am using whenever gem. I am calling runner to run job. I called runner for the same job by 3 type. I am not sure which one working here.
I used command whenever --update-crontab project_name
then crontab -l
Schedule.rb
set :output, 'log/whenever.log'
every 1.days , :at => '03:51 pm' do
runner "SomeJob.perform_later", filename: '/app/jobs/some_job.rb'
end
every 1.days , :at => '03:51 pm' do
runner "SomeJob.perform_later", filename: './app/jobs/some_job.rb'
end
every 1.days , :at => '03:51 pm' do
runner "SomeJob.perform_later"
end
Also these ran only once. I am having difficult time to debug here.
Can anyone tell which one is correct? Also what is the correct way to debug in this scenario?
Running crontab -l gives me this -
51,51,51 16 * * * /bin/bash -l -c 'cd /home/rahul/orthoweb && bin/rails runner -e production '\''InvestigationStopJob.perform_later'\'' >> log/whenever.log 2>&1
To run in development environment I ran this command -
whenever --update-crontab --set environment='development'
But this gives me this message -
## [message] Above is your schedule file converted to cron syntax; your crontab file was not updated.
There are some issues with running the jobs in development mode.
What works is that you have to close all tabs before running your job and scheduler.
There is no need to change setup of your env in schedule file.
To run in development use this
whenever --update-crontab --set environment='development'
Without specifying filename jobs does not run properly. The following line works correctly.
every 1.days , :at => '03:51 pm' do
runner "SomeJob.perform_later", filename: '/app/jobs/some_job.rb'
end
I think, that the resulting crontab contains only one of those tasks, because they were overwritten.
To be sure, check out the resulting crontab by typing whenever. I think that there will be only one entrance.

Whenever cron jobs fail to run

I'm trying to use the whenever gem to run cron jobs into my application. I want to recalculate the attribute "value" of my model "User" every two minutes. Here are the steps I took:
Gemfile
gem 'whenever', :require => false
terminal
$ bundle install
$ wheneverize .
schedule.rb
every 2.minutes do
runner "User.recalculate"
end
models/user.rb
def self.recalculate
User.all.each {|user| user.recalculate_value }
end
private
def recalculate_value
self.value = self.value + 1
self.save!
end
terminal
$ whenever --update-crontab
and '$ whenever -l' returns this:
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/website && bin/rails runner -e development '\''User:recalculate'\'''
But so far nothing has changed in my User.values. Running User.recalculate in my console works, so the error is apparently in Whenever. What am I missing?
I quickly created a new Rails app to try to reproduce your problem, you can find it here, clone it and see for yourself.
https://github.com/mlainez/stackoverflow-28111330/blob/master/app/models/user.rb
First thing you will want to do is verify what environment the runner is running in. By default, whenever uses production. If you're on your local machine, be sure to set it to development in your schedule.rb or check the documentation to see how to update the crontab and set the correct environment.
https://github.com/mlainez/stackoverflow-28111330/blob/master/config/schedule.rb#L7
This is what you should see in your 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 /Users/marc/Projects/stack && bin/rails runner -e development '\''User.recalculate'\'''
Notice it says rails runner -e development and not production.
Another thing is that basia was right. When I leave the private keyword and try to execute User.recalculate in rails c, it fails with:
private method `recalculate_value' called for #<User ...>
I'm surprised it works with the private keyword in your console... You are trying to access a private method on an instance. This is not possible in Ruby. You can only access public methods of an instance from the outside (unless you use send(:method)).
Removing the private keyword fixed it and the task ran as expected every two minutes.
I'm using Rails 4.2.0 and ruby 2.1.0 in my github code.
EDIT
Make sure your cron daemon is running using:
service cron status
I also noticed that in your crontab output you have:
bin/rails runner -e development '\''User:recalculate'\''
Notice the : between User and recalculate. You must have made a typo in your schedule.rb
It should be a . as it's a method call.
Also, you might wanna check that the output from whenever -l and crontab -l are the same. If crontab -l is different or empty, then you didn't use whenever --update-crontab or whenever -w correctly or something might be wrong with the whenever version you're using.
Remove private since right now running User.recalculate will fail with private method 'increment' called for #<User> error.
BTW, is User an ActiveRecord class? Cause there is increment http://apidock.com/rails/ActiveRecord/Base/increment method and increment! (saves record), so it might be good idea to change your method name to something else or just use user.increment!(:value).
You don't seem to be saving your user in your method increment. Try this:
def increment
self.value = self.value + 1
self.save
end

Cannot get whenever to run my rake task

I am trying to get whenever to run my rake task but it doesn't work.
#Command Line
$ whenever -w
[write] crontab file written
#schedule.rb
every :year, :at => "2014-07-25 17:39:48 -0700" do
rake 'timeperiod:create_timeperiod'
end
The rake command saves a model in the db and puts text so I would know if it worked (which it does when I run rake). Is there syntactically something wrong with what I did?
Note, the time and date in schedule.rb is arbitrary, I keep changing it to two minutes from now before testing.
Your task is probably failing because the shell used by cron is not the same you use with your normal user.
Check the log of the crontab:
grep CRON /var/log/syslog
Why don't you verify that your command is getting added to the crontab?
$ crontab -l
You can also add the job manually.
$ whenever
* * * * * * bash -l 'cd /path/to/dir; rake your:task'
$ crontab -e
# add the cron job
Also, output from cron jobs doesn't output to the screen; it goes to your mail. Check your mailbox.
From the man page:
When executing commands, any output is mailed to the owner of the
crontab (or to the user named in the MAILTO environment variable in
the crontab, if such exists). The children copies of cron running
these processes have their name coerced to uppercase, as will be seen
in the syslog and ps output.
For me, I'm using command but I think it's not the best case. Says my repo is in $HOME/www/virtualspirit, in my repository. So my whenever command will be cd /www/virtualspirit && /$HOME/.rvm/rubies/ruby-2.1.2/bin/rake timeperiod:create_timeperiod RAILS_ENV=production
double check it in crontab -e and see whether it saved.
There were a couple issues here.
The best way to add cron jobs to whenever is with the command whenever --update-crontab <name of identifier>
The format was incorrect. By specifying the time in cron directly, I was able to make it work.
For example:
#schedule.rb
every '13 15 26 7 *' do
rake 'timeperiod:create_timeperiod'
end

Resources