From f60431658bd7062a979c429f43fc9e323acc1043 Mon Sep 17 00:00:00 2001 From: Yuri Smirnov Date: Tue, 11 Feb 2025 18:38:13 +0300 Subject: [PATCH] Remove lamian and rails dependencies (#30) --- Gemfile | 9 +- Gemfile.lock | 254 ++++++++----------------------- config/sneakers.yml | 9 ++ environments/development.rb | 6 +- lib/rabbit.rb | 25 ++- lib/rabbit/daemon.rb | 11 +- lib/rabbit/publishing.rb | 4 +- lib/rabbit/receiving/job.rb | 13 +- lib/rabbit/version.rb | 2 +- rabbit_messaging.gemspec | 4 +- spec/units/rabbit/daemon_spec.rb | 16 +- 11 files changed, 109 insertions(+), 244 deletions(-) create mode 100644 config/sneakers.yml diff --git a/Gemfile b/Gemfile index 8c4e4b1..23e5849 100644 --- a/Gemfile +++ b/Gemfile @@ -3,15 +3,14 @@ source "https://rubygems.org" gemspec -gem "benchmark" -gem "bundler" -gem "ostruct" gem "pry" -gem "rails" -gem "rake" +gem "reline" gem "rspec" gem "rspec-its" gem "rubocop-config-umbrellio" gem "simplecov" gem "simplecov-lcov" + +gem "activejob" +gem "activerecord" gem "sneakers_handlers", github: "umbrellio/sneakers_handlers" diff --git a/Gemfile.lock b/Gemfile.lock index 2585a55..8ef66d8 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -8,268 +8,139 @@ GIT PATH remote: . specs: - rabbit_messaging (1.2.0) + rabbit_messaging (1.3.0) bunny (~> 2.0) - kicks (~> 3) - lamian - rails (>= 6.1) + kicks tainbox GEM remote: https://rubygems.org/ specs: - actioncable (7.1.4.1) - actionpack (= 7.1.4.1) - activesupport (= 7.1.4.1) - nio4r (~> 2.0) - websocket-driver (>= 0.6.1) - zeitwerk (~> 2.6) - actionmailbox (7.1.4.1) - actionpack (= 7.1.4.1) - activejob (= 7.1.4.1) - activerecord (= 7.1.4.1) - activestorage (= 7.1.4.1) - activesupport (= 7.1.4.1) - mail (>= 2.7.1) - net-imap - net-pop - net-smtp - actionmailer (7.1.4.1) - actionpack (= 7.1.4.1) - actionview (= 7.1.4.1) - activejob (= 7.1.4.1) - activesupport (= 7.1.4.1) - mail (~> 2.5, >= 2.5.4) - net-imap - net-pop - net-smtp - rails-dom-testing (~> 2.2) - actionpack (7.1.4.1) - actionview (= 7.1.4.1) - activesupport (= 7.1.4.1) - nokogiri (>= 1.8.5) - racc - rack (>= 2.2.4) - rack-session (>= 1.0.1) - rack-test (>= 0.6.3) - rails-dom-testing (~> 2.2) - rails-html-sanitizer (~> 1.6) - actiontext (7.1.4.1) - actionpack (= 7.1.4.1) - activerecord (= 7.1.4.1) - activestorage (= 7.1.4.1) - activesupport (= 7.1.4.1) - globalid (>= 0.6.0) - nokogiri (>= 1.8.5) - actionview (7.1.4.1) - activesupport (= 7.1.4.1) - builder (~> 3.1) - erubi (~> 1.11) - rails-dom-testing (~> 2.2) - rails-html-sanitizer (~> 1.6) - activejob (7.1.4.1) - activesupport (= 7.1.4.1) + activejob (7.2.2.1) + activesupport (= 7.2.2.1) globalid (>= 0.3.6) - activemodel (7.1.4.1) - activesupport (= 7.1.4.1) - activerecord (7.1.4.1) - activemodel (= 7.1.4.1) - activesupport (= 7.1.4.1) + activemodel (7.2.2.1) + activesupport (= 7.2.2.1) + activerecord (7.2.2.1) + activemodel (= 7.2.2.1) + activesupport (= 7.2.2.1) timeout (>= 0.4.0) - activestorage (7.1.4.1) - actionpack (= 7.1.4.1) - activejob (= 7.1.4.1) - activerecord (= 7.1.4.1) - activesupport (= 7.1.4.1) - marcel (~> 1.0) - activesupport (7.1.4.1) + activesupport (7.2.2.1) base64 + benchmark (>= 0.3) bigdecimal - concurrent-ruby (~> 1.0, >= 1.0.2) + concurrent-ruby (~> 1.0, >= 1.3.1) connection_pool (>= 2.2.5) drb i18n (>= 1.6, < 2) + logger (>= 1.4.2) minitest (>= 5.1) - mutex_m - tzinfo (~> 2.0) + securerandom (>= 0.3) + tzinfo (~> 2.0, >= 2.0.5) amq-protocol (2.3.2) ast (2.4.2) base64 (0.2.0) benchmark (0.4.0) - bigdecimal (3.1.8) - builder (3.3.0) + bigdecimal (3.1.9) bunny (2.23.0) amq-protocol (~> 2.3, >= 2.3.1) sorted_set (~> 1, >= 1.0.2) coderay (1.1.3) - concurrent-ruby (1.3.4) - connection_pool (2.4.1) - crass (1.0.6) - date (3.3.4) + concurrent-ruby (1.3.5) + connection_pool (2.5.0) diff-lcs (1.5.1) docile (1.4.1) drb (2.2.1) - erubi (1.13.0) globalid (1.2.1) activesupport (>= 6.1) - i18n (1.14.6) + i18n (1.14.7) concurrent-ruby (~> 1.0) - io-console (0.7.2) - irb (1.14.1) - rdoc (>= 4.0.0) - reline (>= 0.4.2) - json (2.7.2) - kicks (3.1.0) + io-console (0.8.0) + json (2.10.0) + kicks (3.2.0) bunny (~> 2.19) concurrent-ruby (~> 1.0) rake (>= 12.3, < 14.0) serverengine (~> 2.1) thor - lamian (1.9.0) - rails (>= 4.2) - language_server-protocol (3.17.0.3) - logger (1.6.1) - loofah (2.23.1) - crass (~> 1.0.2) - nokogiri (>= 1.12.0) - mail (2.8.1) - mini_mime (>= 0.1.1) - net-imap - net-pop - net-smtp - marcel (1.0.4) + language_server-protocol (3.17.0.4) + logger (1.6.5) method_source (1.1.0) - mini_mime (1.1.5) - mini_portile2 (2.8.8) - minitest (5.25.1) - mutex_m (0.2.0) - net-imap (0.4.17) - date - net-protocol - net-pop (0.1.2) - net-protocol - net-protocol (0.2.2) - timeout - net-smtp (0.5.0) - net-protocol - nio4r (2.7.3) - nokogiri (1.16.8) - mini_portile2 (~> 2.8.2) - racc (~> 1.4) - ostruct (0.6.1) + minitest (5.25.4) parallel (1.26.3) - parser (3.3.5.0) + parser (3.3.7.1) ast (~> 2.4.1) racc - pry (0.14.2) + pry (0.15.2) coderay (~> 1.1) method_source (~> 1.0) - psych (5.1.2) - stringio racc (1.8.1) - rack (3.1.8) - rack-session (2.0.0) - rack (>= 3.0.0) - rack-test (2.1.0) - rack (>= 1.3) - rackup (2.1.0) - rack (>= 3) - webrick (~> 1.8) - rails (7.1.4.1) - actioncable (= 7.1.4.1) - actionmailbox (= 7.1.4.1) - actionmailer (= 7.1.4.1) - actionpack (= 7.1.4.1) - actiontext (= 7.1.4.1) - actionview (= 7.1.4.1) - activejob (= 7.1.4.1) - activemodel (= 7.1.4.1) - activerecord (= 7.1.4.1) - activestorage (= 7.1.4.1) - activesupport (= 7.1.4.1) - bundler (>= 1.15.0) - railties (= 7.1.4.1) - rails-dom-testing (2.2.0) - activesupport (>= 5.0.0) - minitest - nokogiri (>= 1.6) - rails-html-sanitizer (1.6.1) - loofah (~> 2.21) - nokogiri (>= 1.15.7, != 1.16.7, != 1.16.6, != 1.16.5, != 1.16.4, != 1.16.3, != 1.16.2, != 1.16.1, != 1.16.0.rc1, != 1.16.0) - railties (7.1.4.1) - actionpack (= 7.1.4.1) - activesupport (= 7.1.4.1) - irb - rackup (>= 1.0.0) - rake (>= 12.2) - thor (~> 1.0, >= 1.2.2) - zeitwerk (~> 2.6) + rack (3.1.9) rainbow (3.1.1) rake (13.2.1) rbtree (0.4.6) - rdoc (6.7.0) - psych (>= 4.0.0) - regexp_parser (2.9.2) - reline (0.5.10) + regexp_parser (2.10.0) + reline (0.6.0) io-console (~> 0.5) rspec (3.13.0) rspec-core (~> 3.13.0) rspec-expectations (~> 3.13.0) rspec-mocks (~> 3.13.0) - rspec-core (3.13.2) + rspec-core (3.13.3) rspec-support (~> 3.13.0) rspec-expectations (3.13.3) diff-lcs (>= 1.2.0, < 2.0) rspec-support (~> 3.13.0) - rspec-its (1.3.1) - rspec-core (>= 3.0.0) - rspec-expectations (>= 3.0.0) + rspec-its (2.0.0) + rspec-core (>= 3.13.0) + rspec-expectations (>= 3.13.0) rspec-mocks (3.13.2) diff-lcs (>= 1.2.0, < 2.0) rspec-support (~> 3.13.0) - rspec-support (3.13.1) - rubocop (1.66.1) + rspec-support (3.13.2) + rubocop (1.69.2) json (~> 2.3) language_server-protocol (>= 3.17.0) parallel (~> 1.10) parser (>= 3.3.0.2) rainbow (>= 2.2.2, < 4.0) - regexp_parser (>= 2.4, < 3.0) - rubocop-ast (>= 1.32.2, < 2.0) + regexp_parser (>= 2.9.3, < 3.0) + rubocop-ast (>= 1.36.2, < 2.0) ruby-progressbar (~> 1.7) - unicode-display_width (>= 2.4.0, < 3.0) - rubocop-ast (1.32.3) + unicode-display_width (>= 2.4.0, < 4.0) + rubocop-ast (1.38.0) parser (>= 3.3.1.0) - rubocop-config-umbrellio (1.66.0.99) - rubocop (~> 1.66.0) + rubocop-config-umbrellio (1.69.0.101) + rubocop (~> 1.69.0) rubocop-factory_bot (~> 2.26.0) - rubocop-performance (~> 1.22.0) - rubocop-rails (~> 2.26.0) + rubocop-performance (~> 1.23.0) + rubocop-rails (~> 2.28.0) rubocop-rake (~> 0.6.0) - rubocop-rspec (~> 3.0.0) - rubocop-sequel (~> 0.3.3) + rubocop-rspec (~> 3.3.0) + rubocop-sequel (~> 0.3.0) rubocop-factory_bot (2.26.1) rubocop (~> 1.61) - rubocop-performance (1.22.1) + rubocop-performance (1.23.1) rubocop (>= 1.48.1, < 2.0) rubocop-ast (>= 1.31.1, < 2.0) - rubocop-rails (2.26.2) + rubocop-rails (2.28.0) activesupport (>= 4.2.0) rack (>= 1.1) rubocop (>= 1.52.0, < 2.0) rubocop-ast (>= 1.31.1, < 2.0) rubocop-rake (0.6.0) rubocop (~> 1.0) - rubocop-rspec (3.0.5) + rubocop-rspec (3.3.0) rubocop (~> 1.61) - rubocop-sequel (0.3.4) + rubocop-sequel (0.3.8) rubocop (~> 1.0) ruby-progressbar (1.13.0) + securerandom (0.4.1) serverengine (2.4.0) base64 (~> 0.1) logger (~> 1.4) sigdump (~> 0.2.2) - set (1.1.0) + set (1.1.1) sigdump (0.2.5) simplecov (0.22.0) docile (~> 1.1) @@ -281,31 +152,26 @@ GEM sorted_set (1.0.3) rbtree set (~> 1.0) - stringio (3.1.1) tainbox (2.1.2) activesupport thor (1.3.2) - timeout (0.4.1) + timeout (0.4.3) tzinfo (2.0.6) concurrent-ruby (~> 1.0) - unicode-display_width (2.6.0) - webrick (1.8.2) - websocket-driver (0.7.6) - websocket-extensions (>= 0.1.0) - websocket-extensions (0.1.5) - zeitwerk (2.6.18) + unicode-display_width (3.1.4) + unicode-emoji (~> 4.0, >= 4.0.4) + unicode-emoji (4.0.4) PLATFORMS + arm64-darwin ruby DEPENDENCIES - benchmark - bundler - ostruct + activejob + activerecord pry rabbit_messaging! - rails - rake + reline rspec rspec-its rubocop-config-umbrellio diff --git a/config/sneakers.yml b/config/sneakers.yml new file mode 100644 index 0000000..0a2a3f3 --- /dev/null +++ b/config/sneakers.yml @@ -0,0 +1,9 @@ +default: &default + foo: 1 + bunny_options: + bar: 2 + log_level: warn + +development: *default +test: *default +production: *default diff --git a/environments/development.rb b/environments/development.rb index e4a80d2..2ea391e 100644 --- a/environments/development.rb +++ b/environments/development.rb @@ -6,12 +6,8 @@ require "active_job" require "active_record" -def Rails.root - Pathname.new(__dir__).join("..") -end - ActiveJob::Base.queue_adapter = :inline -ActiveJob::Base.logger = Logger.new("/dev/null") +ActiveJob::Base.logger = Logger.new(nil) Rabbit.config.project_id = "test_project_id" Rabbit.config.group_id = "test_group_id" diff --git a/lib/rabbit.rb b/lib/rabbit.rb index e24a896..3009689 100644 --- a/lib/rabbit.rb +++ b/lib/rabbit.rb @@ -32,15 +32,15 @@ class Config attribute :backoff_handler_max_retries, Integer, default: 6 attribute :receive_logger, default: lambda { - Logger.new(Rails.root.join("log", "incoming_rabbit_messages.log")) + Logger.new(Rabbit.root.join("log", "incoming_rabbit_messages.log")) } attribute :publish_logger, default: lambda { - Logger.new(Rails.root.join("log", "rabbit.log")) + Logger.new(Rabbit.root.join("log", "rabbit.log")) } attribute :malformed_logger, default: lambda { - Logger.new(Rails.root.join("log", "malformed_messages.log")) + Logger.new(Rabbit.root.join("log", "malformed_messages.log")) } def validate! @@ -48,7 +48,7 @@ def validate! raise InvalidConfig, "missing group_id" unless group_id raise InvalidConfig, "missing exception_notifier" unless exception_notifier - unless environment.in? %i[test development production] + unless %i[test development production].include?(environment) raise "environment should be one of (test, development, production)" end end @@ -74,6 +74,23 @@ def config @config end + def root + if defined?(Rails) + Rails.root + else + Pathname.new(Dir.pwd) + end + end + + def sneakers_config + if defined?(Rails) + Rails.application.config_for("sneakers") + else + config = YAML.load_file("config/sneakers.yml", aliases: true) + config[Rabbit.config.environment.to_s].to_h.symbolize_keys + end + end + def configure yield(config) config.validate! diff --git a/lib/rabbit/daemon.rb b/lib/rabbit/daemon.rb index 60eb462..612f485 100644 --- a/lib/rabbit/daemon.rb +++ b/lib/rabbit/daemon.rb @@ -1,7 +1,6 @@ # frozen_string_literal: true require "sneakers" -require "lamian" require "sneakers/runner" require "rabbit/extensions/bunny/channel" @@ -12,12 +11,6 @@ module Daemon extend self def run(logger: Sneakers.logger) - unless logger - logger = Logger.new(Rails.root.join("log", "sneakers.log")) - logger.level = Logger::DEBUG - Lamian.extend_logger(logger) - end - self.logger = logger Sneakers.configure(**sneakers_config(logger: logger)) @@ -30,7 +23,7 @@ def run(logger: Sneakers.logger) end def config - @config ||= Rails.application.config_for("sneakers").symbolize_keys + @config ||= Rabbit.sneakers_config end def connection @@ -49,7 +42,7 @@ def connection def sneakers_config(logger:) { connection: connection, - env: Rails.env, + env: Rabbit.config.environment, exchange_type: :direct, exchange: Rabbit.config.app_name, hooks: Rabbit.config.hooks, diff --git a/lib/rabbit/publishing.rb b/lib/rabbit/publishing.rb index 6090d21..4ad22c9 100644 --- a/lib/rabbit/publishing.rb +++ b/lib/rabbit/publishing.rb @@ -40,8 +40,8 @@ def create_queue_if_not_exists(channel, message) end def create_client - config = Rails.application.config_for("sneakers") rescue {} - config = config["bunny_options"].to_h.symbolize_keys + config = Rabbit.sneakers_config + config = config[:bunny_options].to_h.symbolize_keys Bunny.new(config).start end diff --git a/lib/rabbit/receiving/job.rb b/lib/rabbit/receiving/job.rb index b4f4a14..b07869e 100644 --- a/lib/rabbit/receiving/job.rb +++ b/lib/rabbit/receiving/job.rb @@ -1,17 +1,16 @@ # frozen_string_literal: true -require "lamian" -require "active_job" - require "rabbit" require "rabbit/receiving" require "rabbit/receiving/message" require "rabbit/receiving/handler_resolver" require "rabbit/receiving/malformed_message" -class Rabbit::Receiving::Job < ActiveJob::Base - def perform(message, arguments) - Lamian.run do +begin + require "active_job" + + class Rabbit::Receiving::Job < ActiveJob::Base + def perform(message, arguments) message = Rabbit::Receiving::Message.build(message, arguments) handler = Rabbit::Receiving::HandlerResolver.handler_for(message) handler.new(message).call @@ -20,4 +19,6 @@ def perform(message, arguments) Rabbit.config.exception_notifier.call(error) end end +rescue LoadError + warn "ActiveJob not found! You will need to define Rabbit::Receiving::Job class by yourself." end diff --git a/lib/rabbit/version.rb b/lib/rabbit/version.rb index 384cb84..4d1681b 100644 --- a/lib/rabbit/version.rb +++ b/lib/rabbit/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module Rabbit - VERSION = "1.2.0" + VERSION = "1.3.0" end diff --git a/rabbit_messaging.gemspec b/rabbit_messaging.gemspec index 3d066ce..930582b 100644 --- a/rabbit_messaging.gemspec +++ b/rabbit_messaging.gemspec @@ -20,8 +20,6 @@ Gem::Specification.new do |spec| spec.require_paths = ["lib"] spec.add_dependency "bunny", "~> 2.0" - spec.add_dependency "kicks", "~> 3" - spec.add_dependency "lamian" - spec.add_dependency "rails", ">= 6.1" + spec.add_dependency "kicks" spec.add_dependency "tainbox" end diff --git a/spec/units/rabbit/daemon_spec.rb b/spec/units/rabbit/daemon_spec.rb index 6c94ff4..5e55eb4 100644 --- a/spec/units/rabbit/daemon_spec.rb +++ b/spec/units/rabbit/daemon_spec.rb @@ -2,43 +2,29 @@ RSpec.describe Rabbit::Daemon do before do - allow(app_double).to receive(:config_for).with("sneakers").and_return(sneakers_config) allow(runner_double).to receive(:run) allow(logger_double).to receive(:dup).and_return(sneaker_logger_double) allow(sneaker_logger_double).to receive(:level=) allow(Bunny).to receive(:new).and_return(bunny_double) - allow(Rails).to receive(:application).and_return(app_double) - allow(Rails).to receive(:env).and_return("test") allow(Sneakers).to receive(:configure) allow(Sneakers::Runner).to receive(:new).and_return(runner_double) allow(Rabbit::Receiving::Worker).to receive(:from_queue) end - let(:app_double) { double(:rails_app) } let(:worker_double) { double(:receiving_worker) } let(:runner_double) { double(:sneakers_runner) } let(:sneaker_logger_double) { double(:sneaker_logger) } let(:logger_double) { double(:logger) } let(:bunny_double) { double(:bunny) } - let(:sneakers_config) do - { - foo: 1, - bunny_options: { - bar: 2, - log_level: "warn", - }, - } - end - it "setups sneakers properly and runs daemon" do Rabbit::Daemon.run(logger: logger_double) expect(Sneakers).to have_received(:configure).with( connection: bunny_double, - env: "test", + env: :production, exchange_type: :direct, exchange: "test_group_id.test_project_id", hooks: {},