diff --git a/app/controllers/registration_controller.rb b/app/controllers/registration_controller.rb index f6b19e11..82d3fc06 100644 --- a/app/controllers/registration_controller.rb +++ b/app/controllers/registration_controller.rb @@ -138,9 +138,6 @@ def process_update(update_request) EmailApi.send_update_email(@competition_id, user_id, status, @current_user) end - # Invalidate cache - Rails.cache.delete("#{user_id}-registrations-by-user") - { user_id: updated_registration['user_id'], guests: updated_registration.guests, diff --git a/app/jobs/registration_processor.rb b/app/jobs/registration_processor.rb index ef267d6b..ad92ae27 100644 --- a/app/jobs/registration_processor.rb +++ b/app/jobs/registration_processor.rb @@ -7,22 +7,24 @@ class RegistrationProcessor < ApplicationJob def perform(message) Rails.logger.debug { "Working on Message: #{message}" } + side_effects = JobSideEffects.new if message[:step] == 'EventRegistration' event_registration(message[:competition_id], message[:user_id], message[:step_details][:event_ids], message[:step_details][:comment], message[:step_details][:guests], - message[:created_at]) + message[:created_at], + side_effects) end - Rails.cache.delete("#{message[:user_id]}-registrations-by-user") + side_effects.run(:after_processing) Metrics.increment('registrations_processed') end private # rubocop:disable Metrics/ParameterLists - def event_registration(competition_id, user_id, event_ids, comment, guests, created_at) + def event_registration(competition_id, user_id, event_ids, comment, guests, created_at, side_effects) # Event Registration might not be the first lane that is completed # TODO: When we add another lane, we need to update the registration history instead of creating it registration = begin @@ -46,7 +48,9 @@ def event_registration(competition_id, user_id, event_ids, comment, guests, crea else registration.update_attributes(lanes: registration.lanes.append(competing_lane), guests: guests) end - EmailApi.send_creation_email(competition_id, user_id) + side_effects.after_processing do + EmailApi.send_creation_email(competition_id, user_id) + end end # rubocop:enable Metrics/ParameterLists end diff --git a/app/models/registration.rb b/app/models/registration.rb index ddfbca22..054a86e9 100644 --- a/app/models/registration.rb +++ b/app/models/registration.rb @@ -14,6 +14,10 @@ class Registration # Pre-validations before_validation :set_competing_status + # Hooks + after_create :delete_user_registration_from_redis + after_update :delete_user_registration_from_redis + # Validations validate :competing_status_consistency @@ -216,4 +220,8 @@ def waiting_list_status_changed?(update_params) states_are_different = competing_status != update_params[:status] lane_state_present && states_are_different end + + def delete_user_registration_from_redis + RedisHelper.delete_user_registrations(user_id) + end end diff --git a/docker-compose.yml b/docker-compose.yml index e78a4c69..aa73bb87 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -48,6 +48,7 @@ services: REGISTRATION_HISTORY_DYNAMO_TABLE: "registration-history-development" CODE_ENVIRONMENT: "development" QUEUE_NAME: "registrations.fifo" + REDIS_URL: "redis://redis:6379" volumes: - .:/app - gems_volume_worker:/usr/local/bundle diff --git a/lib/job_side_effects.rb b/lib/job_side_effects.rb new file mode 100644 index 00000000..542c4677 --- /dev/null +++ b/lib/job_side_effects.rb @@ -0,0 +1,18 @@ +# frozen_string_literal: true + +class JobSideEffects + attr_writer :after_processing + + def initialize + @after_processing = [] + end + + def after_processing(&blk) + @after_processing << blk + end + + def run(hook) + defined_hooks = self.instance_variable_get(:"@#{hook}") + defined_hooks.each(&:call) + end +end diff --git a/lib/redis_helper.rb b/lib/redis_helper.rb index b060c986..153fed18 100644 --- a/lib/redis_helper.rb +++ b/lib/redis_helper.rb @@ -9,6 +9,10 @@ def self.increment_or_initialize(key, &) end end + def self.delete_user_registrations(user_id) + Rails.cache.delete("#{user_id}-registrations-by-user") + end + def self.decrement_or_initialize(key, &) if Rails.cache.exist?(key) Rails.cache.decrement(key)