legion-transport-java 1.1.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (42) hide show
  1. checksums.yaml +7 -0
  2. data/.circleci/config.yml +89 -0
  3. data/.gitignore +13 -0
  4. data/.rspec +1 -0
  5. data/.rubocop.yml +21 -0
  6. data/CHANGELOG.md +9 -0
  7. data/Gemfile +3 -0
  8. data/README.md +3 -0
  9. data/Rakefile +6 -0
  10. data/bin/console +14 -0
  11. data/bin/setup +8 -0
  12. data/bitbucket-pipelines.yml +24 -0
  13. data/legion-transport.gemspec +43 -0
  14. data/lib/legion/transport.rb +21 -0
  15. data/lib/legion/transport/common.rb +76 -0
  16. data/lib/legion/transport/connection.rb +62 -0
  17. data/lib/legion/transport/consumer.rb +13 -0
  18. data/lib/legion/transport/exchange.rb +51 -0
  19. data/lib/legion/transport/exchanges/crypt.rb +11 -0
  20. data/lib/legion/transport/exchanges/lex.rb +11 -0
  21. data/lib/legion/transport/exchanges/node.rb +11 -0
  22. data/lib/legion/transport/exchanges/task.rb +11 -0
  23. data/lib/legion/transport/message.rb +106 -0
  24. data/lib/legion/transport/messages/check_subtask.rb +25 -0
  25. data/lib/legion/transport/messages/dynamic.rb +25 -0
  26. data/lib/legion/transport/messages/lex_register.rb +31 -0
  27. data/lib/legion/transport/messages/node_health.rb +21 -0
  28. data/lib/legion/transport/messages/request_cluster_secret.rb +33 -0
  29. data/lib/legion/transport/messages/subtask.rb +38 -0
  30. data/lib/legion/transport/messages/task_log.rb +36 -0
  31. data/lib/legion/transport/messages/task_update.rb +31 -0
  32. data/lib/legion/transport/queue.rb +72 -0
  33. data/lib/legion/transport/queues/node.rb +15 -0
  34. data/lib/legion/transport/queues/node_crypt.rb +21 -0
  35. data/lib/legion/transport/queues/node_status.rb +21 -0
  36. data/lib/legion/transport/queues/task_log.rb +21 -0
  37. data/lib/legion/transport/queues/task_update.rb +21 -0
  38. data/lib/legion/transport/settings.rb +72 -0
  39. data/lib/legion/transport/version.rb +5 -0
  40. data/settings/transport.json +5 -0
  41. data/sonar-project.properties +12 -0
  42. metadata +269 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: fe045f5f966e8cde47bd41f5972f1083f7395d9887d7a7a7f0fd561ec0a76c29
4
+ data.tar.gz: fce6b203e18c600aa4e7529a45673cb813e800b084f26457499c5066f409552a
5
+ SHA512:
6
+ metadata.gz: a86241556c6639829d0f82e1a5d91af89988a5d5f8ed827da574480fd25dc82ff8005ca3896a15ea05d6e2193f89f61164e7e12f8884020bc19b345483a2da3b
7
+ data.tar.gz: d2994c7a2bf516769432912c2265d51a066854e4bb6d9539328f9ca3eb50887620b060f7092fc49d7c56e687d9f0c9ddbd4fde474113fae4ad0c5c587f4ce889
@@ -0,0 +1,89 @@
1
+ version: 2.1
2
+ orbs:
3
+ ruby: circleci/ruby@0.2.1
4
+ sonarcloud: sonarsource/sonarcloud@1.0.1
5
+
6
+ jobs:
7
+ "rubocop":
8
+ docker:
9
+ - image: circleci/ruby:2.7-node
10
+ steps:
11
+ - checkout
12
+ - ruby/load-cache
13
+ - ruby/install-deps
14
+ - run:
15
+ name: Run Rubocop
16
+ command: bundle exec rubocop
17
+ - ruby/save-cache
18
+ "ruby-two-five":
19
+ docker:
20
+ - image: circleci/ruby:2.5
21
+ - image: rabbitmq:3.7
22
+ steps:
23
+ - checkout
24
+ - ruby/load-cache
25
+ - run:
26
+ name: update bundler
27
+ command: gem update bundler
28
+ - ruby/install-deps
29
+ - ruby/run-tests
30
+ - ruby/save-cache
31
+ "ruby-two-six":
32
+ docker:
33
+ - image: circleci/ruby:2.6
34
+ - image: rabbitmq:3.7
35
+ steps:
36
+ - checkout
37
+ - ruby/load-cache
38
+ - run:
39
+ name: update bundler
40
+ command: gem update bundler
41
+ - ruby/install-deps
42
+ - ruby/run-tests
43
+ - ruby/save-cache
44
+ "ruby-two-seven":
45
+ docker:
46
+ - image: circleci/ruby:2.7
47
+ - image: rabbitmq:3.7
48
+ steps:
49
+ - checkout
50
+ - ruby/load-cache
51
+ - run:
52
+ name: update bundler
53
+ command: gem update bundler
54
+ - ruby/install-deps
55
+ - ruby/run-tests
56
+ - ruby/save-cache
57
+ "sonarcloud":
58
+ docker:
59
+ - image: circleci/ruby:2.7
60
+ - image: rabbitmq:3.7
61
+ steps:
62
+ - checkout
63
+ - ruby/load-cache
64
+ - ruby/install-deps
65
+ - ruby/run-tests
66
+ - run:
67
+ name: Run Rubocop
68
+ command: bundle exec rubocop --format=json --out=rubocop-result.json
69
+ - sonarcloud/scan
70
+ - ruby/save-cache
71
+
72
+ workflows:
73
+ version: 2
74
+ rubocop-rspec:
75
+ jobs:
76
+ - rubocop
77
+ - ruby-two-five:
78
+ requires:
79
+ - rubocop
80
+ - ruby-two-six:
81
+ requires:
82
+ - ruby-two-five
83
+ - ruby-two-seven:
84
+ requires:
85
+ - ruby-two-five
86
+ - sonarcloud:
87
+ requires:
88
+ - ruby-two-seven
89
+ - ruby-two-six
@@ -0,0 +1,13 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /*.log
11
+
12
+ # rspec failure tracking
13
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,21 @@
1
+ Layout/LineLength:
2
+ Max: 120
3
+ Metrics/MethodLength:
4
+ Max: 30
5
+ Metrics/ClassLength:
6
+ Max: 1500
7
+ Metrics/BlockLength:
8
+ Max: 50
9
+ Layout/SpaceAroundEqualsInParameterDefault:
10
+ EnforcedStyle: space
11
+ Style/SymbolArray:
12
+ Enabled: true
13
+ Style/Documentation:
14
+ Enabled: false
15
+ AllCops:
16
+ TargetRubyVersion: 2.5
17
+ NewCops: enable
18
+ Style/FrozenStringLiteralComment:
19
+ Enabled: false
20
+ Naming/FileName:
21
+ Enabled: false
@@ -0,0 +1,9 @@
1
+ # Legion::Transport ChangeLog
2
+
3
+ ## v1.1.3
4
+ * Adding JRuby support via a different gem
5
+ * Adding more logic on choosing which gem to pick for the CONNECTOR
6
+ * Cleaning up some rubocop things
7
+
8
+ ## v1.1.2
9
+ Updating queue auto_delete to be false
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
@@ -0,0 +1,3 @@
1
+ # Legion::Transport
2
+
3
+ Used to connect Legion to RabbitMQ and is a core component of Legion.
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'legion/transport'
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require 'irb'
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,24 @@
1
+ image: ruby:2.5.0
2
+
3
+ pipelines:
4
+ branches:
5
+ master:
6
+ - step:
7
+ caches:
8
+ - bundler
9
+ script:
10
+ - gem install bundle rubocop
11
+ - bundle install
12
+ - rubocop
13
+ - rake
14
+ services:
15
+ - broker
16
+ definitions:
17
+ caches:
18
+ bundler: vendor/bundle
19
+ services:
20
+ broker:
21
+ image: rabbitmq:3
22
+ environment:
23
+ RABBITMQ_DEFAULT_USER: guest
24
+ RABBITMQ_DEFAULT_PASS: guest
@@ -0,0 +1,43 @@
1
+ lib = File.expand_path('lib', __dir__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'legion/transport/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = (RUBY_ENGINE == 'jruby' ? 'legion-transport-java' : 'legion-transport')
7
+ spec.version = Legion::Transport::VERSION
8
+ spec.authors = ['Esity']
9
+ spec.email = ['matthewdiverson@gmail.com']
10
+
11
+ spec.summary = 'Used by Legion to connect with RabbitMQ'
12
+ spec.description = 'The Legion transport gem'
13
+ spec.homepage = 'https://bitbucket.org/legion-io/legion-transport'
14
+ spec.required_ruby_version = '>= 2.5.0'
15
+
16
+ spec.metadata = {
17
+ 'bug_tracker_uri' => 'https://legionio.atlassian.net/projects/TRANSPORT/issues',
18
+ 'changelog_uri' => 'https://bitbucket.org/legion-io/legion-transport/src/master/CHANGELOG.md',
19
+ 'homepage_uri' => 'https://bitbucket.org/legion-io/legion-transport',
20
+ 'wiki_uri' => 'https://bitbucket.org/legion-io/legion-transport/wiki/Home'
21
+ }
22
+
23
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
24
+ f.match(%r{^(test|spec|features)/})
25
+ end
26
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
27
+ spec.require_paths = ['lib']
28
+
29
+ spec.add_development_dependency 'bundler'
30
+ spec.add_development_dependency 'codecov'
31
+ spec.add_development_dependency 'legion-logging'
32
+ spec.add_development_dependency 'legion-settings'
33
+ spec.add_development_dependency 'march_hare' if RUBY_ENGINE == 'jruby'
34
+ spec.add_development_dependency 'rake'
35
+ spec.add_development_dependency 'rspec'
36
+ spec.add_development_dependency 'rspec_junit_formatter'
37
+ spec.add_development_dependency 'rubocop'
38
+ spec.add_development_dependency 'simplecov'
39
+
40
+ spec.add_dependency 'bunny'
41
+ spec.add_dependency 'concurrent-ruby'
42
+ spec.add_dependency 'legion-json'
43
+ end
@@ -0,0 +1,21 @@
1
+ require_relative 'transport/version'
2
+ require_relative 'transport/settings'
3
+
4
+ module Legion
5
+ module Transport
6
+ if RUBY_ENGINE == 'jruby' && defined?(::Marchhare)
7
+ TYPE = 'marchhare'.freeze
8
+ CONNECTOR = ::Marchhare
9
+ else
10
+ require 'bunny'
11
+ TYPE = 'bunny'.freeze
12
+ CONNECTOR = ::Bunny
13
+ end
14
+ end
15
+
16
+ require_relative 'transport/common'
17
+ require_relative 'transport/queue'
18
+ require_relative 'transport/exchange'
19
+ require_relative 'transport/consumer'
20
+ require_relative 'transport/message'
21
+ end
@@ -0,0 +1,76 @@
1
+ require 'legion/transport/connection'
2
+
3
+ module Legion
4
+ module Transport
5
+ module Common
6
+ def open_channel(_options = {})
7
+ @channel = Legion::Transport::Connection.channel
8
+ end
9
+
10
+ def channel_open?
11
+ channel.open?
12
+ end
13
+
14
+ def options_builder(first, *args)
15
+ final_options = nil
16
+ args.each do |option|
17
+ final_options = if final_options.nil?
18
+ deep_merge(first, option)
19
+ else
20
+ deep_merge(final_options, option)
21
+ end
22
+ end
23
+ final_options
24
+ end
25
+
26
+ # rubocop:disable all
27
+ def deep_merge(original, new)
28
+ {} unless original.is_a?(Hash) && new.is_a?(Hash)
29
+ original unless new.is_a? Hash
30
+ new unless original.is_a? Hash
31
+ new if original.nil? || original.empty?
32
+ original if new.nil? || new.empty?
33
+
34
+ new.each do |k, v|
35
+ unless original.key?(k)
36
+ original[k] = v
37
+ next
38
+ end
39
+
40
+ original[k.to_sym] = if [original[k.to_sym], new[k.to_sym]].all? { |a| a.is_a? Hash }
41
+ deep_merge(original[k], new[k])
42
+ else
43
+ new[k]
44
+ end
45
+ end
46
+ original
47
+ end
48
+ # rubocop:enable all
49
+
50
+ def channel
51
+ @channel ||= Legion::Transport::Connection.channel
52
+ end
53
+
54
+ def close!
55
+ Legion::Logging.error 'close! called'
56
+ false unless Legion::Transport::Connection.channel_open?
57
+ Legion::Transport::Connection.channel.close
58
+ end
59
+
60
+ def close
61
+ Legion::Logging.error 'close called'
62
+ Legion::Logging.warn 'close called, but method is called close!'
63
+ close!
64
+ end
65
+
66
+ def generate_consumer_tag(lex_name: nil, runner_name: nil, thread: Thread.current.object_id)
67
+ tag = "#{Legion::Settings[:client][:name]}_"
68
+ tag.concat("#{lex_name}_") unless lex_name.nil?
69
+ tag.concat("#{runner_name}_") unless runner_name.nil?
70
+ tag.concat("#{thread}_")
71
+ tag.concat(SecureRandom.hex)
72
+ tag
73
+ end
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,62 @@
1
+ require 'concurrent-ruby'
2
+
3
+ module Legion
4
+ module Transport
5
+ module Connection
6
+ class << self
7
+ def connector
8
+ Legion::Transport::CONNECTOR
9
+ end
10
+
11
+ def setup # rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity
12
+ if @session.respond_to?(:value) && session.respond_to?(:closed?) && session.closed?
13
+ @channel_thread = Concurrent::ThreadLocalVar.new
14
+ elsif @session.respond_to?(:value) && session.respond_to?(:closed?) && session.open?
15
+ return nil
16
+ else
17
+ @session ||= Concurrent::Atom.new(
18
+ connector.new(
19
+ Legion::Settings[:transport][:connection],
20
+ logger: Legion::Logging::Logger.new(level: 'warn'),
21
+ log_level: :info
22
+ )
23
+ )
24
+ @channel_thread = Concurrent::ThreadLocalVar.new
25
+ end
26
+
27
+ session.start
28
+ session.create_channel.basic_qos(1, true)
29
+ end
30
+
31
+ def channel
32
+ return @channel_thread.value if !@channel_thread.value.nil? && @channel_thread.value.open?
33
+
34
+ @channel_thread.value = session.create_channel
35
+ @channel_thread.value.prefetch(Legion::Settings[:transport][:prefetch])
36
+ @channel_thread.value
37
+ end
38
+
39
+ def session
40
+ nil if @session.nil?
41
+ @session.value
42
+ end
43
+
44
+ def channel_thread
45
+ channel
46
+ end
47
+
48
+ def channel_open?
49
+ channel.open?
50
+ end
51
+
52
+ def session_open?
53
+ session.open?
54
+ end
55
+
56
+ def shutdown
57
+ session.close
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,13 @@
1
+ module Legion
2
+ module Transport
3
+ class Consumer < Legion::Transport::CONNECTOR::Consumer
4
+ include Legion::Transport::Common
5
+ attr_reader :consumer_tag
6
+
7
+ def initialize(queue:, no_ack: false, exclusive: false, consumer_tag: generate_consumer_tag, **opts)
8
+ @consumer_tag = consumer_tag
9
+ super(channel, queue, consumer_tag, no_ack, exclusive, opts)
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,51 @@
1
+ module Legion
2
+ module Transport
3
+ class Exchange < Legion::Transport::CONNECTOR::Exchange
4
+ include Legion::Transport::Common
5
+
6
+ def initialize(exchange = exchange_name, options = {})
7
+ @options = options
8
+ @type = options[:type] || default_type
9
+ super(channel, @type, exchange, options_builder(default_options, exchange_options, @options))
10
+ rescue ::Bunny::PreconditionFailed, ::Bunny::ChannelAlreadyClosed
11
+ raise unless @retries.nil?
12
+
13
+ @retries = 1
14
+ delete_exchange(exchange)
15
+ retry
16
+ end
17
+
18
+ def delete_exchange(exchange)
19
+ Legion::Logging.warn "Exchange:#{exchange} exists with wrong parameters, deleting and creating"
20
+ channel.exchange_delete(exchange)
21
+ end
22
+
23
+ def default_options
24
+ hash = {}
25
+ hash[:durable] = true
26
+ hash[:auto_delete] = false
27
+ hash[:arguments] = {}
28
+ hash
29
+ end
30
+
31
+ def exchange_name
32
+ self.class.ancestors.first.to_s.split('::')[2].downcase
33
+ end
34
+
35
+ def exchange_options
36
+ {}
37
+ end
38
+
39
+ def delete(options = {})
40
+ super(options)
41
+ true
42
+ rescue ::Bunny::PreconditionFailed
43
+ false
44
+ end
45
+
46
+ def default_type
47
+ 'topic'
48
+ end
49
+ end
50
+ end
51
+ end