Class: Cosmo::API::Batch

Inherits:
Object
  • Object
show all
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 =

Returns:

  • (::Integer)
25

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bid) ⇒ Batch

Returns a new instance of Batch.

Parameters:

  • bid (::String)


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.

Returns:

  • (::String)


21
22
23
# File 'lib/cosmo/api/batch.rb', line 21

def bid
  @bid
end

Class Method Details

.all(limit: LIMIT) ⇒ Array[Batch]

Parameters:

  • limit: (::Integer) (defaults to: LIMIT)

Returns:



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

.counterCounter

Returns:



17
18
19
# File 'lib/cosmo/api/batch.rb', line 17

def self.counter
  Cosmo::Batch.counter
end

.kvKV

Returns:



13
14
15
# File 'lib/cosmo/api/batch.rb', line 13

def self.kv
  Cosmo::Batch.kv
end

Instance Method Details

#callback(event) ⇒ Hash[Symbol, untyped]?

Parameters:

  • event (Symbol)

Returns:

  • (Hash[Symbol, untyped], nil)


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

#callbacksHash[Symbol, Hash[Symbol, untyped]?]

Returns:

  • (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

#counterCounter

Returns:



79
80
81
# File 'lib/cosmo/api/batch.rb', line 79

def counter
  self.class.counter
end

#created_at::Integer?

Returns:

  • (::Integer, nil)


35
36
37
# File 'lib/cosmo/api/batch.rb', line 35

def created_at
  meta[:created_at]
end

#fired?(event) ⇒ Boolean

Parameters:

  • event (Symbol)

Returns:

  • (Boolean)


62
63
64
# File 'lib/cosmo/api/batch.rb', line 62

def fired?(event)
  !!kv.get("#{bid}.fired.#{event}")
end

#kvKV

Returns:



75
76
77
# File 'lib/cosmo/api/batch.rb', line 75

def kv
  self.class.kv
end

#live_statsHash[Symbol, untyped]

Returns:

  • (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

#metaHash[Symbol, untyped]

Returns:

  • (Hash[Symbol, untyped])


27
28
29
# File 'lib/cosmo/api/batch.rb', line 27

def meta
  Utils::Json.parse(kv.get("#{bid}.meta")&.value) || {}
end

#parent_id::String?

Returns:

  • (::String, nil)


31
32
33
# File 'lib/cosmo/api/batch.rb', line 31

def parent_id
  meta[:parent_id]
end

#ready?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/cosmo/api/batch.rb', line 39

def ready?
  !!kv.get("#{bid}.ready")
end

#statsHash[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.

Returns:

  • (Hash[Symbol, untyped])


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