Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ruby 2.6.6
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ gem 'i18n', '~> 1.8.2'
gem 'config', '~> 2.2.1'
gem 'jwt', '~> 2.2.1'

gem 'bunny', '~> 2.15.0'
gem 'pg', '~> 1.2.3'
gem 'sequel', '~> 5.32.0'
gem 'sequel_secure_password', '~> 0.2.15'
Expand Down
4 changes: 4 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ GEM
minitest (~> 5.1)
tzinfo (~> 1.1)
zeitwerk (~> 2.2, >= 2.2.2)
amq-protocol (2.3.2)
backports (3.15.0)
bcrypt (3.1.13)
bunny (2.15.0)
amq-protocol (~> 2.3, >= 2.3.1)
concurrent-ruby (1.1.6)
config (2.2.1)
deep_merge (~> 1.2, >= 1.2.1)
Expand Down Expand Up @@ -119,6 +122,7 @@ PLATFORMS

DEPENDENCIES
activesupport (~> 6.0.0)
bunny (~> 2.15.0)
config (~> 2.2.1)
database_cleaner-sequel (~> 1.8.0)
dry-initializer (~> 3.0.3)
Expand Down
24 changes: 24 additions & 0 deletions app/lib/rabbit_mq.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module RabbitMq
extend self

@mutex = Mutex.new

def connection
@mutex.synchronize do
@connection ||= Bunny.new.start
end
end

def channel
Thread.current[:rabbitmq_channel] ||= connection.create_channel
end

def consumer_channel
# See http://rubybunny.info/articles/concurrency.html#consumer_work_pools
Thread.current[:rabbitmq_consumer_channel] ||=
connection.create_channel(
nil,
Settings.rabbitmq.consumer_pool
)
end
end
3 changes: 1 addition & 2 deletions config.ru
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@ require_relative 'config/environment'

run Rack::URLMap.new(
'/v1/signup' => UserRoutes,
'/v1/login' => UserSessionRoutes,
'/v1/auth' => AuthRoutes
'/v1/login' => UserSessionRoutes
)
22 changes: 22 additions & 0 deletions config/initializers/consumer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
channel = RabbitMq.consumer_channel
exchange = channel.default_exchange
queue = channel.queue('auth', durable: true)

queue.subscribe(manual_ack: true) do |delivery_info, properties, payload|
user_id = nil
payload = JSON(payload)
token = JwtEncoder.decode(payload)

unless token.nil?
result = Auth::FetchUserService.call(token['uuid'])
user_id = result.user.id if result.success?
end
ensure
publish_payload = { user_id: user_id }.to_json
exchange.publish(
publish_payload,
routing_key: properties.reply_to,
correlation_id: properties.correlation_id
)
channel.ack(delivery_info.delivery_tag)
end
4 changes: 3 additions & 1 deletion config/settings.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
db:
adapter: postgresql
host: localhost
user: postgres
user: safrio
pagination:
page_size: 10
app:
secret: secret
rabbitmq:
consumer_pool: 10