Class: Etna::Clients::Magma::MagmaCrudWorkflow
- Inherits:
-
Struct
- Object
- Struct
- Etna::Clients::Magma::MagmaCrudWorkflow
- Defined in:
- lib/etna/clients/magma/workflows/crud_workflow.rb
Instance Attribute Summary collapse
-
#batch_size ⇒ Object
Returns the value of attribute batch_size.
-
#cache_policy ⇒ Object
Returns the value of attribute cache_policy.
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#magma_client ⇒ Object
Returns the value of attribute magma_client.
-
#project_name ⇒ Object
Returns the value of attribute project_name.
-
#read_only ⇒ Object
Returns the value of attribute read_only.
-
#recorded_updates ⇒ Object
readonly
Returns the value of attribute recorded_updates.
Instance Method Summary collapse
-
#initialize(args) ⇒ MagmaCrudWorkflow
constructor
A new instance of MagmaCrudWorkflow.
- #lookup_record(model_name, record_id) ⇒ Object
- #page_records(model_name, request = Etna::Clients::Magma::RetrievalRequest.new( project_name: project_name, model_name: model_name, record_names: 'all', page_size: 20, page: 1, ), &block) ⇒ Object
-
#update_records(method: :update) {|request| ... } ⇒ Object
Todo: Introduce associative concatenation operations for response objects and return one response that munges the batched responses together.
Constructor Details
#initialize(args) ⇒ MagmaCrudWorkflow
Returns a new instance of MagmaCrudWorkflow.
9 10 11 |
# File 'lib/etna/clients/magma/workflows/crud_workflow.rb', line 9 def initialize(args) super(**{logger: Etna::Logger.new('/dev/stdout', 999999, 1024 * 1024), batch_size: 30, cache_policy: :update_on_write}.update(args)) end |
Instance Attribute Details
#batch_size ⇒ Object
Returns the value of attribute batch_size
6 7 8 |
# File 'lib/etna/clients/magma/workflows/crud_workflow.rb', line 6 def batch_size @batch_size end |
#cache_policy ⇒ Object
Returns the value of attribute cache_policy
6 7 8 |
# File 'lib/etna/clients/magma/workflows/crud_workflow.rb', line 6 def cache_policy @cache_policy end |
#logger ⇒ Object
Returns the value of attribute logger
6 7 8 |
# File 'lib/etna/clients/magma/workflows/crud_workflow.rb', line 6 def logger @logger end |
#magma_client ⇒ Object
Returns the value of attribute magma_client
6 7 8 |
# File 'lib/etna/clients/magma/workflows/crud_workflow.rb', line 6 def magma_client @magma_client end |
#project_name ⇒ Object
Returns the value of attribute project_name
6 7 8 |
# File 'lib/etna/clients/magma/workflows/crud_workflow.rb', line 6 def project_name @project_name end |
#read_only ⇒ Object
Returns the value of attribute read_only
6 7 8 |
# File 'lib/etna/clients/magma/workflows/crud_workflow.rb', line 6 def read_only @read_only end |
#recorded_updates ⇒ Object (readonly)
Returns the value of attribute recorded_updates.
7 8 9 |
# File 'lib/etna/clients/magma/workflows/crud_workflow.rb', line 7 def recorded_updates @recorded_updates end |
Instance Method Details
#lookup_record(model_name, record_id) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/etna/clients/magma/workflows/crud_workflow.rb', line 13 def lookup_record(model_name, record_id) if (cached = (@cache ||= {}).dig(model_name, record_id)) return cached end result = magma_client.retrieve(RetrievalRequest.new(project_name: project_name, record_names: [record_id], model_name: model_name))\ .models.model(model_name).documents.document(record_id) if cache_policy ((@cache ||= {})[model_name] ||= {})[record_id = result] end result end |
#page_records(model_name, request = Etna::Clients::Magma::RetrievalRequest.new( project_name: project_name, model_name: model_name, record_names: 'all', page_size: 20, page: 1, ), &block) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/etna/clients/magma/workflows/crud_workflow.rb', line 28 def page_records(model_name, request = Etna::Clients::Magma::RetrievalRequest.new( project_name: project_name, model_name: model_name, record_names: 'all', page_size: 20, page: 1, ), &block) documents = Documents.new({}) last_page = nil while last_page.nil? || last_page.models.model_keys.map { |k| last_page.models.model(k).documents.raw.length }.sum > 0 attempts = 0 begin attempts += 1 last_page = magma_client.retrieve(request) # Unfortunately, paging in magma is not great and times out from time to time. rescue Net::ReadTimeout => e if attempts > 5 raise e end retry rescue Etna::Error => e if e.status === 502 if attempts > 5 raise e end retry else raise e unless e..include?('not found') break end end documents += last_page.models.model(model_name).documents unless block_given? yield last_page if block_given? request.page += 1 end documents end |
#update_records(method: :update) {|request| ... } ⇒ Object
Todo: Introduce associative concatenation operations for response objects and return one response that munges the batched responses together.
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/etna/clients/magma/workflows/crud_workflow.rb', line 73 def update_records(method: :update) @recorded_updates ||= UpdateRequest.new(project_name: project_name) request = UpdateRequest.new(project_name: project_name) yield request case cache_policy when :update_on_write @cache ||= {} request.revisions.each do |model_name, revisions| model_revisions = @cache[model_name] ||= {} revisions.each do |record_name, revision| if model_revisions.include? record_name model_revisions[record_name].update(revision) end end end end revisions = request.revisions responses = [] revisions.to_a.each_slice(batch_size) do |batch| request.revisions = batch.to_h magma_client.send(method, request) unless read_only responses << @recorded_updates.revisions.update(request.revisions) end responses end |