clean directory with whenever gem - ruby-on-rails

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.

Related

How to send mails automatically using whenever gem

I used when ever gem to send mails every day but it's not sending.I am using amazon linux ec2 instance. Please look at my code once.
schedule.rb:
every 5.minutes do
runner "Listings.today_expired_spaces"
end
Listings.rb:(model-file)
def today_expired_spaces
#list=List.find_by_date(Date.today)
UserMailer.today_expired_list(#list).deliver
end
In production:
After running these commands I am getting responses like this
1.whenever -w
[write] crontab file written
2.whenever -i [write] crontab file updated
3.whenever
0,5,10,15,20,25,30,35,40,45,50,55 * * * * /bin/bash -l -c 'cd
/home/ubuntu/my_server && bundle exec script/rails runner -e
production '\''Spaces.today_expired_spaces'\'''
[message] Above is your schedule file converted to cron syntax; your crontab file was not updated.
[message] Run `whenever --help' for more options.
Please provide solution to this.
you should make a rake task in lib/tasks folder and include the task to whenver schedule file in config/schedule.rb
in lib/tasks/send_mail_bang_bang.rake (filename just for example only)
require 'rake'
namespace :task_namespace do
desc 'task description'
task :send_mail => :environment do
# call Listing method to send mail
Listings.today_expired_spaces
end
end
in config/schedule.rb
every 5.minutes do
rake 'task_namespace:send_mail'
end
you can read more about rake task here in "custom-rake-tasks" part
hope it helps

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

How can I run a rake task via cron?

I have and rails application and a rake task which I'm going to execute by cron around once in an hour. But the thing is that the task uses rails environment and some classes of my rails application. If I run it as ruby script, I'll have to include all the dependencies it uses and I think it's not possible to do it correctly and in a simple way. So I'll have to run it as a rake task because it'll preserve all the dependencies, right? Then how can I run a rake task from cron?
Note that I prefer not to use any third-party solution when there's no necessity, in this case I don't want to use the gem whenever or the like.
You can add to your crontab something like
0 * * * * /bin/bash -l -c 'cd /path/to/your/project && bundle exec rake foo:bar >> log/cron.log 2>&1'
This will run foo:bar task every hour and write stdout and stderr to log/cron.log.
Please notice bundle exec before rake command.
Using bundler ensure you that task will fetch correct environment.
To specify RAILS_ENV you can do
... && RAILS_ENV=production bundle exec rake foo:bar
Use whenever to simplify your life https://github.com/javan/whenever ;)
Here's an example of a rake task:
task :foo => :environment do
puts "Running rake task in environment: #{Rails.env}"
# can access Models here or whatever
end
Note that the => :environment part is optional, but it what makes your Rails environment to the task block.
You can put rake run_my_task in your cron job.
You may need to use something like cd /home/$USER/my_rails app && rake run_my_task to ensure that the cron runs the task from the Rails root directory.

Ruby-on-Rails Whenever by Javan working step

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

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