Class: EventDrivenServices::HhSearch::Producers::RestaurantProducer

Inherits:
Object
  • Object
show all
Defined in:
app/services/event_driven_services/hh_search/producers/restaurant_producer.rb

Constant Summary collapse

VALID_EVENT_TYPES =
[
  EventDrivenClient::Constants::INDEX_EVENT,
  EventDrivenClient::Constants::CREATE_EVENT,
  EventDrivenClient::Constants::UPDATE_EVENT,
  EventDrivenClient::Constants::DELETE_EVENT,
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(restaurant_id, event_type, changed_attributes = {}, options = {}) ⇒ RestaurantProducer

Returns a new instance of RestaurantProducer.



15
16
17
18
19
20
21
22
# File 'app/services/event_driven_services/hh_search/producers/restaurant_producer.rb', line 15

def initialize(restaurant_id, event_type, changed_attributes = {}, options = {})
  @restaurant = Restaurant.find_by(id: restaurant_id)&.decorate
  @event_type = validate_event_type(event_type)
  @topic = EventDrivenClient::Constants::RESTAURANTS_TOPIC
  @changed_attributes = changed_attributes
  @domain = 'restaurants'
  @source = options.fetch('source', options.fetch(:source, 'unknown'))
end

Instance Attribute Details

#changed_attributesObject (readonly)

Returns the value of attribute changed_attributes.



6
7
8
# File 'app/services/event_driven_services/hh_search/producers/restaurant_producer.rb', line 6

def changed_attributes
  @changed_attributes
end

#domainObject (readonly)

Returns the value of attribute domain.



6
7
8
# File 'app/services/event_driven_services/hh_search/producers/restaurant_producer.rb', line 6

def domain
  @domain
end

#event_typeObject (readonly)

Returns the value of attribute event_type.



6
7
8
# File 'app/services/event_driven_services/hh_search/producers/restaurant_producer.rb', line 6

def event_type
  @event_type
end

#restaurantObject (readonly)

Returns the value of attribute restaurant.



6
7
8
# File 'app/services/event_driven_services/hh_search/producers/restaurant_producer.rb', line 6

def restaurant
  @restaurant
end

#sourceObject (readonly)

Returns the value of attribute source.



6
7
8
# File 'app/services/event_driven_services/hh_search/producers/restaurant_producer.rb', line 6

def source
  @source
end

#topicObject (readonly)

Returns the value of attribute topic.



6
7
8
# File 'app/services/event_driven_services/hh_search/producers/restaurant_producer.rb', line 6

def topic
  @topic
end

Instance Method Details

#reindex_all(with_availability: false, batch_id: nil) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'app/services/event_driven_services/hh_search/producers/restaurant_producer.rb', line 43

def reindex_all(with_availability: false, batch_id: nil)
  last_record_id = nil
  batch_size = 1
  batch_number = 1
  # NOTE: Added explicit ordering to ensure deterministic batching and prevent duplicate events
  restaurants = Restaurant.bookable.not_expired.order(:id)
  total_items = restaurants.size

  restaurants.find_in_batches(batch_size: batch_size) do |batch|
    # Check if this is the last batch
    # If the batch size is less than the requested batch size, or the last record ID is the same as the last record ID in the batch
    is_last_batch = batch.size < batch_size || (batch.last.id == last_record_id)

    # send event
    payload = generate_reindex_payload(
      batch, total_items, batch_number, is_last_batch, with_availability, batch_id
    )
    client.send_event_async(payload)

    last_record_id = batch.last.id
    batch_number += 1
  end
end

#send_eventObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'app/services/event_driven_services/hh_search/producers/restaurant_producer.rb', line 24

def send_event
  # Skip sending event if restaurant is not found
  return if restaurant.blank?

  payload = generate_payload

  # skip sending event if payload is empty
  return if payload.blank?

  client.send_event_async(payload.to_json)

  if event_type == EventDrivenClient::Constants::UPDATE_EVENT
    send_update_total_restaurants_on_tag_event(payload)
  end
rescue StandardError => e
  APMErrorHandler.report("Failed to send event to topic #{topic}", error: e)
  raise e
end