diff --git a/.tool-versions b/.tool-versions new file mode 100644 index 0000000..1c44dd0 --- /dev/null +++ b/.tool-versions @@ -0,0 +1 @@ +ruby 2.6.6 diff --git a/Gemfile b/Gemfile index b20df6d..f410675 100644 --- a/Gemfile +++ b/Gemfile @@ -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' diff --git a/Gemfile.lock b/Gemfile.lock index 20c5283..8bf6ebd 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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) @@ -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) diff --git a/app/lib/rabbit_mq.rb b/app/lib/rabbit_mq.rb new file mode 100644 index 0000000..ce11e2a --- /dev/null +++ b/app/lib/rabbit_mq.rb @@ -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 diff --git a/config.ru b/config.ru index c6c11b7..f7ad152 100644 --- a/config.ru +++ b/config.ru @@ -2,6 +2,5 @@ require_relative 'config/environment' run Rack::URLMap.new( '/v1/signup' => UserRoutes, - '/v1/login' => UserSessionRoutes, - '/v1/auth' => AuthRoutes + '/v1/login' => UserSessionRoutes ) diff --git a/config/initializers/consumer.rb b/config/initializers/consumer.rb new file mode 100644 index 0000000..4dfeecb --- /dev/null +++ b/config/initializers/consumer.rb @@ -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 diff --git a/config/settings.yml b/config/settings.yml index 380d7b6..cdde460 100644 --- a/config/settings.yml +++ b/config/settings.yml @@ -1,8 +1,10 @@ db: adapter: postgresql host: localhost - user: postgres + user: safrio pagination: page_size: 10 app: secret: secret +rabbitmq: + consumer_pool: 10