Class: Notion::Generated::Endpoints::AsyncTasks

Inherits:
Endpoints::Base show all
Defined in:
lib/notion/endpoints/async_tasks.rb,
lib/notion/generated/endpoints/async_tasks.rb

Constant Summary collapse

TERMINAL =
%w[success completed failed canceled cancelled].freeze

Instance Method Summary collapse

Methods inherited from Endpoints::Base

#initialize, operation, operations

Constructor Details

This class inherits a constructor from Notion::Endpoints::Base

Instance Method Details

#retrieve(task_id:, **params, &block) ⇒ Object

Retrieve an async task



13
14
15
16
# File 'lib/notion/generated/endpoints/async_tasks.rb', line 13

def retrieve(task_id:, **params, &block)
    params[:task_id] = task_id
  invoke_operation(:retrieve, params, &block)
end

#wait(task_id:, timeout: 300, interval: 1, sleeper: Kernel.method(:sleep)) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/notion/endpoints/async_tasks.rb', line 9

def wait(task_id:, timeout: 300, interval: 1, sleeper: Kernel.method(:sleep))
  deadline = Process.clock_gettime(Process::CLOCK_MONOTONIC) + timeout
  attempt = 0
  loop do
    task = retrieve(task_id: task_id)
    status = task.status.to_s
    raise Error, "async task #{status}" if status.match?(/fail|cancel/)
    return task if TERMINAL.include?(status)
    raise TimeoutError, "async task timed out" if Process.clock_gettime(Process::CLOCK_MONOTONIC) >= deadline

    sleeper.call(interval == :exponential ? [2**attempt, 30].min : interval)
    attempt += 1
  end
end