Module: IDRAC::Jobs

Included in:
Client
Defined in:
lib/idrac/jobs.rb

Instance Method Summary collapse

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
70
71
72
# File 'lib/idrac/jobs.rb', line 44

def clear_jobs!
  # Get list of jobs
  jobs_response = authenticated_request(:get, '/redfish/v1/Managers/iDRAC.Embedded.1/Jobs?$expand=*($levels=1)')
  handle_response(jobs_response)
  
  if jobs_response.status == 200
    begin
      jobs_data = JSON.parse(jobs_response.body)
      members = jobs_data["Members"]
      
      # Delete each job individually
      members.each.with_index do |job, i|
        puts "Removing #{job['Id']} : #{job['JobState']} > #{job['Message']} [#{i+1}/#{members.count}]".yellow
        delete_response = authenticated_request(:delete, "/redfish/v1/Managers/iDRAC.Embedded.1/Jobs/#{job['Id']}")
        
        unless delete_response.status.between?(200, 299)
          puts "Warning: Failed to delete job #{job['Id']}. Status code: #{delete_response.status}".yellow
        end
      end
      
      puts "Successfully cleared all jobs".green
      return true
    rescue JSON::ParserError
      raise Error, "Failed to parse jobs response: #{jobs_response.body}"
    end
  else
    raise Error, "Failed to get jobs. Status code: #{jobs_response.status}"
  end
end

#force_clear_jobs!Object

Force clear the job queue



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
132
133
134
135
136
# File 'lib/idrac/jobs.rb', line 75

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, 
    headers: { 'Content-Type' => 'application/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, 
        headers: { 'Content-Type': 'application/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
    error_message = "Failed to force-clear job queue. Status code: #{response.status}"
    
    begin
      error_data = JSON.parse(response.body)
      error_message += ", Message: #{error_data['error']['message']}" if error_data['error'] && error_data['error']['message']
    rescue
      # Ignore JSON parsing errors
    end
    
    raise Error, error_message
  end
end

#jobsObject

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_detailObject

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

#tasksObject

Get system tasks



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/idrac/jobs.rb', line 195

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

Raises:



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
188
189
190
191
192
# File 'lib/idrac/jobs.rb', line 139

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