Module: Resque::StuckQueue

Defined in:
lib/resque_stuck_queue.rb,
lib/resque_stuck_queue/config.rb,
lib/resque_stuck_queue/version.rb

Defined Under Namespace

Classes: Config

Constant Summary collapse

HEARTBEAT_KEY =

defaults

"resque-stuck-queue"
TRIGGERED_KEY =
"resque-stuck-queue-last-triggered"
HEARTBEAT_TIMEOUT =

check/refresh every 20 mins.

20 * 60
TRIGGER_TIMEOUT =

warn/trigger after an hour (with 20 min heartbeat time).

40 * 60
LOGGER =
Logger.new($stdout)
TRIGGERED_HANDLER =

must be called by convention: type_handler

proc { |queue_name, lag| Resque::StuckQueue::LOGGER.info("Shit gone bad with them queues...on #{queue_name}. Lag time is #{lag}") }
RECOVERED_HANDLER =
proc { |queue_name, lag| Resque::StuckQueue::LOGGER.info("recovered queue phew #{queue_name}. Lag time is #{lag}") }
VERSION =
"0.1.1"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.config ⇒ Object

Returns the value of attribute config.



16
17
18
# File 'lib/resque_stuck_queue.rb', line 16

def config
  @config
end

Class Method Details

.force_stop! ⇒ Object



97
98
99
100
101
# File 'lib/resque_stuck_queue.rb', line 97

def force_stop!
  logger.info("Force stopping")
  @threads.map(&:kill)
  reset!
end

.heartbeat_key_for(queue) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/resque_stuck_queue.rb', line 36

def heartbeat_key_for(queue)
  if config[:heartbeat_key]
    "#{queue}:#{config[:heartbeat_key]}"
  else
    "#{queue}:#{HEARTBEAT_KEY}"
  end
end

.heartbeat_keys ⇒ Object



52
53
54
# File 'lib/resque_stuck_queue.rb', line 52

def heartbeat_keys
  queues.map{|q| heartbeat_key_for(q) }
end

.logger ⇒ Object



22
23
24
# File 'lib/resque_stuck_queue.rb', line 22

def logger
  @logger ||= (config[:logger] || StuckQueue::LOGGER)
end

.queues ⇒ Object



56
57
58
# File 'lib/resque_stuck_queue.rb', line 56

def queues
  @queues ||= (config[:queues] || [:app])
end

.redis ⇒ Object



26
27
28
# File 'lib/resque_stuck_queue.rb', line 26

def redis
  @redis ||= (config[:redis] || Resque.redis)
end

.redis=(rds) ⇒ Object



30
31
32
33
34
# File 'lib/resque_stuck_queue.rb', line 30

def redis=(rds)
  # for resq2 tests
  @redis = rds
  Resque.redis = @redis
end

.reset! ⇒ Object



103
104
105
106
107
108
109
# File 'lib/resque_stuck_queue.rb', line 103

def reset!
  # clean state so we can stop and start in the same process.
  @config = Config.new # clear, unfreeze
  @queues = nil
  @running = false
  @logger = nil
end

.reset_keys ⇒ Object



111
112
113
114
115
116
# File 'lib/resque_stuck_queue.rb', line 111

def reset_keys
  queues.each do |qn|
    redis.del(heartbeat_key_for(qn))
    redis.del(triggered_key_for(qn))
  end
end

.start ⇒ Object

call this after setting config. once started you should't be allowed to modify it



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/resque_stuck_queue.rb', line 68

def start
  @running = true
  @stopped = false
  @threads = []
  config.freeze

  reset_keys

  Redis::Classy.db = redis if Redis::Classy.db.nil?

  enqueue_repeating_refresh_job
  setup_checker_thread

  # fo-eva.
  @threads.map(&:join)

  logger.info("threads stopped")
  @stopped = true
end

.start_in_background ⇒ Object



60
61
62
63
64
65
# File 'lib/resque_stuck_queue.rb', line 60

def start_in_background
  Thread.new do
    Thread.current.abort_on_exception = config[:abort_on_exception]
    self.start
  end
end

.stop ⇒ Object



88
89
90
91
92
93
94
95
# File 'lib/resque_stuck_queue.rb', line 88

def stop
  reset!
  # wait for clean thread shutdown
  while @stopped == false
    sleep 1
  end
  logger.info("Stopped")
end

.stopped? ⇒ Boolean

Returns:

  • (Boolean)


118
119
120
# File 'lib/resque_stuck_queue.rb', line 118

def stopped?
  @stopped
end

.triggered_key_for(queue) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/resque_stuck_queue.rb', line 44

def triggered_key_for(queue)
  if config[:triggered_key]
    "#{queue}:#{config[:triggered_key]}"
  else
    "#{queue}:#{TRIGGERED_KEY}"
  end
end