rake task scheduling with whenever - ruby-on-rails

Hello I was trying to run a rake task every 5 minutes with this schedule code using the whenever gem
set :output, "#{path}/log/cron.log"
every 10.minutes do
rake "delete:old_offers"
end
But the code never execute. If I try to run my task with:
rake delete:old_offers
everything works great, so the problem is in the schedule file. Please I need your help to solve this issues.
I'm planning to execute this task every 60 days in my heroku app, so I could the schedule has to work also on heroku.
Thanks in advance.
UPDATE
Sorry guys, I have to set the environment to development like this
set :environment, 'development'

After you write your schedule, you need to actually update your crontab:
whenever --update-crontab
Simply running the whenever command by itself will only show you the schedule in cron format.

Related

Trouble with crontab and using whenever gem

Hi I am using the whenever gem and trying to send a daily email. For testing I set it to 2 minutes, and I have it in my schedule.rb file. It calls a task I have in a rake file. When I run bundle exec rake task_to_be_called, it runs and works. But the actual scheduling does not work. When I try to run things to find out crontab it says no such file or directory. Is there some way to get a crontab file, or do I make it? How do I test or get my scheduler to run that task?
EDIT: Thanks for the advice on sharing code and error.
In my lib/tasks/daily_email.rake I have
desc 'Daily email rake task test'
task daily_email_call: :environment do
ReportMailer.with(email: "email#email.com").daily_summary_report.deliver_now
end
Then in my config/schedule.rb I have
every 2.minute do
rake 'daily_email_call'
end
When I run bundle exec rake daily_email_call it functions correctly and does the send email task. My question is how to get it to do it on the schedule. I have no crontab file. Am I even able to do this locally or would it need to be on a running server. I am using windows not Linux when I run mine locally.
There is a typo in the file name,
lib/tasks/darily_email.rake => lib/tasks/daily_email.rake
I guess this is causing the error, no such file or directory

Rails Whenever gem and cron job not working on development environment

so this is like my first time doing a cron job, and also using a whenever gem.
this is what's in my schedule.rb
set :environment, :development
every 1.minute do
command "rails runner /home/ec2-user/projectA/scripts/download.rb"
end
It's running a script where it download's a file and does a few more other thing.
However, for some reason , the job is not running ? nothing is happening, the results are not returning. why is that?
UPDATE#############
i checked my cron log file, and it seems that it was running the script every minute as intended. however my results of the script is not producing as i would to run it manually.. why is that?

Rails 2 Delayed_job gem

I am working on rails 2 application where i need to send mail in background job.
I have Notifier model and from controller I call a model method to send mail.
I call that method Notifier.deliver_method_name(params) before using delayed job and after creating I use it Notifier.delay.deliver_method_name(params).
Now my question arise
1) Should I need to start the rake jobs:work to send mail in background in development and if yes should I run it every time I start server?
2) what do I need to do on production should I run rake jobs:work for the first time ?
3) Is that right if any error occurs then only there is a entry in delayed job if it is successful then there is no entry in delayed_job table.
I follow the below link
https://github.com/collectiveidea/delayed_job/tree/v2.0
Thanks :)
1) Should I need to start the rake jobs:work to send mail in background in development and if yes should I run it every time I start server?
Yes you have to start rake task separately.
Workers can be running on any computer, as long as they have access to the database and their clock is in sync. Keep in mind that each worker will check the database at least every 5 seconds.
2) what do I need to do on production should I run rake jobs:work for the first time ?
Yes you have to with RAILS_ENV=production script/delayed_job start But I suggest you stop and start this at every deployment to take changes done.
3) Is that right if any error occurs then only there is a entry in delayed job if it is successful then there is no entry in delayed_job table.
No it's completely depends on your configuration
# config/initializers/delayed_job_config.rb
Delayed::Worker.destroy_failed_jobs = false
Delayed::Worker.sleep_delay = 60
Delayed::Worker.max_attempts = 3
Delayed::Worker.max_run_time = 5.minutes

Heroku scheduler weekly?

How can I get the heroku scheduler to run weekly?
Is this even possible, from what I can see I can only schedule hourly, every 10 mins, or daily tasks with no option for a weekly.
If not, what are other Heroku Add-ons that might allow me to run jobs (i.e. cron job) tasks on a weekly bassis in production.
Thanks!
Update 2:
require 'date'
task :weeklydelete do
if Date.today.wday.zero?
runner "Event.clear_expired"
runner "Activity.clear_expired"
end
end
Update 2.5:
$heroku run bundle exec rake weeklydelete -a friendiosenew
Running `bundle exec rake weeklydelete` attached to terminal... up, run.6194
rake aborted!
undefined local variable or method `path' for main:Object
/app/lib/tasks/weeklydelete.rake:2:in `block in <top (required)>'
Tasks: TOP => weeklydelete
(See full trace by running task with --trace)
I found a great answer here. Just use a bash script in the Heroku Scheduler that checks the day of the week before running your command:
if [ "$(date +%u)" = 1 ]; then MY_COMMAND; fi # only run on Mondays
Set up a daily job, and in the job check if the day of week is Sunday (or whichever day). If it is that day, run the job. If it isn't that day, do nothing and exit.
edit: I was thinking more like
require 'date'
task :weeklydelete do
if Date.today.wday.zero?
runner "Event.clear_expired"
runner "Activity.clear_expired"
end
end
The logging to a file stuff wont work on heroku, and I'm not sure what you're using for the time stuff, but I fear if scheduler runs it not exactly at 3, that stuff might not work.
the Date class has some really great helper methods. So, Date.today.wday.zero? could be shortened to Date.today.sunday?
The idea above about running a daily job but then having it exit without running if the day of the week isn't the day you want the job to run is a great solution. However, if you have a worker dyno running Sidekiq or DelayedJob (or any other queueing scheme that allows scheduled jobs), there's another solution, which is to seed the jobs queue with jobs that are not to be run until the day of the week you need.
This approach is a bit cleaner than checking for a particular day of the week, but it only makes sense if you are already using at least one background worker dyno and/or price sensitivity is not an issue.

How to run a cron job in Heroku, with a Sinatra app

I'm writing a tiny Sinatra app, and I want to host it on Heroku for simplicity sake. But, what I have is a task that scraps some sites and adds some data into my database every hour. Currently this is just written as a ruby script that needs to be executed. What Heroku has is a rake based cron job. Now if this was a rails app, I could easily do this, but I want to avoid the clutter for something as simple as this.
Is there a way to avoid this? Or do I have to install rake as well with my app?
Thank you.
Eric
You need a Rakefile like:
desc "This task is called by the Heroku cron add-on"
task :cron do
# Do something
end
Heroku periodically executes rake cron in your app depending on whether you have selected the "cron add-on" to be hourly or daily.
You need to check out Rufus. Rufus is your friend. Rufus will be your crontab while your app is loaded.
I did not try this stuff on Heroku but, give it a try and reply to us.
http://codex.heroku.com/past/2010/4/15/rufus_scheduler_for_my_tiny_sinatra_apps/
Why Rufus is cool? Well check this out, it's clean :)
$ sudo gem install rufus-scheduler
require 'rubygems'
require 'rufus/scheduler'
scheduler = Rufus::Scheduler.start_new
scheduler.cron '00 23 30 * *' do
# Run every 30 days at 23h00m
# ...your magic code goes here...
end
Looked again and looks like I jumped the gun on the question.
For applications that aren't Rails, one just has to create a Rakefile and put the task there.
Hope this helps other people.
Cheers!

Resources