Class: Pgbus::Batch

Inherits:
Object
  • Object
show all
Defined in:
lib/pgbus/batch.rb

Constant Summary collapse

METADATA_KEY =
"pgbus_batch_id"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(on_finish: nil, on_success: nil, on_discard: nil, description: nil, properties: {}) ⇒ Batch

Returns a new instance of Batch.



13
14
15
16
17
18
19
20
21
# File 'lib/pgbus/batch.rb', line 13

def initialize(on_finish: nil, on_success: nil, on_discard: nil, description: nil, properties: {})
  @batch_id = SecureRandom.uuid
  @on_finish = on_finish
  @on_success = on_success
  @on_discard = on_discard
  @description = description
  @properties = properties
  @job_count = 0
end

Instance Attribute Details

#batch_idObject (readonly)

Returns the value of attribute batch_id.



10
11
12
# File 'lib/pgbus/batch.rb', line 10

def batch_id
  @batch_id
end

#descriptionObject (readonly)

Returns the value of attribute description.



10
11
12
# File 'lib/pgbus/batch.rb', line 10

def description
  @description
end

#on_discardObject (readonly)

Returns the value of attribute on_discard.



10
11
12
# File 'lib/pgbus/batch.rb', line 10

def on_discard
  @on_discard
end

#on_finishObject (readonly)

Returns the value of attribute on_finish.



10
11
12
# File 'lib/pgbus/batch.rb', line 10

def on_finish
  @on_finish
end

#on_successObject (readonly)

Returns the value of attribute on_success.



10
11
12
# File 'lib/pgbus/batch.rb', line 10

def on_success
  @on_success
end

#propertiesObject (readonly)

Returns the value of attribute properties.



10
11
12
# File 'lib/pgbus/batch.rb', line 10

def properties
  @properties
end

Class Method Details

.cleanup(older_than:) ⇒ Object

Delete finished batches older than the given threshold.



48
49
50
# File 'lib/pgbus/batch.rb', line 48

def self.cleanup(older_than:)
  BatchEntry.stale(before: older_than).delete_all
end

.find(batch_id) ⇒ Object

Find a batch record by ID. Returns a hash or nil.



43
44
45
# File 'lib/pgbus/batch.rb', line 43

def self.find(batch_id)
  BatchEntry.find_by(batch_id: batch_id)&.attributes
end

.job_completed(batch_id) ⇒ Object

Record a completed job. Returns the batch row after update.



33
34
35
# File 'lib/pgbus/batch.rb', line 33

def self.job_completed(batch_id)
  update_counter(batch_id, "completed_jobs")
end

.job_discarded(batch_id) ⇒ Object

Record a discarded (dead-lettered) job. Returns the batch row after update.



38
39
40
# File 'lib/pgbus/batch.rb', line 38

def self.job_discarded(batch_id)
  update_counter(batch_id, "discarded_jobs")
end

Instance Method Details

#enqueueObject

Enqueue a group of jobs as a batch. Jobs enqueued inside the block are tracked as part of this batch.



25
26
27
28
29
30
# File 'lib/pgbus/batch.rb', line 25

def enqueue(&)
  create_record
  count_jobs(&)
  update_total
  self
end