Class: TableSync::Publisher

Inherits:
BasePublisher show all
Defined in:
lib/table_sync/publisher.rb

Constant Summary collapse

DEBOUNCE_TIME =
1.minute

Constants inherited from BasePublisher

BasePublisher::BASE_SAFE_JSON_TYPES, BasePublisher::NOT_MAPPED

Instance Method Summary collapse

Constructor Details

#initialize(object_class, original_attributes, destroyed: nil, confirm: true, state: :updated) ⇒ Publisher

‘original_attributes’ are not published, they are used to resolve the routing key



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/table_sync/publisher.rb', line 7

def initialize(object_class, original_attributes, destroyed: nil, confirm: true, state: :updated)
  @object_class = object_class.constantize
  @original_attributes = filter_safe_for_serialization(original_attributes.deep_symbolize_keys)
  @confirm = confirm

  if destroyed.nil?
    @state = validate_state(state)
  else
    # TODO Legacy job support, remove
    @state = destroyed ? :destroyed : :updated
  end
end

Instance Method Details

#publishObject



20
21
22
23
24
25
26
27
28
# File 'lib/table_sync/publisher.rb', line 20

def publish
  return enqueue_job if destroyed?

  sync_time = Rails.cache.read(cache_key) || current_time - DEBOUNCE_TIME - 1.second
  return if sync_time > current_time

  next_sync_time = sync_time + DEBOUNCE_TIME
  next_sync_time <= current_time ? enqueue_job : enqueue_job(next_sync_time)
end

#publish_nowObject



30
31
32
33
34
35
# File 'lib/table_sync/publisher.rb', line 30

def publish_now
  # Update request and object does not exist -> skip publishing
  return if !object && !destroyed?

  Rabbit.publish(params)
end