Module: Sneakers::Migrator
- Defined in:
- lib/sneakers/migrator.rb,
lib/sneakers/migrator/version.rb
Overview
Do not define a module named Queue here to avoid conflict with Sneakers::Queue (which is a class, not a module)
Defined Under Namespace
Classes: Error
Constant Summary collapse
- VERSION =
"0.1.3"
Class Method Summary collapse
-
.migrate!(amqp_url:, amqp_api_url:, subscriber_paths: [], logger: nil) ⇒ Object
Main migration entrypoint.
Class Method Details
.migrate!(amqp_url:, amqp_api_url:, subscriber_paths: [], logger: nil) ⇒ Object
Main migration entrypoint
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/sneakers/migrator.rb', line 16 def migrate!(amqp_url:, amqp_api_url:, subscriber_paths: [], logger: nil) logger ||= $stdout # Try to require sneakers or kicks begin require "sneakers" rescue LoadError begin require "kicks" rescue LoadError raise Error, "You must have either the 'sneakers' or 'kicks' gem installed to use subscriber discovery." end end # Load all subscribers (recursively) subscriber_paths.each do |path| Dir[File.join(path, "**", "*.rb")].sort.each do |f| logger.puts "Migrator: Loading subscriber file: #{f}" begin require f rescue LoadError => e logger.puts "Migrator: Failed to load subscriber file #{f}: #{e.}" rescue StandardError => e logger.puts "Migrator: Error loading subscriber file #{f}: #{e.class}: #{e.}" end end end # Find all worker classes for the loaded framework subscribers = ObjectSpace.each_object(Class).select do |klass| klass.included_modules.include?(::Sneakers::Worker) rescue StandardError false end # output found subscribers logger.puts "Migrator: Found #{subscribers.size} subscribers:" subscribers.each { |s| logger.puts " - #{s.name}" } queues = subscribers.map do |klass| opts = klass.instance_variable_get(:@queue_opts) || {} name = klass.instance_variable_get(:@queue_name) || (klass.const_defined?(:QUEUE_NAME) ? klass.const_get(:QUEUE_NAME) : nil) || klass.name.gsub("::", "_").gsub(/([a-z\d])([A-Z])/, '\1_\2').downcase { name: name, exchange: opts[:exchange] || "domain_events", routing_keys: opts[:routing_key] || [], arguments: opts[:arguments] || {}, durable: opts.fetch(:durable, true) }.tap { |q| q[:arguments]["x-queue-type"] ||= "classic" } end.uniq { |q| q[:name] } conn = Bunny.new(amqp_url) conn.start ch = conn.create_channel # Remove unused api_uri and fix variable usage rabbitmq_api_user = URI.parse(amqp_api_url).user rabbitmq_api_pass = URI.parse(amqp_api_url).password all_queues = fetch_all_queues(amqp_api_url, rabbitmq_api_user, rabbitmq_api_pass) queues.each do |q| existing_queue_info = all_queues.find { |info| info["name"] == q[:name] } if existing_queue_info.nil? logger.puts "Migrator: Queue #{q[:name]} does not exist, creating..." ch.queue(q[:name], durable: q[:durable], arguments: q[:arguments]) next end migrate_queue!(q, ch, amqp_api_url, rabbitmq_api_user, rabbitmq_api_pass, logger, existing_queue_info) end end |