Sidekiq and Rails background jobs on anynines

sidekiq

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.

Sounds neat, what do I need for that?

In this particular case, we use Sidekiq, a full-featured background processing framework for Ruby. Sidekiq enqueues its jobs into Redis, that’s why we need that, too.

Our ingredients thus far:
Ruby 2.x (or JRuby 1.7.x)
Sidekiq
Redis 2.4 or greater

Let’s start with the initializing

First, we create an initializer config/initializers/01_cloud_services.rb
This gives us the service credentials in the anynines environment. After that, we need to initialize Sidekiq. Therefor we create another initializer, config/initializers/02_sidekiq.rb

Now we can implement our workers and start enqueueing*, but how do we get this deployed to anynines?

How do I deploy this 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.

Leave a Reply

Your email address will not be published.