Class: Trabox::Relay::Relayer

Inherits:
Object
  • Object
show all
Defined in:
lib/trabox/relay/relayer.rb

Instance Method Summary collapse

Constructor Details

#initialize(publisher, limit: DEFAULT_SELECT_LIMIT, lock: true) ⇒ Relayer

Returns a new instance of Relayer.

Parameters:

  • publisher (Trabox::PubSub::Publisher)
  • limit (Integer) (defaults to: DEFAULT_SELECT_LIMIT)

    SELECT文のLIMIT

  • lock (Boolean, String) (defaults to: true)

    ActiveRecord lock argument

Raises:

  • (ArgumentError)


7
8
9
10
11
12
13
# File 'lib/trabox/relay/relayer.rb', line 7

def initialize(publisher, limit: DEFAULT_SELECT_LIMIT, lock: true)
  raise ArgumentError unless publisher.respond_to?(:publish)

  @publisher = publisher
  @limit = limit
  @lock = lock
end

Instance Method Details

#performObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/trabox/relay/relayer.rb', line 15

def perform
  RelayableModels.list.each do |model|
    model.transaction do
      unpublished_events = begin
        model.lock(@lock).unpublished limit: @limit
      rescue StandardError
        Metric.increment('find_events_error_count',
                         tags: ["event-type:#{model.name.underscore}"])
        raise
      end

      unpublished_events.each do |event|
        Metric.increment('unpublished_event_count',
                         tags: ["event-type:#{event.class.name.underscore}", "event-id:#{event.id}"])

        publish_and_commit(event)

        Metric.increment('published_event_count',
                         tags: ["event-type:#{event.class.name.underscore}", "event-id:#{event.id}"])
      end

      Rails.logger.info "Published events. (#{model.name.underscore}=#{unpublished_events.size})"
    end
  end
end