Class: ScheduleWorkers::WeeklyRestaurantSummarySubWorker

Inherits:
ApplicationWorker show all
Defined in:
app/workers/schedule_workers/weekly_restaurant_summary_sub_worker.rb

Instance Method Summary collapse

Methods inherited from ApplicationWorker

unlimited_retry

Constructor Details

#initialize(hh_search_producer: EventDrivenWorkers::HhSearch::ProducerWorker, redis_client: $redis_lru, business_logger: BUSINESS_LOGGER, apm_error_handler: APMErrorHandler) ⇒ WeeklyRestaurantSummarySubWorker

Initialize with injectable dependencies for better testability

Parameters:

  • hh_search_producer (Class) (defaults to: EventDrivenWorkers::HhSearch::ProducerWorker)

    Producer worker class for HH Search events

  • redis_client (Object) (defaults to: $redis_lru)

    Redis client for caching (defaults to $redis_lru)

  • business_logger (Object) (defaults to: BUSINESS_LOGGER)

    Logger for business events (defaults to BUSINESS_LOGGER)

  • apm_error_handler (Object) (defaults to: APMErrorHandler)

    Error handler for APM reporting (defaults to APMErrorHandler)



16
17
18
19
20
21
22
23
24
# File 'app/workers/schedule_workers/weekly_restaurant_summary_sub_worker.rb', line 16

def initialize(hh_search_producer: EventDrivenWorkers::HhSearch::ProducerWorker,
               redis_client: $redis_lru,
               business_logger: BUSINESS_LOGGER,
               apm_error_handler: APMErrorHandler)
  @hh_search_producer = hh_search_producer
  @redis_client = redis_client
  @business_logger = business_logger
  @apm_error_handler = apm_error_handler
end

Instance Method Details

#perform(restaurant_id) ⇒ Object

Processes weekly summary calculation for a single restaurant.

Parameters:

  • restaurant_id (Integer)

    The restaurant ID to process



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
# File 'app/workers/schedule_workers/weekly_restaurant_summary_sub_worker.rb', line 29

def perform(restaurant_id)
  # Use HhClickHouseRestaurant for reading data
  clickhouse_restaurant = HhClickHouseRestaurant.find(restaurant_id)
  return unless clickhouse_restaurant.active_and_not_expired?

  ActiveRecord::Base.transaction do
    today = Time.now_in_tz(clickhouse_restaurant.time_zone).to_date
    start_date = today - 7.days

    gmv_cents = calculate_gmv_cents(clickhouse_restaurant, start_date)
    reviews_count = calculate_reviews_count(clickhouse_restaurant, start_date)
    reservations_count = calculate_reservations_count(clickhouse_restaurant, start_date)

    # Write to primary database using restaurant_id
    weekly_summary = WeeklyRestaurantSummary.find_or_initialize_by(restaurant_id: clickhouse_restaurant.id)
    weekly_summary.assign_attributes(
      gmv_cents: gmv_cents,
      reviews_count: reviews_count,
      reservations_count: reservations_count,
    )

    weekly_summary.save!
    # Send update event to Kafka, for updating top GMV
    send_update_top_gmv_event(weekly_summary)

    @business_logger.info(
      "#{self.class}: Updated weekly summary for restaurant #{clickhouse_restaurant.id}",
      gmv_cents: gmv_cents,
      reviews_count: reviews_count,
      reservations_count: reservations_count,
    )
  end
rescue ActiveRecord::RecordNotFound
  @business_logger.warn(
    "#{self.class}: Restaurant #{restaurant_id} not found, skipping",
  )
rescue StandardError => e
  @apm_error_handler.report(e, context: { restaurant_id: restaurant_id })
  raise e
end