Class: Partner::BookingExportWorker

Inherits:
ApplicationWorker show all
Includes:
ExportTracking, MoneyRails::ActionViewExtension, ReservationExportHelpers
Defined in:
app/workers/partner/booking_export_worker.rb

Overview

)

Constant Summary

Constants included from ExportConstants

ExportConstants::CHANNEL_IDS_TO_SKIP, ExportConstants::DEFAULT_QUEUE, ExportConstants::EMAIL_BATCH_SIZE, ExportConstants::ERROR_CATEGORIES, ExportConstants::EXCEL_FILE_EXTENSION, ExportConstants::LARGE_DATASET_WARNING_THRESHOLD, ExportConstants::LONG_PROCESS_DAYS_THRESHOLD, ExportConstants::LONG_PROCESS_QUEUE, ExportConstants::MAX_RETRIES, ExportConstants::PDF_FILE_EXTENSION, ExportConstants::PROGRESS_MESSAGES, ExportConstants::PROGRESS_PHASES, ExportConstants::PROGRESS_UPDATE_BATCH_SIZE, ExportConstants::RESERVATION_BATCH_SIZE, ExportConstants::RETRY_BASE_INTERVAL, ExportConstants::RETRY_MAX_ELAPSED_TIME, ExportConstants::ZERO_COMMISSION_CHANNEL_ID

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ReservationExportHelpers

#calc_prepayment, #calculate_commission, #calculate_restaurant_revenue, #calculate_total_package_price, #date_format, #decimal_format, #format_add_on_names_with_quantity, #format_addon_names_for_report, #format_money, #format_package_names, #format_package_names_for_report, #format_package_names_with_quantity, #format_voucher_amount, #format_voucher_code, #get_reservation_distance, #paid_amount, #restaurant_delivery_fee, #sanitize_package_name, #sanitizer, #valid_email?, #voucher_amount, #voucher_code

Methods included from ExportTracking

#complete_export_tracking, #complete_export_tracking_fallback, #create_progress_tracker, #current_export_status, #enhanced_tracking_enabled?, #handle_export_error, #progress_tracker, #setup_enhanced_tracking, #track_data_collection, #track_email_sending, #track_file_generation, #track_file_upload, #update_data_collection_progress, #update_email_sending_progress, #update_file_generation_progress, #with_export_tracking

Methods inherited from ApplicationWorker

unlimited_retry

Instance Attribute Details

#attachmentObject

Returns the value of attribute attachment.



27
28
29
# File 'app/workers/partner/booking_export_worker.rb', line 27

def attachment
  @attachment
end

#data_typeObject

Returns the value of attribute data_type.



27
28
29
# File 'app/workers/partner/booking_export_worker.rb', line 27

def data_type
  @data_type
end

#date_filter_typeObject

Returns the value of attribute date_filter_type.



27
28
29
# File 'app/workers/partner/booking_export_worker.rb', line 27

def date_filter_type
  @date_filter_type
end

#date_typeObject

Returns the value of attribute date_type.



27
28
29
# File 'app/workers/partner/booking_export_worker.rb', line 27

def date_type
  @date_type
end

#emailsObject

Returns the value of attribute emails.



27
28
29
# File 'app/workers/partner/booking_export_worker.rb', line 27

def emails
  @emails
end

#end_dateObject

Returns the value of attribute end_date.



27
28
29
# File 'app/workers/partner/booking_export_worker.rb', line 27

def end_date
  @end_date
end

#end_timeObject

Returns the value of attribute end_time.



27
28
29
# File 'app/workers/partner/booking_export_worker.rb', line 27

def end_time
  @end_time
end

#pdf_urlObject

Returns the value of attribute pdf_url.



27
28
29
# File 'app/workers/partner/booking_export_worker.rb', line 27

def pdf_url
  @pdf_url
end

#reservationsObject

Returns the value of attribute reservations.



27
28
29
# File 'app/workers/partner/booking_export_worker.rb', line 27

def reservations
  @reservations
end

#restaurantObject

Returns the value of attribute restaurant.



27
28
29
# File 'app/workers/partner/booking_export_worker.rb', line 27

def restaurant
  @restaurant
end

#restaurant_idObject

Returns the value of attribute restaurant_id.



27
28
29
# File 'app/workers/partner/booking_export_worker.rb', line 27

def restaurant_id
  @restaurant_id
end

#staff_idObject

Returns the value of attribute staff_id.



27
28
29
# File 'app/workers/partner/booking_export_worker.rb', line 27

def staff_id
  @staff_id
end

#start_dateObject

Returns the value of attribute start_date.



27
28
29
# File 'app/workers/partner/booking_export_worker.rb', line 27

def start_date
  @start_date
end

#start_timeObject

Returns the value of attribute start_time.



27
28
29
# File 'app/workers/partner/booking_export_worker.rb', line 27

def start_time
  @start_time
end

Class Method Details

.smart_perform_async(export_params, attachment_id = nil) ⇒ String

Smart queue selection based on export size (similar to existing system)

Parameters:

  • export_params (Hash)

    Export parameters

  • attachment_id (Integer) (defaults to: nil)

    ID of attachment to track progress

Returns:

  • (String)

    Job ID



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'app/workers/partner/booking_export_worker.rb', line 36

def self.smart_perform_async(export_params, attachment_id = nil)
  start_date = Date.parse(export_params[:start_date]) if export_params[:start_date].present?
  end_date = Date.parse(export_params[:end_date]) if export_params[:end_date].present?

  # Determine if this should use long process queue (3+ days date range)
  use_long_process = if start_date.present? && end_date.present?
                       (end_date - start_date).to_i >= 3
                     else
                       false
                     end

  if use_long_process
    # Use Sidekiq client to explicitly set queue to longprocess
    client = Sidekiq::Client.new
    client.push(
      'class' => 'Partner::BookingExportWorker',
      'queue' => 'longprocess',
      'args' => [export_params, attachment_id],
    )
  else
    # Use default queue for smaller exports
    perform_async(export_params, attachment_id)
  end
end

Instance Method Details

#perform(export_params, attachment_id = nil) ⇒ Object

Main export processing method

Parameters:

  • export_params (Hash)

    Export parameters

  • attachment_id (Integer) (defaults to: nil)

    ID of attachment to track progress



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'app/workers/partner/booking_export_worker.rb', line 65

def perform(export_params, attachment_id = nil)
  # Retry options configuration (matching NotificationWorkers::ReservationReport)
  retry_options = {
    on: [Aws::SES::Errors::Throttling],
    tries: 2,
    base_interval: 1,
    max_elapsed_time: 60,
    on_retry: Proc.new do |_, try, elapsed_time, _|
      APMErrorHandler.report("Retrying after #{elapsed_time} seconds... (Attempt #{try})")
      store status: Attachment::RETRYING_STATUS
      store message: "Retrying after #{elapsed_time} seconds... (Attempt #{try})"
    end,
  }

  Retriable.retriable(retry_options) do
    # Setup enhanced tracking
    setup_enhanced_tracking(attachment_id, true)

    # Parse and validate parameters
    parse_export_parameters(export_params)
    validate_export_parameters

    # Execute export with enhanced tracking
    with_export_tracking do
      # Phase 1: Data Collection
      track_data_collection do
        collect_reservation_data
      end

      # Phase 2: File Generation
      track_file_generation(@reservations&.count || 0) do
        generate_export_file
      end

      # Phase 3: File Upload
      track_file_upload do
        upload_export_file
      end

      # Phase 4: Email Notification
      track_email_sending(emails.count) do
        send_export_notifications
      end

      # Note: Completion tracking is handled automatically by with_export_tracking
    end
  rescue Aws::SES::Errors::Throttling => e
    store email_status: Attachment::FAILED_EMAIL_STATUS
    store message: "Email sending failed due to AWS SES throttling. Error: #{e.message}"
    # Retriable gem will handle retries for this specific error.
    raise e
  rescue StandardError => e
    store email_status: Attachment::FAILED_EMAIL_STATUS
    store message: "An error occurred: #{e.message}"
    store status: Attachment::FAILED_STATUS if @attachment.blank? || @attachment.download_link.blank?
    APMErrorHandler.report("An error occurred: #{e.message}")
    raise e
  end
rescue StandardError => e
  # Handle error through enhanced tracking (stores in message)
  handle_export_error(:export_failed, e.message, e)
  raise e
end