Class: Async::Background::Web::Snapshot

Inherits:
Object
  • Object
show all
Includes:
Clock
Defined in:
lib/async/background/web/snapshot.rb

Defined Under Namespace

Classes: CacheEntry

Constant Summary collapse

RAW_COLUMNS =
{'args' => :args_raw, 'options' => :options_raw}.freeze
ROW_KEYS =
SQL::EXTRA_COLUMNS.to_h do |status, extra|
  columns = SQL::BASE_COLUMNS + extra
  [status, columns.map { |column| RAW_COLUMNS.fetch(column, column.to_sym) }.freeze]
end.freeze
PAGING =
{
  executing: [SQL::EXECUTING, nil, nil].freeze,
  claimed: [SQL::CLAIMED, nil, nil].freeze,
  done: [SQL::DONE, SQL::DONE_AFTER, %i[finished_at id].freeze].freeze,
  failed: [SQL::FAILED, SQL::FAILED_AFTER, %i[finished_at id].freeze].freeze,
  pending: [SQL::PENDING, SQL::PENDING_AFTER, %i[run_at id].freeze].freeze
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(path:, counts_cache_ttl:) ⇒ Snapshot

Returns a new instance of Snapshot.



31
32
33
34
35
36
37
# File 'lib/async/background/web/snapshot.rb', line 31

def initialize(path:, counts_cache_ttl:)
  @path = path
  @overview_cache_ttl = counts_cache_ttl
  @mutex = Mutex.new
  @db = nil
  @overview_cache = nil
end

Instance Method Details

#claimed(limit:) ⇒ Object



85
86
87
# File 'lib/async/background/web/snapshot.rb', line 85

def claimed(limit:)
  list(:claimed, limit: limit)
end

#closeObject



53
54
55
56
57
58
59
60
# File 'lib/async/background/web/snapshot.rb', line 53

def close
  @mutex.synchronize do
    @db&.close unless @db&.closed?
    @db = nil
    @overview_cache = nil
  end
  self
end

#closed?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/async/background/web/snapshot.rb', line 62

def closed?
  @mutex.synchronize { !connected? }
end

#data_versionObject



66
67
68
# File 'lib/async/background/web/snapshot.rb', line 66

def data_version
  with_database { |db| db.get_first_value(Queue::SQL::DATA_VERSION).to_i }
end

#executing(limit:) ⇒ Object



81
82
83
# File 'lib/async/background/web/snapshot.rb', line 81

def executing(limit:)
  list(:executing, limit: limit)
end

#open!Object



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/async/background/web/snapshot.rb', line 39

def open!
  @mutex.synchronize do
    return self if connected?

    db = open_database
    configure_database(db)
    @db = db
  rescue StandardError
    db&.close unless db&.closed?
    raise
  end
  self
end

#overview(force: false) ⇒ Object



70
71
72
73
74
75
76
77
78
79
# File 'lib/async/background/web/snapshot.rb', line 70

def overview(force: false)
  with_database do |db|
    now = monotonic_now
    return @overview_cache.value if !force && overview_cache_current?(now)

    value = read_transaction(db) { overview_from(db) }.freeze
    @overview_cache = CacheEntry.new(value, now)
    value
  end
end

#pending(limit:, cursor: nil) ⇒ Object



97
98
99
# File 'lib/async/background/web/snapshot.rb', line 97

def pending(limit:, cursor: nil)
  list(:pending, limit: limit, cursor: cursor)
end

#recent_done(limit:, cursor: nil) ⇒ Object



89
90
91
# File 'lib/async/background/web/snapshot.rb', line 89

def recent_done(limit:, cursor: nil)
  list(:done, limit: limit, cursor: cursor)
end

#recent_failed(limit:, cursor: nil) ⇒ Object



93
94
95
# File 'lib/async/background/web/snapshot.rb', line 93

def recent_failed(limit:, cursor: nil)
  list(:failed, limit: limit, cursor: cursor)
end