3 # Keep node.info[:running_job_uuid] and node.info[:slurm_state] up to date.
5 # use: script/update_node_attributes.rb [rails_env] [update_interval]
6 # example: script/update_node_attributes.rb production 10
8 ENV["RAILS_ENV"] = ARGV[0] || "development"
9 @update_interval = ARGV[1] ? ARGV[1].to_i : 5
11 require File.dirname(__FILE__) + '/../config/boot'
12 require File.dirname(__FILE__) + '/../config/environment'
14 include ApplicationHelper
18 @running_job_uuid = {}
21 IO.popen('sinfo --noheader --Node || true').readlines.each do |line|
22 tokens = line.strip.split
23 nodestate = tokens.last.downcase
26 if (re = tokens.first.match /^([^\[]*)\[([-\d,]+)\]$/)
28 re[2].split(',').each do |number_range|
29 if number_range.index('-')
30 range = number_range.split('-').collect(&:to_i)
31 (range[0]..range[1]).each do |n|
32 nodenames << "#{nodeprefix}#{n}"
35 nodenames << "#{nodeprefix}#{number_range}"
39 nodenames << tokens.first
42 nodenames.each do |nodename|
43 if @slurm_state[nodename] != nodestate
44 has_no_job = ! ['alloc','comp'].index(nodestate)
46 where('slot_number=? and hostname=?',
47 nodename.match(/(\d+)$/)[1].to_i,
50 raise "Fatal: Node does not exist: #{nodename}" if !node
52 puts "Node #{node.uuid} slot #{node.slot_number} name #{node.hostname} state #{nodestate}#{' (has_no_job)' if has_no_job}"
53 node_info_was = node.info.dup
54 node.info[:slurm_state] = nodestate
55 node.info[:running_job_uuid] = nil if has_no_job
56 if node_info_was != node.info and not node.save
57 raise "Fail: update node #{node.uuid} state #{nodestate}"
59 @slurm_state[nodename] = nodestate
64 IO.popen('squeue --noheader --format="%j %t %N" || true').readlines.each do |line|
65 tokens = line.strip.split
66 running_job_uuid = tokens.first
69 if (re = tokens.last.match /^([^\[]*)\[([-\d,]+)\]$/)
71 re[2].split(',').each do |number_range|
72 if number_range.index('-')
73 range = number_range.split('-').collect(&:to_i)
74 (range[0]..range[1]).each do |n|
75 nodenames << "#{nodeprefix}#{n}"
78 nodenames << "#{nodeprefix}#{number_range}"
82 nodenames << tokens.first
85 nodenames.each do |nodename|
86 if @running_job_uuid[nodename] != running_job_uuid
88 where('slot_number=? and hostname=?',
89 nodename.match(/(\d+)$/)[1].to_i,
92 raise "Fatal: Node does not exist: #{nodename}" if !node
93 puts "Node #{node.uuid} slot #{node.slot_number} name #{node.hostname} running_job_uuid #{running_job_uuid}"
94 if node.info[:running_job_uuid] != running_job_uuid
95 node.info[:running_job_uuid] = running_job_uuid
97 raise "Fail: update node #{node.uuid} running_job_uuid #{running_job_uuid}"
100 @running_job_uuid[nodename] = running_job_uuid
105 sleep @update_interval