Module: Flu

Defined in:
lib/flu-rails.rb,
lib/flu-rails/util.rb,
lib/flu-rails/event.rb,
lib/flu-rails/errors.rb,
lib/flu-rails/railtie.rb,
lib/flu-rails/version.rb,
lib/flu-rails/core_ext.rb,
lib/flu-rails/configuration.rb,
lib/flu-rails/event_factory.rb,
lib/flu-rails/event_publisher.rb,
lib/flu-rails/queue_repository.rb,
lib/flu-rails/active_record_extender.rb,
lib/flu-rails/action_controller_extender.rb,
lib/flu-rails/dummy/in_memory_event_publisher.rb

Defined Under Namespace

Modules: Dummy, Util Classes: ActionControllerExtender, ActiveRecordExtender, Configuration, CoreExt, Error, Event, EventFactory, EventPublisher, NotConnectedError, QueueRepository, Railtie

Constant Summary collapse

VERSION =
"8.0.5"

Class Method Summary collapse

Class Method Details

.configObject



23
24
25
# File 'lib/flu-rails.rb', line 23

def self.config
  @configuration
end

.configure {|@configuration ||= Flu::Configuration.new| ... } ⇒ Object

Yields:



19
20
21
# File 'lib/flu-rails.rb', line 19

def self.configure
  yield @configuration ||= Flu::Configuration.new
end

.create_event_publisher(configuration) ⇒ Object



61
62
63
64
65
66
67
68
69
# File 'lib/flu-rails.rb', line 61

def self.create_event_publisher(configuration)
  if is_testing_environment?
    logger.info("Loading Flu with a dummy event publisher (this will not connect any exchange)")
    require_relative "flu-rails/dummy/in_memory_event_publisher"
    Flu::Dummy::InMemoryEventPublisher.new(@configuration)
  else
    Flu::EventPublisher.new(@configuration)
  end
end

.default_application_nameObject



52
53
54
55
56
57
58
59
# File 'lib/flu-rails.rb', line 52

def self.default_application_name
  if defined?(Rails) && Rails.respond_to?(:application)
    application = Rails.application
    application.nil? ? nil : application.class.module_parent_name
  else
    nil
  end
end

.event_factoryObject



31
32
33
# File 'lib/flu-rails.rb', line 31

def self.event_factory
  @event_factory
end

.event_publisherObject



35
36
37
# File 'lib/flu-rails.rb', line 35

def self.event_publisher
  @event_publisher
end

.extend_models_and_controllersObject



78
79
80
81
# File 'lib/flu-rails.rb', line 78

def self.extend_models_and_controllers
  Flu::CoreExt.extend_model_classes(@event_factory, @event_publisher)
  Flu::CoreExt.extend_controller_classes(@event_factory, @event_publisher, @logger)
end

.initObject



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/flu-rails.rb', line 39

def self.init
  @configuration.application_name ||= default_application_name
  raise "configuration.application_name must not be nil" if @configuration.application_name.nil?
  @logger          = @configuration.logger
  @event_factory   = Flu::EventFactory.new(@configuration)
  # The railtie re-runs 'init' on every code reload. The publisher being replaced owns an open
  # connection, its channel and Bunny's heartbeat thread: dropping the reference to it without
  # closing it leaks all three for the lifetime of the process.
  @event_publisher&.disconnect
  @event_publisher = create_event_publisher(@configuration)
  extend_models_and_controllers
end

.is_testing_environment?Boolean

Returns:

  • (Boolean)


71
72
73
74
75
76
# File 'lib/flu-rails.rb', line 71

def self.is_testing_environment?
  # 'defined?(Rails)' alone is not enough: gems such as 'rails-html-sanitizer' define an empty
  # 'Rails' namespace, so the constant can exist without railties ever defining 'Rails.env'.
  return false unless defined?(Rails) && Rails.respond_to?(:env)
  config.development_environments.include?(Rails.env)
end

.load_configurationObject



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/flu-rails.rb', line 87

def self.load_configuration
  configure do |config|
    config.development_environments       = []
    config.rejected_user_agents           = []
    config.logger                         = ::Logger.new(STDOUT)
    config.rabbitmq_host                  = "localhost"
    config.rabbitmq_vhost                 = "/"
    config.rabbitmq_port                  = 5672
    config.rabbitmq_management_scheme     = "http"
    config.rabbitmq_management_port       = 15672
    config.rabbitmq_user                  = ""
    config.rabbitmq_password              = ""
    config.rabbitmq_exchange_name         = "events"
    config.rabbitmq_exchange_durable      = true
    config.auto_connect_to_exchange       = true
    config.default_ignored_model_changes  = [:password, :password_confirmation, :created_at, :updated_at]
    config.default_ignored_request_params = [:password, :password_confirmation, :controller, :action]
    config.application_name               = nil
    config.bunny_options                  = {}
  end
end

.loggerObject



27
28
29
# File 'lib/flu-rails.rb', line 27

def self.logger
  @logger
end

.startObject



83
84
85
# File 'lib/flu-rails.rb', line 83

def self.start
  @event_publisher.connect if config.auto_connect_to_exchange
end