Class: Renalware::Feeds::MessageProcessor

Inherits:
Object
  • Object
show all
Includes:
Broadcasting, Wisper::Publisher
Defined in:
app/models/renalware/feeds/message_processor.rb

Overview

Responsible for coordinating the processing sequences of a raw HL7 message.

Instance Method Summary collapse

Methods included from Broadcasting

#broadcasting_to_configured_subscribers

Instance Method Details

#call(raw_message) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'app/models/renalware/feeds/message_processor.rb', line 16

def call(raw_message)
  feed_message = nil

  # We want to wrap message processing in a transaction because if message processing
  # fails we don't want to leave an unprocessed message in the feed_messages table.
  # If we did, and the same FeedJob retires a few minutes later, if will try to save to
  # feed_messages with the same MD5 body_hash (the message is identical to one already saved)
  # resulting in unique key violation.
  # Using a transaction here prevents any orphaned records if there is an error.
  # However we should be aware that any listeners raising an error will prevent successful
  # in all other listeners. So a listener should be careful to catch errors and not re-raise
  # them, or use the :message_processed message (lower down) which is safer.
  ActiveRecord::Base.transaction do
    hl7_message = build_hl7_object_from(raw_message)
    feed_message = persist_message(hl7_message)
    broadcast(
      :message_arrived,
      hl7_message: hl7_message,
      feed_message: feed_message
    )
  end

  # Another event, this one letting anyone interested know that a message been successfully
  # processed. They might want to forward the message on somewhere else for instance.
  # Think Diaverum.
  # The crucial difference here is that an exception raised by a listener does not prevent the
  # main processing of the HL7 message from being rolled back.
  # They could use this for message processing but we'd rather it was used for post-processing
  # e.g. forwarding, logging etc.
  # Its is recommended here to use an async listener - see example in renalware-diaverum
  # - so that any error in the listener has its own try mechansim and does not cause the
  # current job to retry,
  broadcast(:message_processed, feed_message: feed_message)
rescue StandardError => exception
  notify_exception(exception)
  raise exception
end