I'm currently using Heroku's scheduler to run a script. However, the time that the script takes to run is going to increase from a few milliseconds to a few minutes. I'm looking at using the delayed_job gem to push this process off to a Worker Dyno. I want to continue to run this script once-a-day, just offload it to the worker. My current rake task is:
desc "This task updates some stuff for you."
task :update_some_stuff => :environment do
puts "Updating some stuff ..."
SomeClass.new.process
puts "... done."
end
Once the gem is installed, migration run, and worker dyno started, will the script just need to change to:
desc "This task updates some stuff for you."
task :update_some_stuff => :environment do
puts "Updating some stuff ..."
SomeClass.new.delay.process
puts "... done."
end
With this task still being a rake task scheduled by Heroku's Scheduler, is the only thing that needs to happen here the introduction of the delay method to put this in the Worker's queue?
Thanks in advance for any help.
Yes, DJ is a very simple gem, and you should be able to see this working correctly locally by adding a record to your delayed_jobs table in your database. With a worker running on Heroku everything should be fine.
Just make sure you have the following in your Procfile:
worker: bundle exec rake jobs:work
More information can be found here: https://devcenter.heroku.com/articles/delayed-job
Related
I have a rails app with sidekiq-pro version 5.2.1 So recently we introduced super_fetch in our code. It does rescue the jobs when the Sidekiq shuts down, now when these rescued jobs are executed again, does it restart from the beginning of the worker or it starts from where it was interrupted?
Sidekiq calls perform each time. There’s no magic to restart at some arbitrary line of Ruby.
i need to know how to run the queue_classic (rake qc:work) rake task automatically on Heroku. I tried with Procfile, but i am using Bamboo and i get the next error: "Heroku push rejected, Procfile is not supported on the Bamboo stack"
any idea??
Thanks.
On heroku there is a addon viz heroku-scheduler, its free. Add this addon to your application. It will appear in your application addon lists, click it, and you will lead to worker dashboard where you can add job for example
bundle exec rake task_name
and schedule the task.
I have a rails app using delayed job, it works fine in development.
However, when I pushed to production it doesn't do anything.
Delayed job starts up fine using either one of these commands:
RAILS_ENV=production rake jobs:work
or
RAILS_ENV=production script/delayed_job start
However, once the process has started it just sits there and does not process any of the jobs in the delayed_backend_mongoid table.
I checked the delayed_job.log and it shows that the process starts up fine but does not throw any errors.
And for the heck of it I checked the production.log and there are no errors there as well.
Also if I run
RAILS_ENV=production rake jobs:clear
It will delete the jobs that are in the que, so I don't think it has an issue communicating with mongodb.
I'm using
Rails 3.1.1
gem 'delayed_job'
gem 'delayed_job_mongoid'
Any insight will be greatly appreciated.
Ok so I finally got it to work, it wasn't related to delayed job at all. It had to do with the ruby gem Anemone. Anemone could not get proper authorization to the mongodb. Fixed, thanks anyways.
I'm using delayed_job and I am able to run jobs using rake jobs:work but using the daemonized version, it does nothing although I see it in the process list.
I'm using:
rails (3.0.9)
delayed_job (2.1.4)
daemons (1.0.10)
I'm running delayed_job using:
unix>RAILS_ENV=development script/delayed_job start
It could be a problem loading a custom job class file. To test that, try this:
Enter the rails console rails console --sandbox.
Make sure you have a job in the table job = Delayed::Job.first.
Try YAML.load(job.handler). If you get an error that looks like this: ArgumentError: undefined class/module MyCustomClass, it's probably a problem loading your custom job
Still in the rails console, run require 'My_Custom_Class. Then run the YAML.load(job.handler) command again. If this returns the appropriate object it's definitely a class loading problem.
To fix the problem create the file config/initializers/custom.rb and in it put require 'My_Custom_Class'.
You should then be able to run rake jobs::workoff and get something that looks like this:
[Worker(host:my.host pid:5085)] Starting job worker
[Worker(host:my.host pid:5085)] MyCustomJob completed after 0.0774
[Worker(host:my.host pid:5085)] 1 jobs processed at 9.1935 j/s, 0 failed ...
[Worker(host:my.host pid:5085)] No more jobs available. Exiting
To answer your question we may need more information.
Are jobs added to database? Are there any errors in jobs?
What's the result of RAILS_ENV=development script/delayed_job status as I already mentioned?
Second, did you went through the most common problems Wiki page?
https://github.com/collectiveidea/delayed_job/wiki/Common-problems
I'm really new to rails. I'm trying to learn how to write a cron job with a rake task to send daily digest emails. How should I go about doing this?
I'm guessing that I write a loop in the task to go through each user and compile and email. Then I send it inside the for loop.
How do I set this rake task as a cron job so that it runs in the background? This seems like it should be super simple, but I feel like I'm missing something.
One way to do this, as you've visualized already, is to define a rake task which would run at a scheduled time. Crontab is best for that if you're on a Linux system. Following are the steps you would follow to do that:
The rake task with the logic of fetching the emails to send digest emails.
A shell script, digest.sh which would load up and run the rake task. Something like this:
cd /your/app /usr/bin/rake utils:sendNotifications > /your/app/log/notifications.log
We're telling it to go into your application folder ( /your/app ), run the rake task utils:sendNotifications and log the output in log/notifications.log
And finally, schedule tasks on Linux using crontab.