Class: Inventory::UpdateCacheWorker

Inherits:
ApplicationWorker show all
Includes:
ElasticAPM::SpanHelpers
Defined in:
app/workers/inventory/update_cache_worker.rb

Overview

engines/mirage/app/consumers/mirage/cache_consumer.rb call this worker to clear cache in Sidekiq, so we have a retry mechanism when something goes wrong

Instance Method Summary collapse

Methods inherited from ApplicationWorker

unlimited_retry

Instance Method Details

#perform(restaurant_id, payload, debug_payload = {}) ⇒ void

This method returns an undefined value.

Processes inventory cache update requests

This worker processes cache update requests for restaurant inventory. It receives a payload from the Mirage::CacheConsumer and delegates the cache update to the restaurant's inventory checker service.

The worker handles multiple update types:

  • Reservation level updates (for specific reservation times)

  • Restaurant level updates (for all restaurant packages)

  • Restaurant package level updates (for specific packages)

Parameters:

  • restaurant_id (Integer)

    ID of the restaurant to update cache for

  • payload (Hash)

    Cache update configuration containing:

    • reservation_id [Integer, nil] Optional ID of a specific reservation

    • restaurant_package_id [Integer, nil] Optional ID of a specific restaurant package

    • restaurant_package_ids [Array<Integer>, nil] Optional list of restaurant package IDs

    • reservation_level [Boolean] Whether to update at reservation level

    • restaurant_level [Boolean] Whether to update at restaurant level

    • restaurant_package_level [Boolean] Whether to update at restaurant package level

    • dates [Array<String>] List of dates to update in YYYY-MM-DD format

    • datetimes [Array<String>] List of specific date-times to update

    • reset_mode [Boolean] Whether to perform a cache reset operation

  • debug_payload (Hash) (defaults to: {})

    Optional debugging information to include in logs



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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'app/workers/inventory/update_cache_worker.rb', line 32

def perform(restaurant_id, payload, debug_payload = {})
  # Prepare debug payload for logging
  debug_payload = debug_payload.with_indifferent_access.merge(
    message: 'Performing Inventory::UpdateCacheWorker',
  )

  begin
    # Log the start of the operation with structured data
    HH_LOGGER.info('Starting inventory cache update', {
                     restaurant_id: restaurant_id,
                     update_type: determine_update_type(payload),
                     package_id: payload['restaurant_package_id'],
                     package_ids_count: payload['restaurant_package_ids']&.size,
                     dates_count: payload['dates']&.size || 0,
                     datetimes_count: payload['datetimes']&.size || 0,
                   })

    # Broadcast debug information
    broadcast_debug_event(payload, debug_payload)

    # Find the restaurant with APM span tracking
    restaurant = Restaurant.find(restaurant_id)

    # Update the cache with APM span tracking
    ElasticAPM.with_span('Update Cache', 'cache', subtype: 'inventory', action: 'update') do |_span|
      # Execute cache update operation
      restaurant.inv_checker_service_class.update_cache_value(
        reservation_id: payload['reservation_id'],
        restaurant_id: restaurant.id,
        restaurant_package_id: payload['restaurant_package_id'],
        restaurant_package_ids: payload['restaurant_package_ids'],
        reservation_level: !!payload['reservation_level'],
        restaurant_level: !!payload['restaurant_level'],
        restaurant_package_level: !!payload['restaurant_package_level'],
        dates: payload['dates'].presence || [],
        datetimes: payload['datetimes'].presence || [],
        reset_mode: !!payload['reset_mode'],
      )
    end

    # Log successful completion
    HH_LOGGER.info('Inventory cache updated successfully', {
                     restaurant_id: restaurant_id,
                     update_type: determine_update_type(payload),
                   })

    # Broadcast success debug event
    broadcast_debug_event(payload, debug_payload.merge(message: 'Inventory cache updated successfully'))
  rescue ActiveRecord::RecordNotFound => e
    # Handle missing restaurant error
    handle_error(e, restaurant_id, payload, debug_payload, 'Restaurant not found')
  rescue StandardError => e
    # Handle general errors
    handle_error(e, restaurant_id, payload, debug_payload, 'Error updating inventory cache')
    raise # Re-raise for Sidekiq retry mechanism
  end
end