Published at 06.10.2014
In a growing system, you will some times need to run extensive and complex tasks, which, because of their resulting accumulated load time, are not fit to process within a single request. This is where background jobs come in.
Background jobs allow you to trigger/enqueue a job, which then get processed in the background by so called workers.
Table of Contents
Our ingredients thus far:
Ruby 2.x (or JRuby 1.7.x)
Redis 2.4 or greater
First, we create an initializer
This gives us the service credentials in the anynines environment. After that, we need to initialize Sidekiq. Therefor we create another initializer,
Now we can implement our workers and start enqueueing*, but how do we get this deployed to anynines?
First of all, we need to create the Redis service. Therefor, we do following:
cf create-service redis 100 sidekiq-example-bj-queue
This creates our needed redis instance which we need to bind to our existing application.
cf bind-service my-sidekiq-example-app sidekiq-example-bj-queue
After that, we create a new application for the sidekiq workers in our manifest.yml
Deploy your application as usual and after that deploy your workers with the following command:
cf push my-sidekiq-example-workers
Now you’re ready to process some of that complex stuff in the background. Enjoy!
* For details about the usage of Sidekiq take a look at sidekiq.org.