Ruby-on-Rails Whenever by Javan working step - ruby-on-rails

I follow the railscast whenever by javan tutorial but I seems to unable to make it happen.
Is there a MUST to install capistrano to have cron job? it is because I don't have the deploy.rb. I list down the steps in details. If there any steps I had missed to cause my cron not working? I always get a report say I got a new mail but I didnt send any email operation.
Step1:
wheneverize .
====> schedule.rb in config folder
Step 2:
[Scheduler.rb]
every '1 * * * *' do
runner 'Company.count'
end
Step 3: [what is "store"?]
whenever --update-crontab store
Step 4: [config/deploy.rb]<-- I don't have deploy files so I create it myself.
after "deploy:symlink", "deploy:update_crontab"
namespace :deploy do
desc "Update the crontab file"
task :update_crontab, :roles => :db do
run "cd #{release_path} && whenever --update-crontab #{application}"
end
end
Step 5 :
whenever --update-crontab store
crontab -l
What have I missed? why it didnt work? Please enlighten me step by step because I am newbie in ROR.. thanks.

The Whenever docs have an example of deploying Whenever with Capistrano. Just add the following to the top of your Capistrano deploy config.
set :whenever_command, "bundle exec whenever" # set this first if using bundler
require "whenever/capistrano"
Then Whenever will install into cron as part of the deploy.
UPDATE
To test that Whenever was successfully updated cron, ssh to the target machine as the Capistrano deploy user for the target machine and run crontab -l. You should see output similar to this:
crontab -l
# Begin Whenever generated tasks for: app_name
0 0 * * * /bin/bash -l -c 'cd /opt/path/app_name/releases/20120321133343 && RAILS_ENV=production rake group:task --silent'
0 3 * * 6 /bin/bash -l -c '/opt/path/app_name/shared/bin/script'
# End Whenever generated tasks for: app_name

Related

Ruby on Rails crontab task is scheduled but not running

I am using the whenever gem to create the rake task, i am expecting to get a line output "Teaching Thabo how to schedule tasks" on my terminal every 2 minutes, i am new to Rails, here is the code for my rake task file which i have named request_invoice.rake
namespace :request_invoice do
desc "Teaching Thabo how to schedule tasks"
task test_tasking: :environment do
puts "Learning the tasking in rails..."
end
end
And the whenever gem created a schedule.rb file in the config of my project, i have the following code in the file
every 2.minutes do
rake 'request_invoice:test_tasking'
end
I have ran the whenever command on the terminal, it gives the following:
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,5 2,54,56,58 /bin/bash -l -c 'cd /home/sable/GMS/grading- management-solution && RAILS_ENV=production bundle exec rake request_invoice:test_tasking --silent'
## [message] Above is your schedule file converted to cron syntax; your crontab file was not updated.
## [message] Run `whenever --help' for more options.
Did you run the:
whenever --update-crontab
command? According to the gem's page, this is required for your crontab file to be updated.
First, you need to check whether you have added tasks to crontab or not using:
crontab -e
If you do not see your task, then add it using:
whenever -i
You can track your cron jobs by setting a log file. Just add the following to the top of config/schedule.rb:
set :output, "log/cron_log.log"
Now, you should be able to check your cron logs (if tail is available to you then do the following from the root of your application path):
tail -f log/cron_log.log

Whenever cronjobs does not run on staging

I am trying to make a simple cron job running this task:
Here is my schedule.rb:
set :environment, "staging"
set :path, "/var/www/my_app/current"
every 2.minutes do
# specify the task name as a string
rake 'cron_test'
end
Here is the task:
task :cron_test => :environment do
out_file=File.new("cron_test.txt", "w")
out_file.puts(Time.now.to_s)
out_file.close
end
I tried to do what is advised on this page but nothing works:
Cron job not working in Whenever gem
When I run crontab -l :
# Begin Whenever generated tasks for: /var/www/my_app/current/config/schedule.rb
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/my_app/current && RAILS_ENV=staging bundle exec rake cron_test'
# End Whenever generated tasks for: /var/www/my_app/current/config/schedule.rb
When I run grep CRON /var/log/syslog:
Jun 29 17:22:01 my_server CRON[21164]: (root) CMD (/bin/bash -l -c 'cd /var/www/my_app/current && RAILS_ENV=staging bundle exec rake cron_test')
Any ideas?
Thanks
What happens when you manually run the command from crontab on the console? Try to run in. See if there are any errors.
/bin/bash -l -c 'cd /var/www/my_app/current && RAILS_ENV=staging bundle exec rake cron_test'
Might be something with local or cron environment. Also logs would be great, capturing any running errors. You can set output log to capture any problems with crontab running the command.
set :output, '/path/to/file.log'
for you example:
set :output, '/var/log/my_app.cron.log'
set :environment, "staging"
set :path, "/var/www/my_app/current"
every 2.minutes do
# specify the task name as a string
rake 'cron_test'
end
Here's the documentation whenever output redirection
Thanks it is working now!
When I created the /var/log/staging_cron.log I read this:
/bin/bash: bundle: command not found
I added env :PATH, ENV['PATH'] on the first line of my schedule.rb, updated the crontab (whenever --update-crontab) and now my test file is well created and updated.

crontab didn't run on ubuntu 14.04

I had a problem about set crontab Ubuntu 14.04 on DigitalOcean VPS.
Here are my steps:
First, create new crontab: crontab -e
Second, i set this command:
This is my crontab which i set it run at 10:26AM everyday:
26 10 * * * /bin/bash -l -c 'cd /var/www/my_app/current && RAILS_ENV=production bundle exec rake check:user >> log/cron_log.log'
Third, i checked crontab as crontab -l
Finally, i restart crontab as sudo service cron restart
But it didn't run anyway (i waited for 10:24 until 10:30), and i copied this command and ran it in my console, it worked very well.
So, i didn't understand why it didn't run same as crontab. Hope everybody can explain or give me some advises. Thank you very much.
EDIT: I tried to use gem whenever but it didn't run.
set :environment, "production"
env :PATH, ENV['PATH']
env :GEM_PATH, ENV['GEM_PATH']
set :output, {:error => "log/cron_error_log.log", :standard => "log/cron_log.log"}
# Learn more: http://github.com/javan/whenever
every :day, at: '10:26am' do
rake "check:user"
end
And i updated my crontab use whenever as whenever -w
But it don't work.
Update:
#Coderhs: I tried run this command bundle exec whenever --update-crontab RAILS_ENV=production, but it didn't work :(.
This is list my crontab when i use command crontab -l:
# Begin Whenever generated tasks for: RAILS_ENV=production
PATH=/var/www/my_app/shared/bundle/ruby/2.2.0/bin:/usr/local/rvm/gems/ruby-2.2.1/bin:/usr/local/rvm/gems/ruby-2.2.1#global/bin:/usr/local/rvm/rubies/ruby-2.2.1/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/local/rvm/bin
GEM_PATH=""
26 10 * * * /bin/bash -l -c 'cd /var/www/my_app/releases/20150710024713 && RAILS_ENV=production bundle exec rake user:check --silent >> log/cron_log.log 2>> log/cron_error_log.log'
# End Whenever generated tasks for: RAILS_ENV=production
It still didn't run.
UPDATE:
The problem was solved. Because in my local TIME was different with SERVER TIME.
So, i had just set SERVER TIME same as my LOCAL TIME. Thank you everybody supported me.
Like previously mentioned, I would use whenever gem.
Make sure you run whenever -w again after each capistrano deploy so its looking for the correct release directory.
Make sure you run whenever -w as the same user that your application is running as. Don't use root unless your app is running as root, which it shouldn't be.
Also, you should have your log dir set as a shared directory in Capistrano deploy config. Something like this in config/deploy.rb :
set :linked_dirs, fetch(:linked_dirs, []).push('log', 'tmp/pids', 'tmp/cache', 'tmp/sockets', 'vendor/bundle', 'public/system', 'pids', 'public/uploads', 'public/temp')
Also run the 'date' command in your terminal and see what the time there is. It might not be your local time, so the cron could work but at a totally different time than when you are expecting.
The issue is probably because the user which runs the cron, can't access the ruby command 'bundle'. You will have to use the full bath to the bundle executable file in the crontab.
The better solution for this problem would be to use the whenever gem https://github.com/javan/whenever
It would manage all the stuff related to crontab, to avoid issues like this.
schedule.rb for whenever
set :environment, "production"
set :output, {:error => "log/cron_error_log.log", :standard => "log/cron_log.log"}
env :PATH, ENV['PATH']
env :GEM_PATH, ENV['GEM_PATH']
# Learn more: github.com/javan/whenever
every :day, at: '10:26am' do
rake "check:user"
end
don't forget to run.
bundle exec whenever --update-crontab RAILS_ENV=production
after you set the shedule.rb

clean directory with whenever gem

I have installed whenever gem:
I want to clean the directory public/uploads/tmp in my app ruby on rails 3.1 each 5 minutes.
every 5.minutes do
#here go the code for clean the directory tmp
end
How can I do it?
Thank you!
You could try using FileUtils#rm_rf included in the standard library. For example:
FileUtils.rm_rf Dir.glob("#{Rails.root}/public/uploads/tmp/*")
Edit (to use it with whenever gem)
An approach by using a rake task could be:
1) Create a rake task in f.ex: lib/tasks/cleanup.rake with something similar to the following:
require 'fileutils'
namespace :app do
desc "Cleanup temp uploads"
task :cleanup => :environment do
FileUtils.rm_rf Dir.glob("#{Rails.root}/public/uploads/tmp/*")
end
end
2) In config/schedule.rb (created by whenever after running the wheneverize command):
every 5.minutes do
# run the previous app:cleanup task
rake "app:cleanup"
end
3) Whenever is only a wrapper to easily define crontab jobs, so now we need to export the defined schedule into the crontab file for the current user. To do that we should type from the application root:
bundle exec whenever -w
4) You can check that it worked by typing crontab -l and you should the something like the following:
# Begin Whenever generated tasks for: /tmp/whene/config/schedule.rb
0,5,10,15,20,25,30,35,40,45,50,55 * * * * /bin/bash -l -c 'cd /tmp/whene && RAILS_ENV=production bundle exec rake app:cleanup --silent
As a side note, if you want the operation to write some log output, please check this page on the whenever github wiki.
Hope it helps.

Cron job not working in Whenever gem

I have an application that contains a bunch of tasks, and every day I want to run a cron job that creates a DayTask for each Task in the database. A Task has_many DayTasks and these daytasks are what users will be checking off every day. I'm using the whenever gem but it doesn't seem to be running at all. Any ideas?
config/schedule.rb
every 1.day, :at => "12:01am" do
runner "Task.generate_tasks_for_day"
end
Task.rb
def generate_tasks_for_day
Task.all.each do |task|
task.day_tasks.create(:target_date => Date.today)
end
end
result of running the 'whenever command'
1 0 * * * /bin/bash -l -c 'cd /home/grant/rails_projects/GoalTwist && script/rails runner -e production '\''Task.generate_tasks_for_day'\'''
Note: I've been changing the times in config/schedule.rb every time I want to test run it.
Finally I have solved how to run the gem Whenever.
It's working good on production, but not in development mode (I think that to working good in dev mode you must do some tricks).
Then, these are the processes to do:
install the gem
write your scheduler.rb file
push to the remote server
login to the remote server (for example with ssh)
see if whenever is good uploaded by running in terminal: whenever
update whenever crontab by running: whenever --update-crontab
restart the server crontab (for example in Ubuntu server): sudo service cron restart
check if crontab is good implemented on the server: crontab -l
That's it!
Personally, I prefer to set up my crons directly from the server:
Edit the crontab: crontab -e
Append my cron (e.g. every day at 5:00 AM - can be little different for not-Linux-based server):
0 5 * * * /bin/bash -l -c 'cd /path_to_my_app/current && RAILS_ENV=production bundle exec rake my_cron_rake'
Check if good implemented: crontab -l
Done
You have to actually register the job with the crontab by running:
whenever --update-crontab
Also if you're trying to get jobs running locally, add
:environment => "development" to your task
runner "MyTask.some_action", :environment => "development"
Try to execute the whenever generated command directly from terminal or add the following line
MAILTO=your#emailadress.com
to your crontab.
if whenever command gives you -bash: whenever: command not found, try bundle exec whenever

Resources