Class: EventHub::Processor

Inherits:
Object
  • Object
show all
Includes:
Helper
Defined in:
lib/eventhub/processor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helper

#class_to_array, #duration, #format_string, #get_host, #get_ip_adresses, #now_stamp

Constructor Details

#initialize(name = nil) ⇒ Processor

Returns a new instance of Processor.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/eventhub/processor.rb', line 12

def initialize(name=nil)
  @name = name || class_to_array(self.class)[1..-1].join(".")
  @folder = Dir.pwd

  # Variables used for heartbeat statistics
  @started = Time.now
  @messages_successful           = 0
  @messages_unsuccessful         = 0
  @messages_average_size         = 0
  @messages_average_process_time = 0
  @first_message                 = true

  @channel_receiver = nil
  @channel_sender    = nil
  @restart = true
end

Instance Attribute Details

#folderObject

Returns the value of attribute folder.



4
5
6
# File 'lib/eventhub/processor.rb', line 4

def folder
  @folder
end

#nameObject

Returns the value of attribute name.



4
5
6
# File 'lib/eventhub/processor.rb', line 4

def name
  @name
end

Instance Method Details

#configurationObject



29
30
31
# File 'lib/eventhub/processor.rb', line 29

def configuration
  EventHub::Configuration.instance.data
end

#connection_settingsObject



53
54
55
# File 'lib/eventhub/processor.rb', line 53

def connection_settings
  { user: server_user, password: server_password, host: server_host, vhost: server_vhost }
end

#handle_message(metadata, payload) ⇒ Object



179
180
181
# File 'lib/eventhub/processor.rb', line 179

def handle_message(,payload)
  raise "Please implement method in derived class"
end

#heartbeatObject



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/eventhub/processor.rb', line 207

def heartbeat
  message = Message.new
  message.origin_module_id   = @name
  message.origin_type      = "processor"
  message.origin_site_id     = 'global'

  message.process_name     = 'event_hub.heartbeat'

  now = Time.now
  message.body = {
     version:               self.version,
     heartbeat: {             
     started:               now_stamp(@started),
     stamp_last_beat:       now_stamp(now), 
     uptime:                duration(now-@started),
     heartbeat_cycle_in_s:  self.heartbeat_cycle_in_s,
     served_queues:         [self.listener_queue],
     host:                  get_host,
     ip_adresses:           get_ip_adresses,
     messages: {
      total:                @messages_successful+@messages_unsuccessful,
      successful:           @messages_successful,
      unsuccessful:         @messages_unsuccessful,
      average_size:         @messages_average_size,
      average_process_time: @messages_average_process_time 
      }
    }
  }

  # send heartbeat message
  send_message(message)

  EventMachine.add_timer(self.heartbeat_cycle_in_s) { heartbeat }

end

#heartbeat_cycle_in_sObject



69
70
71
# File 'lib/eventhub/processor.rb', line 69

def heartbeat_cycle_in_s
  configuration.get('processor.heartbeat_cycle_in_s') || 300
end

#listener_queueObject



57
58
59
# File 'lib/eventhub/processor.rb', line 57

def listener_queue
  configuration.get('processor.listener_queue') || 'undefined_listener_queue'
end

#restart_in_sObject



65
66
67
# File 'lib/eventhub/processor.rb', line 65

def restart_in_s
  configuration.get('processor.restart_in_s') || 15
end

#send_message(message, exchange_name = EH_X_INBOUND) ⇒ Object

send message



244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/eventhub/processor.rb', line 244

def send_message(message,exchange_name=EH_X_INBOUND)

  if @channel_sender.nil? || !@channel_sender.open?
    @channel_sender = AMQP::Channel.new(@connection, prefetch: 1)

    # use publisher confirm
    @channel_sender.confirm_select
      
    # @channel.on_error { |ch, channel_close| EventHub.logger.error "Oops! a channel-level exception: #{channel_close.reply_text}" }
      # @channel.on_ack   { |basic_ack| EventHub.logger.info "Received basic_ack: multiple = #{basic_ack.multiple}, delivery_tag = #{basic_ack.delivery_tag}" }
  end

  exchange = @channel_sender.direct(exchange_name, :durable => true, :auto_delete => false) 
    exchange.publish(message.to_json, :persistent => true)
end

#server_hostObject



33
34
35
# File 'lib/eventhub/processor.rb', line 33

def server_host
  configuration.get('server.host') || 'localhost'
end

#server_management_portObject



45
46
47
# File 'lib/eventhub/processor.rb', line 45

def server_management_port
  configuration.get('server.management_port') || 15672
end

#server_passwordObject



41
42
43
# File 'lib/eventhub/processor.rb', line 41

def server_password
  configuration.get('server.password') || 'admin'
end

#server_userObject



37
38
39
# File 'lib/eventhub/processor.rb', line 37

def server_user
  configuration.get('server.user') || 'admin'
end

#server_vhostObject



49
50
51
# File 'lib/eventhub/processor.rb', line 49

def server_vhost
  configuration.get('server.vhost') || 'event_hub'
end

#sleep_break(seconds) ⇒ Object

breaks after n seconds or after interrupt



260
261
262
263
264
265
266
# File 'lib/eventhub/processor.rb', line 260

def sleep_break( seconds ) # breaks after n seconds or after interrupt
    while (seconds > 0)
      sleep(1)
      seconds -= 1
      break unless @restart
    end
end

#start(detached = false) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/eventhub/processor.rb', line 73

def start(detached=false)
  daemonize if detached

  EventHub.logger.info("Processor [#{@name}] base folder [#{@folder}]")

  while @restart

    begin 
      AMQP.start(self.connection_settings) do |connection, open_ok|

        @connection = connection
        
        # deal with tcp connection issues
        @connection.on_tcp_connection_loss do |conn, settings|
          EventHub.logger.warn("Processor lost tcp connection. Trying to restart in #{self.restart_in_s} seconds...")
          stop_processor(true)
        end

        # create channel
        @channel_receiver = AMQP::Channel.new(@connection, prefetch: 1)

        # connect to queue
        @queue = @channel_receiver.queue(self.listener_queue, durable: true, auto_delete: false)

        # subscribe to queue
        @queue.subscribe(:ack => true) do |, payload|     
         begin
           start_stamp = Time.now
           messages_to_send = []

           # try to convert to Evenhub message
           message = Message.from_json(payload)
           EventHub.logger.info("-> #{message.to_s}")

           if message.status_code == STATUS_INVALID
             messages_to_send << message
             EventHub.logger.info("-> #{message.to_s} => Put to queue [#{EH_X_INBOUND}].")
           else
              # pass received message to handler or dervied handler
              messages_to_send = Array(handle_message(message))
            end

           # forward invalid or returned messages to dispatcher
            messages_to_send.each do |message|
              send_message(message)
            end
            @channel_receiver.acknowledge(.delivery_tag)

            # collect statistics for the heartbeat
            @messages_successful += 1
            if @first_message
              @messages_average_process_time = Time.now - start_stamp
              @messages_average_size = payload.size
              @first_message = false
            else
              @messages_average_process_time = (@messages_average_process_time + (Time.now - start_stamp))/2.0
              @messages_average_size = (@messages_average_size + payload.size) / 2.0
            end
            
         rescue => e
           @channel_receiver.reject(.delivery_tag,false)
           @messages_unsuccessful += 1
           EventHub.logger.error("Unexpected exception in handle_message method: #{e}. Message dead lettered.")
            EventHub.logger.save_detailed_error(e)
         end
        end

        EventHub.logger.info("Processor [#{@name}] is listening to vhost [#{self.server_vhost}], queue [#{self.listener_queue}]")

        # Singnal Listening
        Signal.trap("TERM") {stop_processor}
        Signal.trap("INT")  {stop_processor}

        # post_start is a custom post start routing to be overwritten
        post_start

        # Various timers
        EventMachine.add_timer(@watchdog_cycle_in_s) { watchdog }

        heartbeat
      end
    rescue => e
      Signal.trap("TERM") { stop_processor }
      Signal.trap("INT")  { stop_processor }

      id = EventHub.logger.save_detailed_error(e)
      EventHub.logger.error("Unexpected exception: #{e}, see => #{id}. Trying to restart in #{self.restart_in_s} seconds...")
      
      sleep_break self.restart_in_s
    end

  end # while

  # post_start is a custom post start routing to be overwritten
  post_stop

  EventHub.logger.info("Processor [#{@name}] has been stopped")
ensure
  # remove pid file
  begin
    File.delete("#{@folder}/pids/#{@name}.pid") 
  rescue
    # ignore exceptions here
  end
end

#versionObject



8
9
10
# File 'lib/eventhub/processor.rb', line 8

def version
  "1.0.0"
end

#watchdogObject



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/eventhub/processor.rb', line 183

def watchdog
  begin
    response = RestClient.get "http://#{self.server_user}:#{self.server_password}@#{self.server_host}:#{self.server_management_port}/api/queues/#{self.server_vhost}/#{self.listener_queue}/bindings", { :content_type => :json}
      data = JSON.parse(response.body)
  
      if response.code != 200
        EventHub.logger.warn("Watchdog: Server did not answered properly. Trying to restart in #{self.restart_in_s} seconds...")
        EventMachine.add_timer(self.restart_in_s) { stop_processor(true) }
      elsif data.size == 0
        EventHub.logger.warn("Watchdog: Something is wrong with the vhost, queue, and/or bindings. Trying to restart in #{self.restart_in_s} seconds...")
        EventMachine.add_timer(self.restart_in_s) { stop_processor(true) }
        # does it make sence ? Needs maybe more checks in future
      else
        # Watchdog is happy :-)
      # add timer for next check
      EventMachine.add_timer(self.watchdog_cycle_in_s) { watchdog }
    end 
      
  rescue => e
    EventHub.logger.error("Watchdog: Unexpected exception: #{e}. Trying to restart in #{self.restart_in_s} seconds...")
    stop_processor
  end
end

#watchdog_cycle_in_sObject



61
62
63
# File 'lib/eventhub/processor.rb', line 61

def watchdog_cycle_in_s
  configuration.get('processor.watchdog_cycle_is_s') || 15
end