How should I run delayed_job in production environment? - ruby-on-rails

I have no problems with running it in development mode via rake jobs:work. However, I'm somehow unable to figure out how to use it in production. I'm using Capistrano for deployment.
Thanks for any advice!

If you install delayed_job as a gem you need to run the generator in order to create the script scripts/delayed_job and set run permissions.
Then you can follow the instructions on How to configure Capistrano for Delayed Job to hook it up in your Capistrano file.

See this answer. In a nutshell, use the Collective Idea fork of delayed_job. It contains a script called delayed_job that can be used.

You can run the generated delayed_job script as follows:
RAILS_ENV=production script/delayed_job start
Hope this helps

My first thought will be to add a after deploy task in capistrano to run the rake jobs:work task. you might need to check if the process is already running and restart it.

If you are running it via rake then couldn't you just run however often you wanted via cron? The whenever gem is a great interface to this from ruby.

Related

Cron job using "whenever" ruby gem is not working

On my local I am generating sitemaps with the help of sitemap_generator gem. I have set up everything and now when I run rake sitemap:generate it is generating sitemap. Now I am trying to schedule this process using whenever gem. My schedule.rb is as follows:
schedule.rb
every 1.minutes do
rake "sitemap:refresh"
end
I keep my rails server running but nothing seems to happen after 1 minute. I might be missing something here. Before I push it on production server I wanted to check it. Can someone please tell me what is the issue here?
Can you please run crontab -l and check if cronjob is scheduled.
You need to convert your schedule.rb to cron syntax.
Try
whenever --update-crontab
And then check crontab.
crontab -l
Whenever command

How do I run CRON jobs for a Rails app on Amazon EC2?

Could anyone give me a tip how to set up CRON jobs for a Rails app that's running on Amazon EC2 (Ubuntu)?
Trying to find some tutorials or tips how to make it work, but still without any success.
Thank you
Try creating a model method which execute a task for your crondjob. You can execute the script by using rails runner.
rails runner 'User.deliver_reminder_emails!'
The whenever gem can even do fancier stuff: https://github.com/javan/whenever
You should have a look at rake tasks. Check this, this and this.
Running them is as simple as
rake foo:bar
You can schedule the same in your crontab; You might need to setup the correct PATH variable in case you are managing gems locally using rvm and bundler.
This worked out to be rather tricky for me, as the ELB-EC2 instance was trying to run a cron job as root, which seemed to result in a different set of permissions, bundle, etc.
It's a bit hacky, but this worked:
sudo su ec2-user bin/bash -lc "cd /var/app/current && /opt/rubies/ruby-2.1.8/bin/bundle exec/bundle exec rails runner -e your_env \"Model.do_something\""
Basically, su to the user that usually runs rails, and explicitly state the paths for bundle.

How to troubleshoot a dying resque process

I have a Rails 3.x application with Resque. I run the resque command with:
nohup rake RAILS_ENV=production environment resque:work QUEUE='*' & >>/tmp/resque.log 2>> /tmp/resque.err.log
Every other day the process dies, but the two output files are always empty. Any other way of figuring out why the Resque process goes down?
Try the super awesome Pry console. This is similar to irb only much more advanced.
You can use binding.pry inside your perform method or preferably in a hook which will start up a pry console using which you can debug. Helped me in similar situations a lot.

run whenever gem in mongrel development environment

I want run whenever gem and I have this task in schedule.rb file:
every 5.minutes do
rake "directory:cleanup"
end
I'm in environment development with Mongrel.
Why this task is not fired every 5 minutes?
What am I doing wrong?
PD: The task works fine from console.
You need to run the whenever --update-crontab command from the root directory of your application. This will add the task to your crontab.
However, if you're using OSX, there probably isn't a crontab to begin with so this won't work.
If you are constantly needing to cleanup some directories in development, I think there might be a better solution. Look into using something like Guard perhaps.

How to setup delayed_jobs in Linux?

I have a remote Ubuntu Linux for testing a Ruby on Rails application. I deployed delayed_jobs gem in the application.
In my local machine, I used rake jobs:work to start the worker process, which will run all delayed jobs automatically.
I would like to start this worker process in Linux and then quit the SSH connection.
What's the best practice in setting up delayed_job in linux? Thanks.
try to use & at the end of your command, in order to start rake as a background process:
rake jobs:work &
Now you can quit SSH
Use nohup rake jobs:work & can fix the problem. Solved :)

Resources