Class: Riveter::Resources::Runs

Inherits:
Object
  • Object
show all
Defined in:
lib/riveter/resources.rb

Overview

The uniform lifecycle for every async operation — status, results, stop.

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Runs

Returns a new instance of Runs.



7
8
9
# File 'lib/riveter/resources.rb', line 7

def initialize(client)
  @client = client
end

Instance Method Details

#get(run_id) ⇒ Object

Status and progress of a run. (operationId: getRun)



12
13
14
# File 'lib/riveter/resources.rb', line 12

def get(run_id)
  Run.new(@client.request(:get, "/runs/#{run_id}"))
end

#list(**params) ⇒ Object

List the account's runs, newest first. (operationId: listRuns)



29
30
31
32
# File 'lib/riveter/resources.rb', line 29

def list(**params)
  fetch_page = ->(page) { @client.request(:get, "/runs", query: params.merge(page: page)) }
  RunsPage.new(fetch_page.call(params[:page] || 1), fetch_page)
end

#result(run_id, wait: nil) ⇒ Object

The run plus its #output (nil until finished). wait long-polls up to 50 seconds server-side. (operationId: getRunResult)



18
19
20
21
# File 'lib/riveter/resources.rb', line 18

def result(run_id, wait: nil)
  timeout = wait ? [@client.timeout, wait + 10].max : nil
  Run.new(@client.request(:get, "/runs/#{run_id}/result", query: { wait: wait }, timeout: timeout))
end

#stop(run_id) ⇒ Object

Stop a run early. (operationId: stopRun)



24
25
26
# File 'lib/riveter/resources.rb', line 24

def stop(run_id)
  Run.new(@client.request(:post, "/runs/#{run_id}/stop"))
end

#summaryObject

All-time run counts by status. (operationId: runsSummary)



35
36
37
# File 'lib/riveter/resources.rb', line 35

def summary
  RunsSummary.new(@client.request(:get, "/runs/summary"))
end

#wait_for_result(run_id, timeout: 600, poll_wait: 50) ⇒ Object

Polls #result with 50s long-polls until the run reaches success or stopped; raises APITimeoutError past the timeout (seconds).



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/riveter/resources.rb', line 41

def wait_for_result(run_id, timeout: 600, poll_wait: 50)
  deadline = Process.clock_gettime(Process::CLOCK_MONOTONIC) + timeout
  loop do
    run = result(run_id, wait: poll_wait)
    return run if run.finished?

    if Process.clock_gettime(Process::CLOCK_MONOTONIC) >= deadline
      raise APITimeoutError, "Run #{run_id} did not finish within #{timeout}s"
    end
  end
end