-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: false overall metric + refactoring
- Loading branch information
1 parent
d3be980
commit eb438e4
Showing
5 changed files
with
198 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
module Resque | ||
module Integration | ||
class QueuesInfo | ||
class Age | ||
def initialize(config) | ||
@config = config | ||
end | ||
|
||
def time(queue) | ||
from_time = Time.now.utc | ||
max_secs = 0 | ||
|
||
jobs.each do |job| | ||
next unless job['queue'] == queue | ||
job_secs = seconds_for(job, from_time) | ||
max_secs = job_secs if job_secs > max_secs | ||
end | ||
|
||
max_secs | ||
end | ||
|
||
def overall | ||
from_time = Time.now.utc | ||
|
||
max_secs = 0 | ||
|
||
jobs.each do |job| | ||
next unless job['queue'] | ||
job_secs = seconds_for(job, from_time) | ||
next if job_secs < threshold(job['queue']) | ||
max_secs = job_secs if job_secs > max_secs | ||
end | ||
|
||
max_secs | ||
end | ||
|
||
def threshold(queue) | ||
@config.max_age(queue) | ||
end | ||
|
||
private | ||
|
||
def jobs | ||
Resque.workers.each_with_object([]) { |worker, memo| memo << worker.job unless worker.idle? } | ||
end | ||
|
||
def seconds_for(job, from_time) | ||
(from_time - DateTime.strptime(job['run_at'], '%Y-%m-%dT%H:%M:%S').utc).to_i | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
module Resque | ||
module Integration | ||
class QueuesInfo | ||
class Config | ||
def initialize(config_path) | ||
config = load_config(config_path) | ||
@defaults = config['defaults'] | ||
@queues = expand_config(config['queues']) | ||
end | ||
|
||
def max_age(queue) | ||
threshold(queue, 'max_age') | ||
end | ||
|
||
def max_size(queue) | ||
threshold(queue, 'max_size') | ||
end | ||
|
||
def data | ||
@data ||= @queues.map do |k, v| | ||
{ | ||
"{#QUEUE}" => k | ||
} | ||
end | ||
end | ||
|
||
private | ||
|
||
def threshold(queue, param) | ||
(@queues[queue] || @defaults)[param] | ||
end | ||
|
||
def load_config(path) | ||
input = File.read(path) | ||
input = ERB.new(input).result if defined?(ERB) | ||
YAML.load(input) | ||
end | ||
|
||
def expand_config(config) | ||
keys = config.keys.dup | ||
|
||
keys.each do |key| | ||
v = config.delete(key) | ||
|
||
key.split(',').each do |queue| | ||
queue.chomp! | ||
queue.strip! | ||
config[queue] = v | ||
end | ||
end | ||
|
||
config | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
module Resque | ||
module Integration | ||
class QueuesInfo | ||
class Size | ||
def initialize(config) | ||
@config = config | ||
end | ||
|
||
def size(queue) | ||
Resque.size(queue) || 0 | ||
end | ||
|
||
def overall | ||
max = 0 | ||
|
||
Resque.queues.each do |queue| | ||
size = Resque.size(queue).to_i | ||
next if size < threshold(queue) | ||
max = size if size > max | ||
end | ||
|
||
max | ||
end | ||
|
||
private | ||
|
||
def threshold(queue) | ||
@config.max_size(queue) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters