Class: Quicopt::Job

Inherits:
Object
  • Object
show all
Defined in:
lib/quicopt/client.rb

Overview

A handle to an asynchronous job. result polls until it finishes.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, job_id) ⇒ Job

Returns a new instance of Job.



449
450
451
452
# File 'lib/quicopt/client.rb', line 449

def initialize(client, job_id)
  @client = client
  @job_id = job_id
end

Instance Attribute Details

#clientClient (readonly)

Returns the client that submitted the job.

Returns:

  • (Client)

    the client that submitted the job



445
446
447
# File 'lib/quicopt/client.rb', line 445

def client
  @client
end

#job_idString (readonly)

Returns the server-assigned job id.

Returns:

  • (String)

    the server-assigned job id



447
448
449
# File 'lib/quicopt/client.rb', line 447

def job_id
  @job_id
end

Instance Method Details

#deletenil

Delete the job and its stored blob, result and log on the server.

Returns:

  • (nil)


494
495
496
497
# File 'lib/quicopt/client.rb', line 494

def delete
  @client.request_raw("DELETE", "/v1/jobs/#{@job_id}")
  nil
end

#inspectString

Returns the job id, tagged with the class.

Returns:

  • (String)

    the job id, tagged with the class



500
501
502
# File 'lib/quicopt/client.rb', line 500

def inspect
  "#<Quicopt::Job #{@job_id}>"
end

#logString

Fetch the job's log — the same framed view as display, not a raw solver log.

Returns:

  • (String)


487
488
489
# File 'lib/quicopt/client.rb', line 487

def log
  @client.request_raw("GET", "/v1/jobs/#{@job_id}/log")
end

#result(wait: true, timeout: 120.0, poll: 0.5) ⇒ Result

Fetch the job's result, optionally polling until it is ready.

Parameters:

  • wait (Boolean) (defaults to: true)

    poll past the service's not_done reason until the worker finishes; when false, fetch once

  • timeout (Numeric) (defaults to: 120.0)

    maximum time to poll, in seconds

  • poll (Numeric) (defaults to: 0.5)

    delay between polls, in seconds

Returns:

Raises:

  • (QuicoptError)

    if wait is false and the job is not yet done, on any non-+not_done+ error, or once timeout elapses



471
472
473
474
475
476
477
478
479
480
481
# File 'lib/quicopt/client.rb', line 471

def result(wait: true, timeout: 120.0, poll: 0.5)
  deadline = Process.clock_gettime(Process::CLOCK_MONOTONIC) + timeout
  loop do
    return Result.from_json(@client.request("GET", "/v1/jobs/#{@job_id}/result"))
  rescue QuicoptError => e
    raise if !wait || e.reason != "not_done"
    raise if Process.clock_gettime(Process::CLOCK_MONOTONIC) > deadline

    sleep(poll)
  end
end

#statusHash

Fetch the job's metadata and framed log tail.

Returns:

  • (Hash)

    the job state (+queued+/+running+/+done+/+failed+) and its log tail, as returned by the service



458
459
460
# File 'lib/quicopt/client.rb', line 458

def status
  @client.request("GET", "/v1/jobs/#{@job_id}")
end