Enqueue into Sidekiq via pure Redis (without loading sidekiq)

We want to enqueue jobs, but do not want to blow up the app with sidekiq and it’s dependencies.

Usage

RawSidekiq.enqueue("XyzJob", [1,2,3], :namespace => "custom")

Code

# https://grosser.it/2013/01/17/enqueue-into-sidekiq-via-pure-redis-without-loading-sidekiq
require "json"
require "redis"
require "securerandom"

class RawSidekiq
  def self.enqueue(queue, klass, args, options={})
    payload = {
      'class' => klass,
      'args' => args,
      'jid' => SecureRandom.hex(12),
      #'retry' => true
    }.to_json

    conn = Redis.new
    conn.multi do
      conn.sadd([options[:namespace], "queues"].compact.join(":"), queue)
      conn.lpush([options[:namespace], "queue", queue].compact.join(":"), payload)
    end
  end
end
Advertisements

2 thoughts on “Enqueue into Sidekiq via pure Redis (without loading sidekiq)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s