Class: ScheduleWorkers::WeeklyRestaurantSummaryMainWorker

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

Instance Method Summary collapse

Methods inherited from ApplicationWorker

unlimited_retry

Constructor Details

#initialize(sub_worker_class: WeeklyRestaurantSummarySubWorker) ⇒ WeeklyRestaurantSummaryMainWorker

Initialize with injectable dependencies for better testability

Parameters:

  • sub_worker_class (Class) (defaults to: WeeklyRestaurantSummarySubWorker)

    Sub-worker class for processing individual restaurants



20
21
22
# File 'app/workers/schedule_workers/weekly_restaurant_summary_main_worker.rb', line 20

def initialize(sub_worker_class: WeeklyRestaurantSummarySubWorker)
  @sub_worker_class = sub_worker_class
end

Instance Method Details

#performObject

Dispatches restaurant processing to sub-workers for parallel execution. Each job is scheduled with a random delay (0-24 hours) to spread database load and prevent memory limit exceeded errors in ClickHouse.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'app/workers/schedule_workers/weekly_restaurant_summary_main_worker.rb', line 27

def perform
  # Clean up summaries for inactive/expired restaurants
  cleanup_invalid_summaries

  restaurant_ids = []

  Restaurant.active.not_expired.find_each(batch_size: 10) do |restaurant|
    restaurant_ids << restaurant.id
    # Spread jobs randomly over 24 hours (86400 seconds) to prevent DB overload
    delay_seconds = rand(0..86400)
    @sub_worker_class.perform_in(delay_seconds, restaurant.id)
  end

  BUSINESS_LOGGER.info(
    "#{self.class}: Dispatched #{restaurant_ids.size} restaurants for weekly summary calculation (spread over 24 hours)",
    restaurant_ids: restaurant_ids,
  )
end