Class: QueueBus::Adapters::Sidekiq

Inherits:
Base
  • Object
show all
Defined in:
lib/sidekiq_bus/adapter.rb

Overview

The sidekiq adapter for queue-bus. It handles enabling, enqueuing, and setting up the heartbeat.

Instance Method Summary collapse

Instance Method Details

#enabled!Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/sidekiq_bus/adapter.rb', line 8

def enabled!
  # know we are using it
  require 'sidekiq'

  # this sidekiq middleware adds in the 'retry' key to the job payload so
  # we ensure sidekiq plays well with resque.
  ::Sidekiq.configure_server do |config|
    config.client_middleware do |chain|
      chain.prepend ::SidekiqBus::Middleware::Client::Retry
    end
  end

  ::QueueBus::Worker.include ::Sidekiq::Worker
end

#enqueue(queue_name, klass, hash) ⇒ Object



27
28
29
30
31
# File 'lib/sidekiq_bus/adapter.rb', line 27

def enqueue(queue_name, klass, hash)
  ::Sidekiq::Client.push('queue' => queue_name,
                         'class' => klass,
                         'args' => [hash])
end

#enqueue_at(epoch_seconds, queue_name, klass, hash) ⇒ Object



33
34
35
36
37
38
# File 'lib/sidekiq_bus/adapter.rb', line 33

def enqueue_at(epoch_seconds, queue_name, klass, hash)
  ::Sidekiq::Client.push('queue' => queue_name,
                         'class' => klass,
                         'args' => [hash],
                         'at' => epoch_seconds)
end

#redis(&block) ⇒ Object



23
24
25
# File 'lib/sidekiq_bus/adapter.rb', line 23

def redis(&block)
  ::Sidekiq.redis(&block)
end

#setup_heartbeat!(queue_name) ⇒ Object

Sets up the heartbeat to be broadcast via sidekiq. Only enable this when you have disabled the resque heart beat schedule as well, as having both may cause issues.

While this will work so long as every time sidekiq boots it triggers this set up. You may consider enabling dynamic schedules to keep all nodes up to date if it ever changes.



47
48
49
50
51
52
53
# File 'lib/sidekiq_bus/adapter.rb', line 47

def setup_heartbeat!(queue_name)
  require 'sidekiq-scheduler'

  ::Sidekiq.configure_server do |config|
    config.on(:startup) { set_schedule(queue_name) }
  end
end