Class: CardDB::Resources::Exports

Inherits:
Base
  • Object
show all
Defined in:
lib/carddb/resources/exports.rb

Overview

Publisher dataset export jobs resource.

Instance Attribute Summary

Attributes inherited from Base

#client, #config, #connection

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from CardDB::Resources::Base

Instance Method Details

#cancel(id:) ⇒ Object

Cancel a pending or processing export job. Requires a server-side secret credential.



72
73
74
75
76
77
# File 'lib/carddb/resources/exports.rb', line 72

def cancel(id:)
  config.require_secret_credential!('exports.cancel')

  data = connection.execute(QueryBuilder.export_job_cancel, { id: id })
  ExportJob.new(data['exportJobCancel'], client: client)
end

#get_job(id, cache: nil) ⇒ Object

Fetch one export job.



17
18
19
20
21
22
23
# File 'lib/carddb/resources/exports.rb', line 17

def get_job(id, cache: nil)
  key = cache_key('exports', 'get_job', id: id)
  with_cache(key, resource: :exports, cache: cache) do
    data = connection.execute(QueryBuilder.export_job, { id: id })
    data['exportJob'] ? ExportJob.new(data['exportJob'], client: client) : nil
  end
end

#list_jobs(publisher_id:, game_id: nil, dataset_id: nil, status: nil, first: nil, after: nil, cache: nil) ⇒ Object

List export jobs for a publisher, optionally filtered by game, dataset, or status.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/carddb/resources/exports.rb', line 26

def list_jobs(publisher_id:, game_id: nil, dataset_id: nil, status: nil, first: nil, after: nil, cache: nil)
  query = QueryBuilder.export_jobs(
    publisher_id: publisher_id,
    game_id: game_id,
    dataset_id: dataset_id,
    status: status,
    first: first,
    after: after
  )
  variables = build_variables(
    publisherId: publisher_id,
    gameId: game_id,
    datasetId: dataset_id,
    status: status,
    first: first,
    after: after
  )
  key = cache_key('exports', 'list_jobs', **variables)

  data = with_cache(key, resource: :exports, cache: cache) { connection.execute(query, variables) }

  Collection.new(
    data['exportJobs'],
    item_class: ExportJob,
    next_page_loader: lambda { |cursor|
      list_jobs(
        publisher_id: publisher_id,
        game_id: game_id,
        dataset_id: dataset_id,
        status: status,
        first: first,
        after: cursor,
        cache: cache
      )
    },
    client: client
  )
end

#refresh_url(id:) ⇒ Object

Refresh the signed download URL for a completed export job.



66
67
68
69
# File 'lib/carddb/resources/exports.rb', line 66

def refresh_url(id:)
  data = connection.execute(QueryBuilder.export_job_refresh_url, { id: id })
  ExportJob.new(data['exportJobRefreshUrl'], client: client)
end

#run(input:) ⇒ Object Also known as: create

Start an async dataset export. Requires a server-side secret credential.



8
9
10
11
12
13
# File 'lib/carddb/resources/exports.rb', line 8

def run(input:)
  config.require_secret_credential!('exports.run')

  data = connection.execute(QueryBuilder.dataset_export, { input: input })
  ExportJob.new(data['datasetExport'], client: client)
end

#wait_for_job(id, interval: 1, timeout: 300) ⇒ Object

Poll an export job until completion or failure.



80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/carddb/resources/exports.rb', line 80

def wait_for_job(id, interval: 1, timeout: 300)
  started_at = Process.clock_gettime(Process::CLOCK_MONOTONIC)

  loop do
    job = get_job(id, cache: false)
    raise Error, "Export job '#{id}' was not found" unless job
    return job if job.completed? || job.failed?

    elapsed = Process.clock_gettime(Process::CLOCK_MONOTONIC) - started_at
    raise Error, "Timed out waiting for export job '#{id}'" if elapsed >= timeout

    sleep interval
  end
end