Module: IDRAC::Jobs
- Included in:
- Client
- Defined in:
- lib/idrac/jobs.rb
Constant Summary collapse
- JOB_TERMINAL_STATES =
Job states that mean the job is finished, one way or another.
%w[Completed CompletedWithErrors Failed RebootFailed].freeze
Instance Method Summary collapse
-
#clear_jobs! ⇒ Object
Clear all jobs from the job queue.
-
#force_clear_jobs! ⇒ Object
Force clear the job queue.
-
#jobs ⇒ Object
Summarize jobs.
-
#jobs_detail ⇒ Object
Get detailed job information.
-
#tasks ⇒ Object
Get system tasks.
-
#wait_for_job(job_id) ⇒ Object
Wait for a job to complete.
-
#wait_for_job_completion(job_id, timeout: 600, interval: 10) ⇒ Object
Wait for an iDRAC job to reach a terminal state and report on the JOB itself.
Instance Method Details
#clear_jobs! ⇒ Object
Clear all jobs from the job queue
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/idrac/jobs.rb', line 44 def clear_jobs! jobs_response = authenticated_request(:get, '/redfish/v1/Managers/iDRAC.Embedded.1/Jobs?$expand=*($levels=1)') return true unless jobs_response.status == 200 jobs_data = JSON.parse(jobs_response.body) members = jobs_data["Members"] || [] if members.empty? puts "No jobs to clear.".yellow return true end puts "Clearing #{members.length} jobs...".yellow members.each_with_index do |job, i| puts "Removing #{job['Id']} : #{job['JobState']} > #{job['Message']} [#{i+1}/#{members.count}]".yellow response = authenticated_request(:delete, "/redfish/v1/Managers/iDRAC.Embedded.1/Jobs/#{job['Id']}") unless response.status.between?(200, 299) puts "Warning: Failed to delete job #{job['Id']}. Status code: #{response.status}".yellow end end puts "Successfully cleared all jobs".green true end |
#force_clear_jobs! ⇒ Object
Force clear the job queue
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/idrac/jobs.rb', line 72 def force_clear_jobs! # Clear the job queue using force option which will also clear any pending data and restart processes path = '/redfish/v1/Dell/Managers/iDRAC.Embedded.1/DellJobService/Actions/DellJobService.DeleteJobQueue' payload = { "JobID" => "JID_CLEARALL_FORCE" } response = authenticated_request( :post, path, body: payload.to_json ) if response.status.between?(200, 299) puts "Successfully force-cleared job queue".green # Monitor LC status until it's Ready puts "Waiting for LC status to be Ready..." retries = 60 # ~10 minutes with 10s sleep while retries > 0 lc_response = authenticated_request( :post, '/redfish/v1/Dell/Managers/iDRAC.Embedded.1/DellLCService/Actions/DellLCService.GetRemoteServicesAPIStatus', body: {}.to_json ) if lc_response.status.between?(200, 299) begin lc_data = JSON.parse(lc_response.body) status = lc_data["LCStatus"] if status == "Ready" puts "LC Status is Ready".green return true end puts "Current LC Status: #{status}. Waiting..." rescue JSON::ParserError puts "Failed to parse LC status response, will retry...".yellow end end retries -= 1 sleep 10 end puts "Warning: LC status did not reach Ready state within timeout".yellow return true else = "Failed to force-clear job queue. Status code: #{response.status}" begin error_data = JSON.parse(response.body) += ", Message: #{error_data['error']['message']}" if error_data['error'] && error_data['error']['message'] rescue # Ignore JSON parsing errors end raise Error, end end |
#jobs ⇒ Object
Summarize jobs
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/idrac/jobs.rb', line 7 def jobs response = authenticated_request(:get, '/redfish/v1/Managers/iDRAC.Embedded.1/Jobs?$expand=*($levels=1)') if response.status == 200 begin jobs_data = JSON.parse(response.body) { completed_count: jobs_data["Members"].select { |j| j["JobState"] == "Completed" }.count, incomplete_count: jobs_data["Members"].select { |j| j["JobState"] != "Completed" }.count, total_count: jobs_data["Members"].count } rescue JSON::ParserError raise Error, "Failed to parse jobs response: #{response.body}" end else raise Error, "Failed to get jobs. Status code: #{response.status}" end end |
#jobs_detail ⇒ Object
Get detailed job information
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/idrac/jobs.rb', line 25 def jobs_detail response = authenticated_request(:get, '/redfish/v1/Managers/iDRAC.Embedded.1/Jobs?$expand=*($levels=1)') if response.status == 200 begin jobs_data = JSON.parse(response.body) jobs_data["Members"].each.with_index do |job, i | puts "#{job['Id']} : #{job['JobState']} > #{job['Message']} <#{job['CompletionTime']}> [#{i+1}/#{jobs_data["Members"].count}]" end return jobs_data rescue JSON::ParserError raise Error, "Failed to parse jobs detail response: #{response.body}" end else raise Error, "Failed to get jobs detail. Status code: #{response.status}" end end |
#tasks ⇒ Object
Get system tasks
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 |
# File 'lib/idrac/jobs.rb', line 238 def tasks response = authenticated_request(:get, '/redfish/v1/TaskService/Tasks') if response.status == 200 begin tasks_data = JSON.parse(response.body) # "Tasks: #{tasks_data['Members'].count}", 0 return tasks_data['Members'] rescue JSON::ParserError raise Error, "Failed to parse tasks response: #{response.body}" end else raise Error, "Failed to get tasks. Status code: #{response.status}" end end |
#wait_for_job(job_id) ⇒ Object
Wait for a job to complete
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/idrac/jobs.rb', line 134 def wait_for_job(job_id) # Job ID can be a job ID, path, or response hash from another request job_path = if job_id.is_a?(Hash) if job_id['headers'] && job_id['headers']['location'] job_id['headers']['location'].sub(/^\/redfish\/v1\//, '') else raise Error, "Invalid job hash, missing location header" end elsif job_id.to_s.start_with?('/redfish/v1/') job_id.sub(/^\/redfish\/v1\//, '') else "Managers/iDRAC.Embedded.1/Jobs/#{job_id}" end puts "Waiting for job to complete: #{job_id}".light_cyan retries = 36 # ~6 minutes with 10s sleep while retries > 0 response = authenticated_request(:get, "/redfish/v1/#{job_path}") if response.status == 200 begin job_data = JSON.parse(response.body) job_state = job_data["JobState"] case job_state when "Completed" puts "Job completed successfully".green puts "CompletionTime: #{job_data['CompletionTime']}".green if job_data['CompletionTime'] return job_data when "Failed" puts "Job failed: #{job_data['Message']}".red puts "CompletionTime: #{job_data['CompletionTime']}".red if job_data['CompletionTime'] raise Error, "Job failed: #{job_data['Message']}" when "CompletedWithErrors" puts "Job completed with errors: #{job_data['Message']}".yellow puts "CompletionTime: #{job_data['CompletionTime']}".yellow if job_data['CompletionTime'] return job_data end puts "Job state: #{job_state}. Waiting...".yellow rescue JSON::ParserError puts "Failed to parse job status response, will retry...".yellow end else puts "Failed to get job status. Status code: #{response.status}".red end retries -= 1 sleep 10 end raise Error, "Timeout waiting for job to complete" end |
#wait_for_job_completion(job_id, timeout: 600, interval: 10) ⇒ Object
Wait for an iDRAC job to reach a terminal state and report on the JOB itself.
Unlike wait_for_job this does not raise for a job that simply failed -- it returns the outcome so the caller can log or re-check it. The hash always carries :job_id.
{ status: :success | :failed | :timeout, job_id:, job_state:,
message:, messages: [message], job: <raw job data>,
error: <the job's own message, on anything but :success> }
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 |
# File 'lib/idrac/jobs.rb', line 200 def wait_for_job_completion(job_id, timeout: 600, interval: 10) job_id = job_id.to_s.split("/").last deadline = Time.now + timeout state = nil last_error = nil while Time.now < deadline begin response = authenticated_request(:get, "/redfish/v1/Managers/iDRAC.Embedded.1/Jobs/#{job_id}") job = response.status == 200 ? JSON.parse(response.body) : nil state = job ? job["JobState"] : nil if JOB_TERMINAL_STATES.include?(state) = job["Message"] || Array(job["Messages"]).map { |m| m["Message"] }.compact.first success = state == "Completed" debug "Job #{job_id} #{state}: #{}", 1, success ? :green : :red result = { status: success ? :success : :failed, job_id: job_id, job_state: state, message: , messages: [].compact, job: job } result[:error] = || "Job #{job_id} finished with state #{state}" unless success return result end last_error = "HTTP #{response.status}" unless job debug "Job #{job_id} state: #{state || last_error}. Waiting...", 1, :yellow rescue StandardError => e # An SCP import can bounce the host, so a job can be briefly unreadable. Keep polling. last_error = e. debug "Job #{job_id} not readable (#{e.}). Waiting...", 1, :yellow end sleep interval end { status: :timeout, job_id: job_id, job_state: state, error: "Timed out after #{timeout}s waiting for job #{job_id} (last state: #{state || last_error || 'unknown'})" } end |