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.8"

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



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

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

.global_key ⇒ Object



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

def global_key
  # public, for use in custom heartbeat job
  config[:global_key] || GLOBAL_KEY
end

.logger ⇒ Object



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

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

.redis ⇒ Object



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

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

.reset! ⇒ Object



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

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

.start ⇒ Object

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



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/resque_stuck_queue.rb', line 61

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



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

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

.stop ⇒ Object



79
80
81
82
83
84
85
86
# File 'lib/resque_stuck_queue.rb', line 79

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

.stopped? ⇒ Boolean

Returns:

  • (Boolean)


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

def stopped?
  @stopped
end