Module: Resque::StuckQueue

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

Defined Under Namespace

Modules: Signals Classes: Config, HeartbeatJob

Constant Summary collapse

HEARTBEAT_INTERVAL =

defaults

5 * 60
WATCHER_INTERVAL =

send heartbeat job every 5 minutes

5
TRIGGER_TIMEOUT =

check key is udpated every 5 seconds.

60 * 60
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}") }
LOGGER =
Logger.new($stdout)
HEARTBEAT_KEY =
"resque-stuck-queue"
TRIGGERED_KEY =
"resque-stuck-queue-last-triggered"
VERSION =
"0.4.2"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configObject

Returns the value of attribute config.



19
20
21
# File 'lib/resque_stuck_queue.rb', line 19

def config
  @config
end

Class Method Details

.force_stop!Object



101
102
103
104
105
# File 'lib/resque_stuck_queue.rb', line 101

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

.heartbeat_key_for(queue) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/resque_stuck_queue.rb', line 33

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

.heartbeat_keysObject



49
50
51
# File 'lib/resque_stuck_queue.rb', line 49

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

.loggerObject



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

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

.queuesObject



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

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

.redisObject



29
30
31
# File 'lib/resque_stuck_queue.rb', line 29

def redis
  @redis ||= config[:redis]
end

.reset!Object



107
108
109
110
111
112
113
# File 'lib/resque_stuck_queue.rb', line 107

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_keysObject



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

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

.startObject

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



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

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

  Signals.enable!

  log_starting_info

  reset_keys

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

  pretty_process_name

  setup_heartbeat_thread
  setup_watcher_thread

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

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

.start_in_backgroundObject



57
58
59
60
61
62
# File 'lib/resque_stuck_queue.rb', line 57

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

.stopObject



92
93
94
95
96
97
98
99
# File 'lib/resque_stuck_queue.rb', line 92

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

.stopped?Boolean

Returns:

  • (Boolean)


122
123
124
# File 'lib/resque_stuck_queue.rb', line 122

def stopped?
  @stopped
end

.triggered_key_for(queue) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/resque_stuck_queue.rb', line 41

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