Module: Messaging::Write

Included in:
Controls::Write::Example, Substitute::Write
Defined in:
lib/messaging/write.rb,
lib/messaging/write/substitute.rb

Defined Under Namespace

Modules: Build, Call, Configure, Substitute, Telemetry Classes: Error

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(cls) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/messaging/write.rb', line 5

def self.included(cls)
  cls.class_exec do
    include Dependency
    include TemplateMethod
    include Log::Dependency

    dependency :message_writer
    dependency :telemetry, ::Telemetry

    extend Build
    extend Call
    extend Configure

    template_method! :configure

    const_set :Substitute, Substitute
  end
end

.register_telemetry_sink(writer) ⇒ Object



128
129
130
131
132
# File 'lib/messaging/write.rb', line 128

def self.register_telemetry_sink(writer)
  sink = Telemetry.sink
  writer.telemetry.register sink
  sink
end

Instance Method Details

#call(message_or_batch, stream_name, expected_version: nil, reply_stream_name: nil) ⇒ Object Also known as: write



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/messaging/write.rb', line 48

def call(message_or_batch, stream_name, expected_version: nil, reply_stream_name: nil)
  unless message_or_batch.is_a? Array
    logger.trace(tag: :write) { "Writing message (Type: #{message_or_batch.class.message_type}, Stream Name: #{stream_name}, Expected Version: #{expected_version.inspect}, Reply Stream Name: #{reply_stream_name.inspect})" }
  else
    logger.trace(tag: :write) do
      message_types = message_or_batch.map {|message| message.class.message_type }.uniq.join(', ')
      "Writing batch (Types: #{message_types}, Stream Name: #{stream_name}, Expected Version: #{expected_version.inspect}, Reply Stream Name: #{reply_stream_name.inspect})"
    end
  end
  logger.trace(tags: [:data, :message]) { message_or_batch.pretty_inspect }

  message_batch = Array(message_or_batch)

  message_data_batch = message_data_batch(message_batch, reply_stream_name)
## Can be called `position` - Scott, Tue Mar 26 2024
  last_position = message_writer.(message_data_batch, stream_name, expected_version: expected_version)

  unless message_or_batch.is_a? Array
    logger.info(tag: :write) { "Wrote message (Type: #{message_or_batch.class.message_type}, Stream Name: #{stream_name}, Position: #{last_position}, Expected Version: #{expected_version.inspect}, Reply Stream Name: #{reply_stream_name.inspect})" }
  else
    logger.info(tag: :write) do
      message_types = message_or_batch.map {|message| message.class.message_type }.uniq.join(', ')
      "Wrote batch (Types: #{message_types}, Stream Name: #{stream_name}, Position: #{last_position}, Expected Version: #{expected_version.inspect}, Reply Stream Name: #{reply_stream_name.inspect})"
    end
  end
  logger.info(tags: [:data, :message]) { message_data_batch.pretty_inspect }

  message_batch.each do |message|
    telemetry.record :written, Telemetry::Data.new(message, stream_name, expected_version, reply_stream_name)
  end

  last_position
end

#initial(message, stream_name) ⇒ Object Also known as: write_initial



123
124
125
# File 'lib/messaging/write.rb', line 123

def initial(message, stream_name)
  write(message, stream_name, expected_version: :no_stream)
end

#message_data_batch(message_batch, reply_stream_name = nil) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/messaging/write.rb', line 83

def message_data_batch(message_batch, reply_stream_name=nil)
  message_data_batch = []
  message_batch.each do |message|
    unless reply_stream_name.nil?
       message..reply_stream_name = reply_stream_name
    end

    message_data_batch << Message::Export.(message)
  end

  message_data_batch
end

#reply(message) ⇒ Object



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
# File 'lib/messaging/write.rb', line 96

def reply(message)
  if message.is_a? Array
    error_msg = "Cannot reply with a batch"
    logger.error { error_msg }
    raise Error, error_msg
  end

   = message.
  reply_stream_name = .reply_stream_name

  logger.trace(tags: [:write, :reply]) { "Replying (Message Type: #{message.message_type}, Stream Name: #{reply_stream_name.inspect})" }

  if reply_stream_name.nil?
    error_msg = "Message has no reply stream name. Cannot reply. (Message Type: #{message.message_type})"
    logger.error { error_msg }
    logger.error(tags: [:data, :message]) { message.pretty_inspect }
    raise Error, error_msg
  end

  .clear_reply_stream_name

  write(message, reply_stream_name).tap do
    logger.info(tags: [:write, :reply]) { "Replied (Message Type: #{message.message_type}, Stream Name: #{reply_stream_name})" }
    telemetry.record :replied, Telemetry::Data.new(message, reply_stream_name)
  end
end