From 96537e39ae2faa4c7801423055db26b86f875431 Mon Sep 17 00:00:00 2001 From: Nat Lasseter Date: Mon, 2 Mar 2020 14:30:43 +0000 Subject: Broke apart job running and metric collection. Jobs are now picked up automagically from lib/jobs --- .gitignore | 1 + lib/jobs/slurm_job_states.rb | 26 ++++++++++++++++++++++ lib/longboat.rb | 1 + lib/longboat/collector.rb | 11 ---------- lib/longboat/jobs.rb | 41 +++++++++++++++++++++++++++++++++++ lib/longboat/jobs/slurm_job_states.rb | 31 -------------------------- longboat | 13 +++++++---- 7 files changed, 78 insertions(+), 46 deletions(-) create mode 100644 .gitignore create mode 100644 lib/jobs/slurm_job_states.rb create mode 100644 lib/longboat/jobs.rb delete mode 100644 lib/longboat/jobs/slurm_job_states.rb diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1377554 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.swp diff --git a/lib/jobs/slurm_job_states.rb b/lib/jobs/slurm_job_states.rb new file mode 100644 index 0000000..bc3e8a1 --- /dev/null +++ b/lib/jobs/slurm_job_states.rb @@ -0,0 +1,26 @@ +class SlurmJobStates + def initialize(collector) + @collector = collector + end + + def run + start_time = (Time.now - 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 diff --git a/lib/longboat.rb b/lib/longboat.rb index 8096815..09c9a98 100644 --- a/lib/longboat.rb +++ b/lib/longboat.rb @@ -1 +1,2 @@ +require 'longboat/jobs' require 'longboat/collector' 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 diff --git a/longboat b/longboat index bf2c13d..fb50073 100755 --- a/longboat +++ b/longboat @@ -2,12 +2,17 @@ $LOAD_PATH << './lib' require 'longboat' -require 'longboat/jobs/slurm_job_states' #require 'longboat/server' collector = Longboat::Collector.new -Longboat::Jobs::SlurmJobStates.new(collector) -collector.collect! -puts collector.metrics +jobs = Longboat::Jobs.new +jobs.load(collector) +jobs.collect_every + +loop do + sleep 30 + puts collector.metrics + sleep 30 +end #Longboat::Server.serve! -- cgit v1.2.1