diff options
Diffstat (limited to 'lib/longboat')
-rw-r--r-- | lib/longboat/collector.rb | 11 | ||||
-rw-r--r-- | lib/longboat/jobs.rb | 41 | ||||
-rw-r--r-- | lib/longboat/jobs/slurm_job_states.rb | 31 |
3 files changed, 41 insertions, 42 deletions
diff --git a/lib/longboat/collector.rb b/lib/longboat/collector.rb index ccf103b..2b61c8d 100644 --- a/lib/longboat/collector.rb +++ b/lib/longboat/collector.rb @@ -2,7 +2,6 @@ module Longboat class Collector def initialize @metrics = {} - @jobs = [] end def report!(name, value, help: nil, type: nil, labels: {}) @@ -30,15 +29,5 @@ module Longboat end res end - - def register!(job) - @jobs << job - end - - def collect! - @jobs.each do |job| - job.run - end - end end end diff --git a/lib/longboat/jobs.rb b/lib/longboat/jobs.rb new file mode 100644 index 0000000..155b305 --- /dev/null +++ b/lib/longboat/jobs.rb @@ -0,0 +1,41 @@ +module Longboat + class Jobs + def initialize + @jobs = [] + end + + def load(collector) + Dir.entries("./lib/jobs/").each do |file| + next if file =~ /^\./ + + reqname = File.basename(file, ".rb") + cname = reqname.split('_').map(&:capitalize).join + + require "jobs/#{reqname}" + @jobs << Kernel.const_get(cname).new(collector) + end + end + + def collect! + @jobs.each do |job| + job.run + end + end + + def collect_every(time = 60, async = true) + if async + Thread.new do + loop do + collect! + sleep(time) + end + end + else + loop do + collect! + sleep(time) + end + end + end + end +end diff --git a/lib/longboat/jobs/slurm_job_states.rb b/lib/longboat/jobs/slurm_job_states.rb deleted file mode 100644 index 74c7999..0000000 --- a/lib/longboat/jobs/slurm_job_states.rb +++ /dev/null @@ -1,31 +0,0 @@ -module Longboat - module Jobs - class SlurmJobStates - def initialize(collector) - @collector = collector - @collector.register!(self) - end - - def run - start_time = (Time.now - 15 * 60).strftime("%H:%M:%S") - raw = `sacct -a -P -o State -S #{start_time}`.lines.map(&:strip)[1..-1] - - tally = Hash.new{0} - - raw.each do |state| - tally[state] += 1 - end - - tally.each do |state, number| - @collector.report!( - "longboat_slurm_job_states", - number, - help: "Number of jobs in each state", - type: "gauge", - labels: {state: state} - ) - end - end - end - end -end |