Class: Scrapio::Resources::Jobs

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

Constant Summary collapse

TERMINAL =
%w[completed partial failed cancelled].freeze

Instance Method Summary collapse

Constructor Details

#initialize(http) ⇒ Jobs

Returns a new instance of Jobs.



6
# File 'lib/scrapio/resources/jobs.rb', line 6

def initialize(http) = @http = http

Instance Method Details

#create(job_type:, payload:, webhook_url: nil) ⇒ Object



8
9
10
# File 'lib/scrapio/resources/jobs.rb', line 8

def create(job_type:, payload:, webhook_url: nil)
  @http.post("/v1/jobs", { job_type: job_type, payload: payload, webhook_url: webhook_url })
end

#get(job_id) ⇒ Object



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

def get(job_id)
  @http.get("/v1/jobs/#{job_id}")
end

#get_result(job_id) ⇒ Object



16
17
18
# File 'lib/scrapio/resources/jobs.rb', line 16

def get_result(job_id)
  @http.get("/v1/jobs/#{job_id}/result")
end

#wait_for_completion(job_id, poll_interval: 2.0, timeout: 300.0) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'lib/scrapio/resources/jobs.rb', line 20

def wait_for_completion(job_id, poll_interval: 2.0, timeout: 300.0)
  deadline = Time.now + timeout
  loop do
    job = get(job_id)
    return get_result(job_id) if TERMINAL.include?(job["status"])
    raise ScrapioError, "Job #{job_id} did not complete within #{timeout}s" if Time.now >= deadline
    sleep(poll_interval)
  end
end