Tag Archives: background jobs

Single Process Asynchronous Job with Sucker Punch

In a previous post we discussed about running asynchronous job with sidekiq. Sidekiq will spawn it’s workers processes and look for jobs on queue. But what if we don’t want to use separate processes to do asynchronous job?

Enter sucker punch.  Sucker punch enable us to do asynchronous processing within a single job.  This will reduce memory usage in your hosting and reduce hosting cost in Heroku. I use it often when I develop a mockup on Heroku free hosting.It won’t be wrong to call it a poor men’s sidekiq.

Sucker punch is simple to use. I just want to remind you to take special precaution when interacting with active record in an asynchronous process. Use ActiveRecord::Base.connection_pool.with_connection to not exhaust the connection in the ppol, as per the example in the readme:

# app/jobs/awesome_job.rb

class AwesomeJob
  include SuckerPunch::Job

  def perform(user_id)
    ActiveRecord::Base.connection_pool.with_connection do
      user = User.find(user_id)
      user.update_attributes(is_awesome: true)

I also like to give some delay before starting an async job.

class Job
  include SuckerPunch::Job

  def perform(data)
    puts data

  def later(sec, data)
    after(sec) { perform(data) }

Job.new.async.later(60, "asdf") # `perform` will be excuted 60 sec. later

So far sucker punch is my best friend when working with Heroku, since I’m a cheapskate, hahah.

Scheduled Task with Sidekiq and Sidetiq

Sometimes, we need to do scheduled task for our Rails app, for example for caching, sending mass notification, etc. There are multiple solution available, one of them is by using Sidekiq and Sidetiq.

Sidekiq is a gem that enable us to do asynchronous works by creating background workers. What makes it better from other solution like Resque is Sidekiq uses multithreading so it is much more memory efficient than resque. For queuing, Sidekiq requires Redis, so make sure to setup it first.

Now, if you want to make recurring scheduled jobs, we can add Sidetiq on top of Sidekiq. With Sidetiq, you can easily define the interval of the schedule like daily, minutely, etc. Please use ruby > 2.0 when using Sidetiq, since it has a known memory issues when used with ruby 1.9.3 or lower.  You also need to use Redis > 2.6, so check your redis version out. It costs me half a day just to figure that out, hahah.

If you do everything correctly, Sidetiq can give you this beautiful UI:

sidetiq ui

The recurring jobs are only listed in production environment though. This is because in Rails development environment, eager loading is disabled by default.

Ok, hope that helps you in using Sidekiq and Sidetiq. Cheers!