Class: ScheduleWorkers::WeeklyRestaurantSummaryMainWorker
- Inherits:
-
ApplicationWorker
- Object
- ApplicationWorker
- ScheduleWorkers::WeeklyRestaurantSummaryMainWorker
- Defined in:
- app/workers/schedule_workers/weekly_restaurant_summary_main_worker.rb
Instance Method Summary collapse
-
#initialize(sub_worker_class: WeeklyRestaurantSummarySubWorker) ⇒ WeeklyRestaurantSummaryMainWorker
constructor
Initialize with injectable dependencies for better testability.
-
#perform ⇒ Object
Dispatches restaurant processing to sub-workers for parallel execution.
Methods inherited from ApplicationWorker
Constructor Details
#initialize(sub_worker_class: WeeklyRestaurantSummarySubWorker) ⇒ WeeklyRestaurantSummaryMainWorker
Initialize with injectable dependencies for better testability
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
#perform ⇒ Object
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 |