Module: OnsOnRails

Defined in:
lib/ons_on_rails.rb,
lib/ons_on_rails/version.rb,
lib/ons_on_rails/publisher.rb,
lib/ons_on_rails/subscriber.rb,
lib/ons_on_rails/publishers/tcp.rb,
lib/ons_on_rails/publishers/test.rb

Overview

.

Defined Under Namespace

Modules: Publishers, Subscriber Classes: Publisher

Constant Summary collapse

VERSION =
'1.0.0'.freeze

Class Method Summary collapse

Class Method Details

.create_publisher(publisher_name, backend: :tcp) ⇒ Object

Create a Publisher.

Parameters:

  • publisher_name (Symbol, String)

    the publisher's name

  • backend (#to_s) (defaults to: :tcp)

    backend name, such as :tcp, :test, etc.

See Also:


49
50
51
52
53
54
55
56
# File 'lib/ons_on_rails.rb', line 49

def self.create_publisher(publisher_name, backend: :tcp)
  options ||= begin
    opts = OnsOnRails.ons_default_options
    opts.slice(:access_key, :secret_key).merge(opts.fetch(publisher_name.to_s.underscore.to_sym, {}))
  end

  OnsOnRails::Publisher.new(backend, options)
end

.loggerObject

Get the global logger.


13
14
15
# File 'lib/ons_on_rails.rb', line 13

def self.logger
  @logger ||= initialize_logger
end

.ons_default_optionsObject

Get the ons default options.


25
26
27
# File 'lib/ons_on_rails.rb', line 25

def self.ons_default_options
  @ons_default_options ||= initialize_ons_default_options
end

.run_subscriber_as_a_daemon(subscriber_class_name, app_path) ⇒ Object

Run a subscriber as a separate process.

Parameters:

  • subscriber_class_name (Symbol, String)

    the subscriber's class name

  • app_path (String)

    the Rails root directory path


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
87
# File 'lib/ons_on_rails.rb', line 62

def self.run_subscriber_as_a_daemon(subscriber_class_name, app_path)
  options = {
    backtrace: true,
    dir_mode: :normal,
    dir: File.join(app_path, 'tmp', 'pids'),
    log_dir: File.join(app_path, 'log'),
    log_output: true
  }

  subscriber_class_name = subscriber_class_name.to_s.camelize
  Daemons.run_proc(subscriber_class_name.underscore, options) do
    require File.join(app_path, 'config', 'environment')
    require 'ons' unless defined?(Ons)

    subscriber_class = subscriber_class_name.constantize
    subscriber_class.check_subscriber_definition!

    options = subscriber_class.ons_options
    Ons::Consumer.new(options.fetch(:access_key), options.fetch(:secret_key), options.fetch(:consumer_id))
                 .subscribe(options.fetch(:topic), options.fetch(:tag), &->(message) { subscriber_class.consume(message) })
                 .start

    Ons.register_cleanup_hooks
    Ons.loop_forever
  end
end