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, time_limit = nil) ⇒ Job

Returns a new instance of Job.



556
557
558
559
560
# File 'lib/quicopt/client.rb', line 556

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

Instance Attribute Details

#client ⇒ Client (readonly)

Returns the client that submitted the job.

Returns:

  • (Client) —

    the client that submitted the job



550
551
552
# File 'lib/quicopt/client.rb', line 550

def client
  @client
end

#job_id ⇒ String (readonly)

Returns the server-assigned job id.

Returns:

  • (String) —

    the server-assigned job id



552
553
554
# File 'lib/quicopt/client.rb', line 552

def job_id
  @job_id
end

#time_limit ⇒ Float? (readonly)

Returns the time limit the job was submitted with, if any.

Returns:

  • (Float, nil) —

    the time limit the job was submitted with, if any



554
555
556
# File 'lib/quicopt/client.rb', line 554

def time_limit
  @time_limit
end

Instance Method Details

#delete ⇒ nil

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

Returns:

  • (nil)


605
606
607
608
# File 'lib/quicopt/client.rb', line 605

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

#inspect ⇒ String

Returns the job id, tagged with the class.

Returns:

  • (String) —

    the job id, tagged with the class



611
612
613
# File 'lib/quicopt/client.rb', line 611

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

#log ⇒ String

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

Returns:

  • (String)


598
599
600
# File 'lib/quicopt/client.rb', line 598

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

#result(wait: true, timeout: nil, 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, nil) (defaults to: nil) —

    maximum time to poll, in seconds. Defaults to 120 s, or the job's time_limit plus a minute when that is longer.

  • 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



581
582
583
584
585
586
587
588
589
590
591
592
# File 'lib/quicopt/client.rb', line 581

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

    sleep(poll)
  end
end

#status ⇒ Hash

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



567
568
569
# File 'lib/quicopt/client.rb', line 567

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