This repository has been archived by the owner on Jan 3, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
34 changed files
with
1,274 additions
and
575 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,5 +35,5 @@ | |
.idea | ||
tmp | ||
node_modules | ||
storage | ||
localstack | ||
yarn.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
require 'securerandom' | ||
class MetricsController < ApplicationController | ||
def index | ||
# Get the queue attributes | ||
queue_url = $sqs.get_queue_url(queue_name: "registrations.fifo").queue_url | ||
response = $sqs.get_queue_attributes({ | ||
queue_url: queue_url, | ||
attribute_names: ["ApproximateNumberOfMessages"] | ||
}) | ||
|
||
# Get the queue size | ||
queue_size = response.attributes["ApproximateNumberOfMessages"].to_i | ||
|
||
render json: { queue_size: queue_size} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
require 'json' | ||
require 'aws-sdk-sqs' | ||
require_relative 'registration_processor' | ||
|
||
class QueuePoller | ||
# Wait for 1 second so we can start work on 10 messages at at time | ||
# These numbers can be tweaked after load testing | ||
WAIT_TIME = 1 | ||
MAX_MESSAGES = 10 | ||
|
||
def self.perform | ||
if ENV['LOCALSTACK_ENDPOINT'] | ||
@sqs ||= Aws::SQS::Client.new(endpoint: ENV['LOCALSTACK_ENDPOINT']) | ||
else | ||
@sqs ||= Aws::SQS::Client.new | ||
end | ||
|
||
queue = @sqs.get_queue_url(queue_name: "registrations.fifo").queue_url | ||
poller = Aws::SQS::QueuePoller.new(queue) | ||
poller.poll(wait_time_seconds: WAIT_TIME, max_number_of_messages: MAX_MESSAGES) do |messages| | ||
messages.each do |msg| | ||
# Messages are deleted from the queue when the block returns normally! | ||
puts "Received message with ID: #{msg.message_id}" | ||
puts "Message body: #{msg.body}" | ||
body = JSON.parse msg.body | ||
begin | ||
RegistrationProcessor.process_message(body) | ||
rescue StandardError => e | ||
# unexpected error occurred while processing messages, | ||
# log it, and skip delete so it can be re-processed later | ||
puts "Error #{e} when processing message with ID #{msg}" | ||
throw :skip_delete | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
require 'aws-sdk-dynamodb' | ||
|
||
class RegistrationProcessor | ||
def self.process_message(message) | ||
if ENV['LOCALSTACK_ENDPOINT'] | ||
@dynamodb ||= Aws::DynamoDB::Client.new(endpoint: ENV['LOCALSTACK_ENDPOINT']) | ||
else | ||
@dynamodb ||= Aws::DynamoDB::Client.new | ||
end | ||
# implement your message processing logic here | ||
puts "Working on Message: #{message}" | ||
if message['step'] == "Event Registration" | ||
registration = { | ||
competitor_id: message['competitor_id'], | ||
competition_id: message['competition_id'], | ||
event_ids: message['event_ids'], | ||
registration_status: "waiting", | ||
} | ||
save_registration(registration) | ||
end | ||
end | ||
|
||
private | ||
|
||
def self.save_registration(registration) | ||
@dynamodb.put_item({ | ||
table_name: 'Registrations', | ||
item: registration | ||
}) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,11 @@ | ||
# config/initializers/aws.rb | ||
|
||
if Rails.env.production? | ||
# We are using IAM Roles to authenticate in prod | ||
Aws.config.update({ | ||
region: ENV["AWS_REGION"], | ||
}) | ||
$dynamodb = Aws::DynamoDB::Client.new | ||
$sqs = Aws::SQS::Client.new | ||
$queue = ENV["QUEUE_URL"] | ||
else | ||
# We are using fake values in dev | ||
$dynamodb = Aws::DynamoDB::Client.new(endpoint: ENV['DYNAMODB_ENDPOINT'], region: "my-cool-region-1", credentials: Aws::Credentials.new('my_cool_key', 'my_cool_secret')) | ||
# We are using localstack to emulate AWS in dev | ||
$dynamodb = Aws::DynamoDB::Client.new(endpoint: ENV['LOCALSTACK_ENDPOINT']) | ||
$sqs = Aws::SQS::Client.new(endpoint: ENV['LOCALSTACK_ENDPOINT']) | ||
end | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,8 @@ | ||
Rails.application.routes.draw do | ||
get '/healthcheck', to: 'healthcheck#index' | ||
post '/register', to: 'registration#create' | ||
patch '/register', to: 'registration#update' | ||
delete '/register', to: 'registration#delete' | ||
get '/registrations', to: 'registration#list' | ||
get '/metrics', to: 'metrics#index' | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.