Class: Etna::Clients::Magma::MagmaCrudWorkflow

Inherits:
Struct
  • Object
show all
Defined in:
lib/etna/clients/magma/workflows/crud_workflow.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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_sizeObject

Returns the value of attribute batch_size

Returns:

  • (Object)

    the current value of batch_size



6
7
8
# File 'lib/etna/clients/magma/workflows/crud_workflow.rb', line 6

def batch_size
  @batch_size
end

#cache_policyObject

Returns the value of attribute cache_policy

Returns:

  • (Object)

    the current value of cache_policy



6
7
8
# File 'lib/etna/clients/magma/workflows/crud_workflow.rb', line 6

def cache_policy
  @cache_policy
end

#loggerObject

Returns the value of attribute logger

Returns:

  • (Object)

    the current value of logger



6
7
8
# File 'lib/etna/clients/magma/workflows/crud_workflow.rb', line 6

def logger
  @logger
end

#magma_clientObject

Returns the value of attribute magma_client

Returns:

  • (Object)

    the current value of magma_client



6
7
8
# File 'lib/etna/clients/magma/workflows/crud_workflow.rb', line 6

def magma_client
  @magma_client
end

#project_nameObject

Returns the value of attribute project_name

Returns:

  • (Object)

    the current value of project_name



6
7
8
# File 'lib/etna/clients/magma/workflows/crud_workflow.rb', line 6

def project_name
  @project_name
end

#read_onlyObject

Returns the value of attribute read_only

Returns:

  • (Object)

    the current value of read_only



6
7
8
# File 'lib/etna/clients/magma/workflows/crud_workflow.rb', line 6

def read_only
  @read_only
end

#recorded_updatesObject (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.message.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.

Yields:

  • (request)


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