Class: Async::Background::Web::Snapshot
- Inherits:
-
Object
- Object
- Async::Background::Web::Snapshot
- Includes:
- Clock
- Defined in:
- lib/async/background/web/snapshot.rb
Defined Under Namespace
Classes: CacheEntry
Instance Method Summary collapse
- #claimed(limit:) ⇒ Object
- #close ⇒ Object
- #closed? ⇒ Boolean
- #data_version ⇒ Object
- #executing(limit:) ⇒ Object
-
#initialize(path:, counts_cache_ttl:) ⇒ Snapshot
constructor
A new instance of Snapshot.
- #open! ⇒ Object
- #overview(force: false) ⇒ Object
- #pending(limit:, cursor: nil) ⇒ Object
- #recent_done(limit:, cursor: nil) ⇒ Object
- #recent_failed(limit:, cursor: nil) ⇒ Object
Constructor Details
#initialize(path:, counts_cache_ttl:) ⇒ Snapshot
Returns a new instance of Snapshot.
17 18 19 20 21 22 23 |
# File 'lib/async/background/web/snapshot.rb', line 17 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
71 72 73 |
# File 'lib/async/background/web/snapshot.rb', line 71 def claimed(limit:) read_rows(SQL::CLAIMED, [limit]).map { |row| claimed_row(row) } end |
#close ⇒ Object
39 40 41 42 43 44 45 46 |
# File 'lib/async/background/web/snapshot.rb', line 39 def close @mutex.synchronize do @db&.close unless @db&.closed? @db = nil @overview_cache = nil end self end |
#closed? ⇒ Boolean
48 49 50 |
# File 'lib/async/background/web/snapshot.rb', line 48 def closed? @mutex.synchronize { !connected? } end |
#data_version ⇒ Object
52 53 54 |
# File 'lib/async/background/web/snapshot.rb', line 52 def data_version with_database { |db| db.get_first_value(Queue::SQL::DATA_VERSION).to_i } end |
#executing(limit:) ⇒ Object
67 68 69 |
# File 'lib/async/background/web/snapshot.rb', line 67 def executing(limit:) read_rows(SQL::EXECUTING, [limit]).map { |row| executing_row(row) } end |
#open! ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/async/background/web/snapshot.rb', line 25 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
56 57 58 59 60 61 62 63 64 65 |
# File 'lib/async/background/web/snapshot.rb', line 56 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
85 86 87 88 |
# File 'lib/async/background/web/snapshot.rb', line 85 def pending(limit:, cursor: nil) sql, binds = pending_query(limit, cursor) read_rows(sql, binds).map { |row| pending_row(row) } end |
#recent_done(limit:, cursor: nil) ⇒ Object
75 76 77 78 |
# File 'lib/async/background/web/snapshot.rb', line 75 def recent_done(limit:, cursor: nil) sql, binds = terminal_query(SQL::DONE, SQL::DONE_AFTER, limit, cursor) read_rows(sql, binds).map { |row| done_row(row) } end |
#recent_failed(limit:, cursor: nil) ⇒ Object
80 81 82 83 |
# File 'lib/async/background/web/snapshot.rb', line 80 def recent_failed(limit:, cursor: nil) sql, binds = terminal_query(SQL::FAILED, SQL::FAILED_AFTER, limit, cursor) read_rows(sql, binds).map { |row| failed_row(row) } end |