Module: Resque::StuckQueue

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

Constant Summary collapse

GLOBAL_KEY =
"resque-stuck-queue"
HEARTBEAT =

check/refresh every hour

60 * 60
TRIGGER_TIMEOUT =

warn/trigger 5 hours

5 * 60 * 60
HANDLER =
proc { $stdout.puts("Shit gone bad with them queues.") }
VERSION =
"0.0.10"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.config ⇒ Object

how often we refresh the key

:heartbeat = 5 * 60

this could just be :heartbeat but it's possible there's an acceptable lag/bottleneck

in the queue that we want to allow to be before we think it's bad.

:trigger_timeout = 10 * 60

The global key that will be used to check the latest time

:global_key = "resque-stuck-queue"

for threads involved here. default is false

:abort_on_exception

default handler

config = proc { send_mail }

explicit redis

config = Redis.new



41
42
43
# File 'lib/resque_stuck_queue.rb', line 41

def config
  @config
end

Class Method Details

.force_stop! ⇒ Object



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

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

.global_key_for(under_queue) ⇒ Object



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

def global_key_for(under_queue)
  "#{under_queue}:#{config[:global_key] || GLOBAL_KEY}"
end

.global_keys ⇒ Object



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

def global_keys
  queues.map{|q| global_key_for(q) }
end

.logger ⇒ Object



45
46
47
# File 'lib/resque_stuck_queue.rb', line 45

def logger
  @logger ||= (config[:logger] || Logger.new($stdout))
end

.queues ⇒ Object



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

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

.redis ⇒ Object



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

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

.reset! ⇒ Object



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

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

.start ⇒ Object

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



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/resque_stuck_queue.rb', line 73

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

  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



65
66
67
68
69
70
# File 'lib/resque_stuck_queue.rb', line 65

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

.stop ⇒ Object



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

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

.stopped? ⇒ Boolean

Returns:

  • (Boolean)


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

def stopped?
  @stopped
end