Skip to content
This repository has been archived by the owner on Jan 3, 2025. It is now read-only.

Commit

Permalink
Add hook to delay email sending in registration queue (#696)
Browse files Browse the repository at this point in the history
* Add hook to delay email sending in registration queue

* Rename JobHooks to JobSideEffects

* add redis to local docker compose

* move invalidating Redis to after_create and after_update hooks

---------

Co-authored-by: FinnIckler <finnickler@gmail.com>
  • Loading branch information
gregorbg and FinnIckler authored Sep 23, 2024
1 parent 558e339 commit 9025317
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 7 deletions.
3 changes: 0 additions & 3 deletions app/controllers/registration_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
12 changes: 8 additions & 4 deletions app/jobs/registration_processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
8 changes: 8 additions & 0 deletions app/models/registration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 18 additions & 0 deletions lib/job_side_effects.rb
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions lib/redis_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 9025317

Please sign in to comment.