aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNat Lasseter <nat.lasseter@york.ac.uk>2020-03-02 14:30:43 +0000
committerNat Lasseter <nat.lasseter@york.ac.uk>2020-03-02 14:30:43 +0000
commit96537e39ae2faa4c7801423055db26b86f875431 (patch)
tree700ccc30e5157c0aeac571b0e8f0e04cc6a5a713
parentcd0f6b103553f547bc21a8447e785c31f7ce858c (diff)
Broke apart job running and metric collection. Jobs are now picked up automagically from lib/jobs
-rw-r--r--.gitignore1
-rw-r--r--lib/jobs/slurm_job_states.rb26
-rw-r--r--lib/longboat.rb1
-rw-r--r--lib/longboat/collector.rb11
-rw-r--r--lib/longboat/jobs.rb41
-rw-r--r--lib/longboat/jobs/slurm_job_states.rb31
-rwxr-xr-xlongboat13
7 files changed, 78 insertions, 46 deletions
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!