Module: Vizir
- Defined in:
- lib/vizir.rb
Defined Under Namespace
Classes: Job
Constant Summary collapse
- APIURL =
"https://localhost:8080/oargridapi"
- IGNORED_SITES =
['grenoble-exp', 'grenoble-ext', 'grenoble-obs', 'luxembourg', 'portoalegre']
- FIRST_ALERT_TIME =
600
Class Method Summary collapse
- .alert_jobs(api) ⇒ Object
- .first_ending_job ⇒ Object
- .get(api, uri) ⇒ Object
- .get_api(uri) ⇒ Object
- .get_remaining_time(end_time, now) ⇒ Object
- .humanize_time(time) ⇒ Object
- .learn_new_jobs(api) ⇒ Object
- .learn_new_jobs_on_site(api, site) ⇒ Object
- .learn_sites(api, ignored_sites) ⇒ Object
Class Method Details
.alert_jobs(api) ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/vizir.rb', line 96 def Vizir.alert_jobs(api) $jobs.each do |jobid, job| if job.should_be_ending? # Check if the job still exists before sending a notification if job.is_ended?(api) # Job is not running anymore, remove it from the hash $jobs.delete(jobid) next end remaining_sec = Vizir.get_remaining_time(job.end_time, Time.now) remaining_time = Vizir.humanize_time(remaining_sec) notify_via_growl(jobid, job.site_name, remaining_time) end end end |
.first_ending_job ⇒ Object
92 93 94 |
# File 'lib/vizir.rb', line 92 def Vizir.first_ending_job $jobs.sort{|a,b| a[1].end_time <=> b[1].end_time}.first end |
.get(api, uri) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/vizir.rb', line 34 def Vizir.get(api, uri) begin return JSON.parse(api[uri + '?structure=simple'].get(:accept => 'application/json')) rescue RestClient::RequestTimeout => e # This should really be elsewhere, but will do for now # Were we querying a specific site? If yes, remove it from $sites if uri =~ /\/sites\/(\w+)/ $sites.delete_if { |site| site['site'] == "#{$1}" } return nil else raise e end rescue => e if e.respond_to?('http_code') puts "Error: #{e.http_code}:\n #{e.response.body}" return nil else puts e.inspect exit 1 end end end |
.get_api(uri) ⇒ Object
61 62 63 |
# File 'lib/vizir.rb', line 61 def Vizir.get_api(uri) return RestClient::Resource.new(uri, :timeout => 15) end |
.get_remaining_time(end_time, now) ⇒ Object
30 31 32 |
# File 'lib/vizir.rb', line 30 def Vizir.get_remaining_time(end_time, now) return (end_time - now).round end |
.humanize_time(time) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/vizir.rb', line 9 def Vizir.humanize_time(time) return "now" if time <= 0 time_units = [] minutes = time / 60 if minutes != 0 s = "#{minutes} minute" s += "s" if minutes > 1 time_units.push(s) end seconds = time % 60 if seconds != 0 s = "#{seconds} second" s += "s" if seconds > 1 time_units.push(s) end return "in " + time_units.join(" and ") end |
.learn_new_jobs(api) ⇒ Object
86 87 88 89 90 |
# File 'lib/vizir.rb', line 86 def Vizir.learn_new_jobs(api) $sites.each do |site| Vizir.learn_new_jobs_on_site(api, site) end end |
.learn_new_jobs_on_site(api, site) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/vizir.rb', line 65 def Vizir.learn_new_jobs_on_site(api, site) site_name = site['site'] jobs = Vizir.get(api, "#{site['uri']}/jobs") return if jobs.nil? jobs.each do |job| if job['owner'] == $login job_details = Vizir.get(api, "#{job['uri']}") break if job_details == nil if job_details['jobType'] == 'INTERACTIVE' jobid = job_details['Job_Id'] # If we don't yet know the job, record it in $jobs if $jobs[jobid].nil? ending_time = Time.at(Integer(job_details['startTime']) + Integer(job_details['walltime'])) $jobs[jobid] = Vizir::Job.new(jobid, ending_time, site_name.capitalize) $log.info("learned job #{jobid} in #{site_name.capitalize} ending on #{ending_time.strftime('%b %d')} at #{ending_time.strftime('%H:%M:%S')}") end end end end end |
.learn_sites(api, ignored_sites) ⇒ Object
57 58 59 |
# File 'lib/vizir.rb', line 57 def Vizir.learn_sites(api, ignored_sites) return get(api, '/sites').delete_if { |hash| ignored_sites.include?(hash["site"]) } end |