Class: ScheduleWorkers::InventoryRedisCacheCleaner
- Inherits:
-
ApplicationWorker
- Object
- ApplicationWorker
- ScheduleWorkers::InventoryRedisCacheCleaner
- Includes:
- ElasticAPM::SpanHelpers
- Defined in:
- app/workers/schedule_workers/inventory_redis_cache_cleaner.rb
Overview
ScheduleWorkers::InventoryRedisCacheCleaner This worker is responsible for cleaning up old Redis inventory cache keys on a scheduled basis. It scans for old cache entries and removes expired versions to prevent Redis memory bloat.
Cleanup Strategy:
- Keeps only the LATEST version per restaurant/service_type
- Deletes keys with dates older than YESTERDAY (based on restaurant timezone)
Methods:
- perform: Main entry point, scans all restaurants and cleans old cache keys
- cleanup_restaurant_cache: Cleans cache for a specific restaurant
- scan_restaurants: Gets list of restaurants with inventory cache
- cleanup_old_date_keys: Removes cache keys with dates older than yesterday
- cleanup_old_version_keys: Removes cache keys with old versions (keeps latest 1 version)
Error Handling:
- All errors are reported using APMErrorHandler.report for monitoring and diagnostics
- Uses HH_LOGGER for structured logging
Performance:
- Processes restaurants in batches to avoid memory issues
- Uses Redis SCAN instead of KEYS for better performance
- Includes rate limiting to avoid overwhelming Redis
Constant Summary collapse
- KEEP_LATEST_VERSIONS =
Configuration
1- RESTAURANT_BATCH_SIZE =
Keep only the latest version
50- REDIS_SCAN_COUNT =
1000
Instance Method Summary collapse
-
#perform ⇒ Object
Main entry point for the worker.
Methods inherited from ApplicationWorker
Instance Method Details
#perform ⇒ Object
Main entry point for the worker
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 69 70 71 72 73 74 75 |
# File 'app/workers/schedule_workers/inventory_redis_cache_cleaner.rb', line 36 def perform ElasticAPM.with_span('Inventory Redis Cache Cleanup', 'cache', subtype: 'redis', action: 'cleanup') do |_span| cleanup_stats = { restaurants_processed: 0, old_date_keys_deleted: 0, old_version_keys_deleted: 0, total_memory_freed: 0, errors: 0, } begin HH_LOGGER.info('inventory_redis_cache_cleanup_started', { keep_latest_versions: KEEP_LATEST_VERSIONS, }) # Process all restaurants with inventory cache scan_restaurants.each_slice(RESTAURANT_BATCH_SIZE) do |restaurant_batch| restaurant_batch.each do |restaurant_id| restaurant_stats = cleanup_restaurant_cache(restaurant_id) cleanup_stats[:restaurants_processed] += 1 cleanup_stats[:old_date_keys_deleted] += restaurant_stats[:old_date_keys_deleted] cleanup_stats[:old_version_keys_deleted] += restaurant_stats[:old_version_keys_deleted] cleanup_stats[:total_memory_freed] += restaurant_stats[:memory_freed] rescue StandardError => e cleanup_stats[:errors] += 1 APMErrorHandler.report(e, context: { restaurant_id: restaurant_id }) end # Small delay between batches to avoid overwhelming Redis sleep(0.1) if restaurant_batch.size == RESTAURANT_BATCH_SIZE end HH_LOGGER.info('inventory_redis_cache_cleanup_completed', cleanup_stats) rescue StandardError => e APMErrorHandler.report(e, context: { cleanup_stats: cleanup_stats }) raise end end end |