Class: EventTracer::DynamoDB::Worker

Inherits:
Object
  • Object
show all
Includes:
Sidekiq::Worker
Defined in:
lib/event_tracer/dynamo_db/worker.rb

Constant Summary collapse

MAX_DYNAMO_DB_ITEM_PER_REQUEST =
25

Instance Method Summary collapse

Constructor Details

#initialize(client = nil) ⇒ Worker

Returns a new instance of Worker.



23
24
25
26
# File 'lib/event_tracer/dynamo_db/worker.rb', line 23

def initialize(client = nil)
  @config = EventTracer::Config.config
  @client = client || @config.dynamo_db_client || Client.call
end

Instance Method Details

#perform(items) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/event_tracer/dynamo_db/worker.rb', line 28

def perform(items)
  wrap(items).each_slice(MAX_DYNAMO_DB_ITEM_PER_REQUEST) do |batch|
    data = batch.map do |item|
      { put_request: { item: clean_empty_values(item) } }
    end

    client.batch_write_item(
      request_items: { config.dynamo_db_table_name => data }
    )

  rescue Aws::DynamoDB::Errors::ServiceError => e
    EventTracer.error(
      loggers: %i(base),
      action: 'DynamoDBWorker',
      app: EventTracer::Config.config.app_name,
      error: e.class.name,
      message: e.message
    )
  end
end