Module: Legion::Transport::Common

Includes:
Logging::Helper
Included in:
Consumer, Exchange, Message, Queue
Defined in:
lib/legion/transport/common.rb

Constant Summary collapse

NAMESPACE_BOUNDARIES =
%w[Actor Actors Runners Helpers Transport Data Queues Queue Exchanges Exchange Messages Message].freeze

Instance Method Summary collapse

Instance Method Details

#channelObject

rubocop:enable all



57
58
59
# File 'lib/legion/transport/common.rb', line 57

def channel
  @channel ||= Legion::Transport::Connection.channel
end

#channel_open?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/legion/transport/common.rb', line 17

def channel_open?
  channel.open?
end

#closeObject



68
69
70
71
# File 'lib/legion/transport/common.rb', line 68

def close
  log.warn 'close called, but method is called close!'
  close!
end

#close!Object



61
62
63
64
65
66
# File 'lib/legion/transport/common.rb', line 61

def close!
  log.info 'close! called'
  return false unless Legion::Transport::Connection.channel_open?

  Legion::Transport::Connection.channel.close
end

#deep_merge(original, new) ⇒ Object

rubocop:disable all



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/legion/transport/common.rb', line 34

def deep_merge(original, new)
  {} unless original.is_a?(Hash) && new.is_a?(Hash)
  original unless new.is_a? Hash
  new unless original.is_a? Hash
  new if original.nil? || original.empty?
  original if new.nil? || new.empty?

  new.each do |k, v|
    unless original.key?(k)
      original[k] = v
      next
    end

    original[k.to_sym] = if [original[k.to_sym], new[k.to_sym]].all? { |a| a.is_a? Hash }
                    deep_merge(original[k], new[k])
                  else
                    new[k]
                  end
  end
  original
end

#generate_consumer_tag(lex_name: nil, runner_name: nil, thread: Thread.current.object_id) ⇒ Object



73
74
75
76
77
78
79
80
# File 'lib/legion/transport/common.rb', line 73

def generate_consumer_tag(lex_name: nil, runner_name: nil, thread: Thread.current.object_id)
  tag = "#{Legion::Settings[:client][:name]}_"
  tag.concat("#{lex_name}_") unless lex_name.nil?
  tag.concat("#{runner_name}_") unless runner_name.nil?
  tag.concat("#{thread}_")
  tag.concat(SecureRandom.hex)
  tag
end

#open_channel(_options = {}) ⇒ Object



13
14
15
# File 'lib/legion/transport/common.rb', line 13

def open_channel(_options = {})
  @channel = Legion::Transport::Connection.channel
end

#options_builder(first, *args) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/legion/transport/common.rb', line 21

def options_builder(first, *args)
  final_options = nil
  args.each do |option|
    final_options = if final_options.nil?
                      deep_merge(first, option)
                    else
                      deep_merge(final_options, option)
                    end
  end
  final_options
end