Class: Banter::Publisher

Inherits:
Object
  • Object
show all
Defined in:
lib/banter/publisher.rb

Constant Summary collapse

@@publisher =
nil

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(exchange = nil) ⇒ Publisher

Returns a new instance of Publisher.



15
16
17
18
19
20
# File 'lib/banter/publisher.rb', line 15

def initialize(exchange = nil)
  @exchange = exchange || Banter::Configuration.exchange_name
  @disabled = false
  @batch_messages = false
  @stack_depth = 0
end

Instance Attribute Details

#channelObject (readonly)

Returns the value of attribute channel.



4
5
6
# File 'lib/banter/publisher.rb', line 4

def channel
  @channel
end

#publisherObject (readonly)

Returns the value of attribute publisher.



3
4
5
# File 'lib/banter/publisher.rb', line 3

def publisher
  @publisher
end

Class Method Details

.instanceObject



8
9
10
11
12
13
# File 'lib/banter/publisher.rb', line 8

def self.instance
  if @@publisher.nil?
    @@publisher = ::Banter::Publisher.new
  end
  @@publisher
end

Instance Method Details

#delay_messagesObject



27
28
29
30
31
32
# File 'lib/banter/publisher.rb', line 27

def delay_messages
  delay_start
  yield
ensure
  delay_execute
end

#enable(value) ⇒ Object



22
23
24
25
# File 'lib/banter/publisher.rb', line 22

def enable(value)
  @disabled = !value
  @disabled
end

#publish(context, key, payload) ⇒ Object



60
61
62
63
64
65
66
67
68
69
# File 'lib/banter/publisher.rb', line 60

def publish(context, key, payload)
  routing_key = "#{@exchange}.#{key}"
  envelope    = ::Banter::Message.new.serialize(context, key, payload)

  if @batch_messages
    add_message(routing_key, envelope)
  else
    execute_publish(routing_key, envelope)
  end
end

#startObject



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
# File 'lib/banter/publisher.rb', line 34

def start
  unless Configuration.push_enabled
    @disabled = true
    return
  end

  # grab server configuration from initialization file somewhere
  begin
    @connection = Bunny.new(Configuration.connection)
    @connection.start

    @channel   = @connection.create_channel
    @publisher = @channel.topic(@exchange, :durable => true, :auto_delete => false)

  rescue => e
    Airbrake.notify(e, parameters: { message: e.message }, environment_name: ENV['RAILS_ENV'])
    return
  end

  @publisher.on_return do |return_info, properties, content|
    # contents are already transformed into message that we want to send
    Banter::RabbitLogger.failed_publish(return_info[:routing_key], properties, Hashie::Mash.new(::JSON.parse(content)))
  end

end