Class: Cosmo::API::Batch
- Inherits:
-
Object
- Object
- Cosmo::API::Batch
- Defined in:
- lib/cosmo/api/batch.rb,
sig/cosmo/api/batch.rbs
Overview
Read-model over Cosmo::Batch's KV/counter state, for the status API and Web UI.
Constant Summary collapse
- LIMIT =
25
Instance Attribute Summary collapse
-
#bid ⇒ ::String
readonly
Returns the value of attribute bid.
Class Method Summary collapse
Instance Method Summary collapse
- #callback(event) ⇒ Hash[Symbol, untyped]?
- #callbacks ⇒ Hash[Symbol, Hash[Symbol, untyped]?]
- #counter ⇒ Counter
- #created_at ⇒ ::Integer?
- #fired?(event) ⇒ Boolean
-
#initialize(bid) ⇒ Batch
constructor
A new instance of Batch.
- #kv ⇒ KV
- #live_stats ⇒ Hash[Symbol, untyped]
- #meta ⇒ Hash[Symbol, untyped]
- #parent_id ⇒ ::String?
- #ready? ⇒ Boolean
-
#stats ⇒ Hash[Symbol, untyped]
{ total:, pending:, succeeded:, failed: } -- read from the
readysnapshot once finalized (counters are reset by then), or live off the counters (withpendingstill moving) while the batch is still open.
Constructor Details
#initialize(bid) ⇒ Batch
Returns a new instance of Batch.
23 24 25 |
# File 'lib/cosmo/api/batch.rb', line 23 def initialize(bid) @bid = bid end |
Instance Attribute Details
#bid ⇒ ::String (readonly)
Returns the value of attribute bid.
21 22 23 |
# File 'lib/cosmo/api/batch.rb', line 21 def bid @bid end |
Class Method Details
.all(limit: LIMIT) ⇒ Array[Batch]
9 10 11 |
# File 'lib/cosmo/api/batch.rb', line 9 def self.all(limit: LIMIT) kv.keys("*.meta", limit: limit).map { |key| new(key.delete_suffix(".meta")) } end |
Instance Method Details
#callback(event) ⇒ Hash[Symbol, untyped]?
53 54 55 56 |
# File 'lib/cosmo/api/batch.rb', line 53 def callback(event) entry = kv.get("#{bid}.callback.#{event}") Utils::Json.parse(entry&.value) end |
#callbacks ⇒ Hash[Symbol, Hash[Symbol, untyped]?]
58 59 60 |
# File 'lib/cosmo/api/batch.rb', line 58 def callbacks { success: callback(:success), complete: callback(:complete) } end |
#counter ⇒ Counter
79 80 81 |
# File 'lib/cosmo/api/batch.rb', line 79 def counter self.class.counter end |
#created_at ⇒ ::Integer?
35 36 37 |
# File 'lib/cosmo/api/batch.rb', line 35 def created_at [:created_at] end |
#fired?(event) ⇒ Boolean
62 63 64 |
# File 'lib/cosmo/api/batch.rb', line 62 def fired?(event) !!kv.get("#{bid}.fired.#{event}") end |
#live_stats ⇒ Hash[Symbol, untyped]
68 69 70 71 72 73 |
# File 'lib/cosmo/api/batch.rb', line 68 def live_stats total = counter.get("#{bid}.total") failed = counter.get("#{bid}.failed") pending = counter.get("#{bid}.pending") { total: total, pending: pending, succeeded: total - failed - pending, failed: failed } end |
#meta ⇒ Hash[Symbol, untyped]
27 28 29 |
# File 'lib/cosmo/api/batch.rb', line 27 def Utils::Json.parse(kv.get("#{bid}.meta")&.value) || {} end |
#parent_id ⇒ ::String?
31 32 33 |
# File 'lib/cosmo/api/batch.rb', line 31 def parent_id [:parent_id] end |
#ready? ⇒ Boolean
39 40 41 |
# File 'lib/cosmo/api/batch.rb', line 39 def ready? !!kv.get("#{bid}.ready") end |
#stats ⇒ Hash[Symbol, untyped]
{ total:, pending:, succeeded:, failed: } -- read from the ready
snapshot once finalized (counters are reset by then), or live off the
counters (with pending still moving) while the batch is still open.
46 47 48 49 50 51 |
# File 'lib/cosmo/api/batch.rb', line 46 def stats entry = kv.get("#{bid}.ready") return Utils::Json.parse(entry.value).merge(pending: 0) if entry live_stats end |