Class: Pgbus::Batch
- Inherits:
-
Object
- Object
- Pgbus::Batch
- Defined in:
- lib/pgbus/batch.rb
Constant Summary collapse
- METADATA_KEY =
"pgbus_batch_id"
Instance Attribute Summary collapse
-
#batch_id ⇒ Object
readonly
Returns the value of attribute batch_id.
-
#description ⇒ Object
readonly
Returns the value of attribute description.
-
#on_discard ⇒ Object
readonly
Returns the value of attribute on_discard.
-
#on_finish ⇒ Object
readonly
Returns the value of attribute on_finish.
-
#on_success ⇒ Object
readonly
Returns the value of attribute on_success.
-
#properties ⇒ Object
readonly
Returns the value of attribute properties.
Class Method Summary collapse
-
.cleanup(older_than:) ⇒ Object
Delete finished batches older than the given threshold.
-
.find(batch_id) ⇒ Object
Find a batch record by ID.
-
.job_completed(batch_id) ⇒ Object
Record a completed job.
-
.job_discarded(batch_id) ⇒ Object
Record a discarded (dead-lettered) job.
Instance Method Summary collapse
-
#enqueue ⇒ Object
Enqueue a group of jobs as a batch.
-
#initialize(on_finish: nil, on_success: nil, on_discard: nil, description: nil, properties: {}) ⇒ Batch
constructor
A new instance of Batch.
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_id ⇒ Object (readonly)
Returns the value of attribute batch_id.
10 11 12 |
# File 'lib/pgbus/batch.rb', line 10 def batch_id @batch_id end |
#description ⇒ Object (readonly)
Returns the value of attribute description.
10 11 12 |
# File 'lib/pgbus/batch.rb', line 10 def description @description end |
#on_discard ⇒ Object (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_finish ⇒ Object (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_success ⇒ Object (readonly)
Returns the value of attribute on_success.
10 11 12 |
# File 'lib/pgbus/batch.rb', line 10 def on_success @on_success end |
#properties ⇒ Object (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
#enqueue ⇒ Object
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 |