Class: Mara::Batch
- Inherits:
-
Object
- Object
- Mara::Batch
- Defined in:
- lib/mara/batch.rb
Overview
This is not the same as a transaction. It only saves on the number of API calls to DynamoDB.
Perform operations in batches.
Constant Summary collapse
- BATCH_STACK_VAR_NAME =
The name of the thread variable that holds the current batch spec.
'mara_batch'.freeze
Instance Attribute Summary collapse
-
#batch_id ⇒ String
readonly
The current batch id.
-
#operations ⇒ Array<Array<Symbol, Hash>>
readonly
The queue of operations to perform on commit.
Class Method Summary collapse
-
.delete_model(item) ⇒ Object
Perform a delete model.
-
.delete_model!(item) ⇒ Object
Perform a delete model.
-
.in_batch { ... } ⇒ Object
Perform in a batch.
-
.save_model(item) ⇒ Object
Perform a save model.
-
.save_model!(item) ⇒ Object
Perform a save model.
Instance Method Summary collapse
-
#abort_batch ⇒ void
Abort the batch and clear the current batch operations.
-
#add(action_name, item) ⇒ void
Add an item to the operation queue.
-
#commit_batch ⇒ void
Perform all the operations in the queue.
-
#initialize ⇒ Batch
constructor
Create a new batch.
Constructor Details
#initialize ⇒ Batch
Create a new batch.
160 161 162 163 |
# File 'lib/mara/batch.rb', line 160 def initialize @batch_id = SecureRandom.uuid @operations = [] end |
Instance Attribute Details
#batch_id ⇒ String (readonly)
The current batch id.
154 155 156 |
# File 'lib/mara/batch.rb', line 154 def batch_id @batch_id end |
#operations ⇒ Array<Array<Symbol, Hash>> (readonly)
The queue of operations to perform on commit.
148 149 150 |
# File 'lib/mara/batch.rb', line 148 def operations @operations end |
Class Method Details
.delete_model(item) ⇒ Object
Perform a delete model. If there is a current batch it is added to the operation queue. If there is no current batch, this will be forwarded directly to the { Mara::Persistence}.
95 96 97 |
# File 'lib/mara/batch.rb', line 95 def delete_model(item) perform_for_model(:delete_model, item) end |
.delete_model!(item) ⇒ Object
Perform a delete model. If there is a current batch it is added to the operation queue. If there is no current batch, this will be forwarded directly to the { Mara::Persistence}.
107 108 109 |
# File 'lib/mara/batch.rb', line 107 def delete_model!(item) perform_for_model(:delete_model!, item) end |
.in_batch { ... } ⇒ Object
Perform in a batch.
All save/destroy calls on a model will be routed into the current batch.
If there is a error raised all operations will be dropped.
If the error is a { Mara::Rollback} the batch will silently rollback. If not, it will be re-thrown after the rollback.
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/mara/batch.rb', line 47 def in_batch begin_new_batch begin yield rescue Mara::Rollback abort_current_batch # rubocop:disable Lint/RescueException rescue Exception => exception # rubocop:enable Lint/RescueException abort_current_batch raise exception else commit_current_batch end end |
.save_model(item) ⇒ Object
Perform a save model. If there is a current batch it is added to the operation queue. If there is no current batch, this will be forwarded directly to the { Mara::Persistence}.
71 72 73 |
# File 'lib/mara/batch.rb', line 71 def save_model(item) perform_for_model(:save_model, item) end |
.save_model!(item) ⇒ Object
Perform a save model. If there is a current batch it is added to the operation queue. If there is no current batch, this will be forwarded directly to the { Mara::Persistence}.
83 84 85 |
# File 'lib/mara/batch.rb', line 83 def save_model!(item) perform_for_model(:save_model!, item) end |
Instance Method Details
#abort_batch ⇒ void
This method returns an undefined value.
Abort the batch and clear the current batch operations.
198 199 200 201 |
# File 'lib/mara/batch.rb', line 198 def abort_batch @operations = [] Mara.instrument('batch.abort', batch_id: batch_id) end |
#add(action_name, item) ⇒ void
This method returns an undefined value.
Add an item to the operation queue.
174 175 176 177 178 |
# File 'lib/mara/batch.rb', line 174 def add(action_name, item) Mara.instrument('batch.add_item', batch_id: batch_id, action: action_name, item: item) do operations << [action_name, item] end end |
#commit_batch ⇒ void
This method returns an undefined value.
Perform all the operations in the queue.
186 187 188 189 190 |
# File 'lib/mara/batch.rb', line 186 def commit_batch Mara.instrument('batch.commit', batch_id: batch_id) do execute_commit end end |