Class: IbmPowerHmc::HmcJob

Inherits:
Object
  • Object
show all
Defined in:
lib/ibm_power_hmc/apis/job.rb

Overview

HMC Job for long running operations.

Defined Under Namespace

Classes: JobFailed, JobNotStarted

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(conn, method_url, operation, group, params = {}) ⇒ HmcJob

Construct a new HMC Job.

Parameters:

  • conn (IbmPowerHmc::Connection)

    The connection to the HMC.

  • method_url (String)

    The method URL.

  • operation (String)

    The name of the requested operation.

  • group (String)

    The name of the group.

  • params (Hash) (defaults to: {})

    The job name/value parameters.



31
32
33
34
35
36
37
# File 'lib/ibm_power_hmc/apis/job.rb', line 31

def initialize(conn, method_url, operation, group, params = {})
  @conn = conn
  @method_url = method_url
  @operation = operation
  @group = group
  @params = params
end

Instance Attribute Details

#last_statusHash (readonly)

Returns The job results returned by the HMC.

Returns:

  • (Hash)

    The job results returned by the HMC.



55
56
57
# File 'lib/ibm_power_hmc/apis/job.rb', line 55

def last_status
  @last_status
end

#resultsHash (readonly)

Returns The job results returned by the HMC.

Returns:

  • (Hash)

    The job results returned by the HMC.



55
56
57
# File 'lib/ibm_power_hmc/apis/job.rb', line 55

def results
  @results
end

Instance Method Details

#deleteObject

Delete the job from the HMC.

Raises:



110
111
112
113
114
115
# File 'lib/ibm_power_hmc/apis/job.rb', line 110

def delete
  raise JobNotStarted unless defined?(@href)

  @conn.request(:delete, @href)
  # Returns HTTP 204 if ok
end

#run(timeout = 120, poll_interval = 0) ⇒ String

Run the job synchronously.

Parameters:

  • timeout (Integer) (defaults to: 120)

    The maximum time in seconds to wait for the job to complete.

  • poll_interval (Integer) (defaults to: 0)

    The interval in seconds between status queries (0 means auto).

Returns:

  • (String)

    The status of the job.



99
100
101
102
103
104
105
# File 'lib/ibm_power_hmc/apis/job.rb', line 99

def run(timeout = 120, poll_interval = 0)
  start
  wait(timeout, poll_interval)
  raise JobFailed.new(@last_status), "Job failed" unless @last_status.status.eql?("COMPLETED_OK")
ensure
  delete if defined?(@href)
end

#startString

Start the job asynchronously.

Returns:

  • (String)

    The URL of the job.



43
44
45
46
47
48
49
50
51
52
# File 'lib/ibm_power_hmc/apis/job.rb', line 43

def start
  headers = {
    :content_type => "application/vnd.ibm.powervm.web+xml; type=JobRequest"
  }
  jobreq = JobRequest.marshal({:operation => @operation, :group => @group, :params => @params}, WEB_XMLNS)
  response = @conn.request(:put, @method_url, headers, jobreq.xml.to_s)
  jobresp = Parser.new(response.body).object(:JobResponse)
  # Save the URL of the job (JobID is not sufficient as not all jobs are in uom).
  @href = jobresp.href.path
end

#statusString

Return the status of the job.

Returns:

  • (String)

    The status of the job.

Raises:



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/ibm_power_hmc/apis/job.rb', line 61

def status
  raise JobNotStarted unless defined?(@href)

  headers = {
    :content_type => "application/vnd.ibm.powervm.web+xml; type=JobRequest"
  }
  response = @conn.request(:get, @href, headers)
  @last_status = Parser.new(response.body).object(:JobResponse)
  @results = @last_status.results
  @last_status.status
end

#wait(timeout = 120, poll_interval = 0) ⇒ String

Wait for the job to complete.

Parameters:

  • timeout (Integer) (defaults to: 120)

    The maximum time in seconds to wait for the job to complete.

  • poll_interval (Integer) (defaults to: 0)

    The interval in seconds between status queries (0 means auto).

Returns:

  • (String)

    The status of the job.



79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/ibm_power_hmc/apis/job.rb', line 79

def wait(timeout = 120, poll_interval = 0)
  endtime = Time.now.utc + timeout
  auto = poll_interval == 0
  poll_interval = 1 if auto
  while Time.now.utc < endtime
    status = self.status
    return status if status != "RUNNING" && status != "NOT_STARTED"

    poll_interval *= 2 if auto && poll_interval < 30
    sleep(poll_interval)
  end
  "TIMEDOUT"
end