legion-transport 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (43) hide show
  1. checksums.yaml +7 -0
  2. data/.circleci/config.yml +116 -0
  3. data/.gitignore +13 -0
  4. data/.rubocop.yml +27 -0
  5. data/Gemfile +4 -0
  6. data/Rakefile +6 -0
  7. data/bin/console +14 -0
  8. data/bin/setup +8 -0
  9. data/bitbucket-pipelines.yml +24 -0
  10. data/legion-transport.gemspec +38 -0
  11. data/lib/legion/transport.rb +20 -0
  12. data/lib/legion/transport/common.rb +71 -0
  13. data/lib/legion/transport/connection.rb +18 -0
  14. data/lib/legion/transport/connections/bunny.rb +53 -0
  15. data/lib/legion/transport/connections/common.rb +36 -0
  16. data/lib/legion/transport/connections/marchhare.rb +20 -0
  17. data/lib/legion/transport/consumer.rb +6 -0
  18. data/lib/legion/transport/consumers/bunny.rb +11 -0
  19. data/lib/legion/transport/consumers/common.rb +8 -0
  20. data/lib/legion/transport/consumers/marchhare.rb +11 -0
  21. data/lib/legion/transport/exchange.rb +13 -0
  22. data/lib/legion/transport/exchanges/bunny.rb +28 -0
  23. data/lib/legion/transport/exchanges/common.rb +32 -0
  24. data/lib/legion/transport/exchanges/lex.rb +11 -0
  25. data/lib/legion/transport/exchanges/marchhare.rb +17 -0
  26. data/lib/legion/transport/exchanges/task.rb +11 -0
  27. data/lib/legion/transport/message.rb +8 -0
  28. data/lib/legion/transport/messages/base.rb +22 -0
  29. data/lib/legion/transport/messages/lex_register.rb +39 -0
  30. data/lib/legion/transport/messages/node_status.rb +49 -0
  31. data/lib/legion/transport/messages/task_check_subtask.rb +35 -0
  32. data/lib/legion/transport/messages/task_subtask.rb +45 -0
  33. data/lib/legion/transport/messages/task_update.rb +52 -0
  34. data/lib/legion/transport/queue.rb +13 -0
  35. data/lib/legion/transport/queues/bunny.rb +26 -0
  36. data/lib/legion/transport/queues/common.rb +45 -0
  37. data/lib/legion/transport/queues/marchhare.rb +26 -0
  38. data/lib/legion/transport/queues/node_status.rb +21 -0
  39. data/lib/legion/transport/queues/task_log.rb +21 -0
  40. data/lib/legion/transport/queues/task_update.rb +21 -0
  41. data/lib/legion/transport/version.rb +5 -0
  42. data/settings/transport.json +1 -0
  43. metadata +210 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: d8e533b2973535c4763ab30eddf935de8f180e1114dc94c04ca8b1f875ff29ac
4
+ data.tar.gz: 595f0dbb65675f616c9485f28433220475ded71a4727e8f3db8f4fc8206449c3
5
+ SHA512:
6
+ metadata.gz: 6b1eac97b2775e5d67ff1f27efecf93fa96ab7dc276a5698a1323be276ee0176040eccec771752eee49068a928cce8fa044cb9ddb0d59c603f37c511c913937e
7
+ data.tar.gz: 4efa70d6438d27bfb9a9ceda17be7bb63f3f34aca5ff0dfad33186f3d16d70d99fc4aeeeecc227769a098a0118554abca4781ec5c8b37de8f359f928b52e9b25
@@ -0,0 +1,116 @@
1
+ version: 2 # use CircleCI 2.0
2
+ jobs:
3
+ "rubocop":
4
+ docker:
5
+ - image: circleci/ruby:2.4-node
6
+ steps:
7
+ - checkout
8
+ - run: gem install rubocop
9
+ - run:
10
+ name: Run Rubocop
11
+ command: rubocop
12
+ - store_test_results:
13
+ path: test_results
14
+ "ruby-2.3":
15
+ docker:
16
+ - image: circleci/ruby:2.3
17
+ - image: rabbitmq:3.7
18
+ steps:
19
+ - checkout
20
+ - run:
21
+ name: Bundle Install
22
+ command: bundle install
23
+ - run:
24
+ name: Run RSpec
25
+ command: bundle exec rspec --format progress --format RspecJunitFormatter -o test-results/rspec/results.xml
26
+ when: always
27
+ - store_test_results:
28
+ path: test-results
29
+
30
+ "ruby-2.4":
31
+ docker:
32
+ - image: circleci/ruby:2.4-node
33
+ - image: rabbitmq:3.7
34
+ steps:
35
+ - checkout
36
+ - run:
37
+ name: Bundle Install
38
+ command: bundle install
39
+ - run:
40
+ name: Run RSpec
41
+ command: bundle exec rspec --format progress --format RspecJunitFormatter -o test-results/rspec/results.xml
42
+ when: always
43
+ - store_test_results:
44
+ path: test-results
45
+ "ruby-2.5":
46
+ docker:
47
+ - image: circleci/ruby:2.5-node
48
+ - image: rabbitmq:3.7
49
+ steps:
50
+ - checkout
51
+ - run:
52
+ name: Bundle Install
53
+ command: bundle install
54
+ - run:
55
+ name: Run RSpec
56
+ command: bundle exec rspec --format progress --format RspecJunitFormatter -o test-results/rspec/results.xml
57
+ when: always
58
+ - store_test_results:
59
+ path: test-results
60
+ "ruby-2.6":
61
+ docker:
62
+ - image: circleci/ruby:2.6-node
63
+ - image: rabbitmq:3.7
64
+ steps:
65
+ - checkout
66
+ - run:
67
+ name: Bundle Install
68
+ command: bundle install
69
+ - run:
70
+ name: Run RSpec
71
+ command: bundle exec rspec --format progress --format RspecJunitFormatter -o test-results/rspec/results.xml
72
+ when: always
73
+ - store_test_results:
74
+ path: test-results
75
+ "jruby-9.2":
76
+ docker:
77
+ - image: circleci/jruby:9.2
78
+ - image: rabbitmq:3.7
79
+ steps:
80
+ - checkout
81
+ - run:
82
+ name: Bundle Install
83
+ command: bundle install
84
+ - run:
85
+ name: Run RSpec
86
+ command: bundle exec rspec --format progress --format RspecJunitFormatter -o test-results/rspec/results.xml
87
+ when: always
88
+ - store_test_results:
89
+ path: test-results
90
+
91
+ workflows:
92
+ version: 2
93
+ cop_rake_deploy:
94
+ jobs:
95
+ - rubocop
96
+ - ruby-2.3:
97
+ requires:
98
+ - ruby-2.4
99
+ filters:
100
+ branches:
101
+ only: /\bdevelop\b|\bmaster\b/
102
+ - ruby-2.4:
103
+ requires:
104
+ - rubocop
105
+ - ruby-2.5:
106
+ requires:
107
+ - ruby-2.4
108
+ filters:
109
+ branches:
110
+ only: /\bdevelop\b|\bmaster\b/
111
+ - ruby-2.6:
112
+ requires:
113
+ - ruby-2.4
114
+ filters:
115
+ branches:
116
+ only: /\bdevelop\b|\bmaster\b/
@@ -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
@@ -0,0 +1,27 @@
1
+ Metrics/LineLength:
2
+ Max: 120
3
+ Metrics/MethodLength:
4
+ Max: 30
5
+ Metrics/ClassLength:
6
+ Max: 1500
7
+ Metrics/BlockLength:
8
+ Max: 50
9
+ Exclude:
10
+ - 'spec/legion/connections/*'
11
+ Layout/SpaceAroundEqualsInParameterDefault:
12
+ EnforcedStyle: space
13
+ Style/SymbolArray:
14
+ Enabled: true
15
+ Layout/AlignHash:
16
+ EnforcedHashRocketStyle: table
17
+ EnforcedColonStyle: table
18
+ Style/HashSyntax:
19
+ EnforcedStyle: ruby19_no_mixed_keys
20
+ Style/Documentation:
21
+ Enabled: false
22
+ AllCops:
23
+ TargetRubyVersion: 2.4
24
+ Style/FrozenStringLiteralComment:
25
+ Enabled: false
26
+ Naming/FileName:
27
+ Enabled: false
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in legion-transport.gemspec
4
+ gemspec
@@ -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.4.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,38 @@
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
+
15
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
16
+ f.match(%r{^(test|spec|features)/})
17
+ end
18
+ spec.bindir = 'exe'
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ['lib']
21
+
22
+ spec.add_development_dependency 'bundler', '~> 1'
23
+ spec.add_development_dependency 'codecov'
24
+ spec.add_development_dependency 'rake', '~> 10.0'
25
+ spec.add_development_dependency 'rspec', '~> 3.0'
26
+ spec.add_development_dependency 'rspec_junit_formatter'
27
+ spec.add_development_dependency 'rubocop'
28
+
29
+ if RUBY_ENGINE == 'jruby'
30
+ spec.add_development_dependency 'legion-settings-java', '~> 0.1'
31
+ spec.add_dependency 'march_hare'
32
+ else
33
+ spec.add_development_dependency 'legion-settings', '~> 0.1'
34
+ spec.add_dependency 'bunny'
35
+ end
36
+
37
+ spec.add_development_dependency 'legion-logging', '~> 0.1'
38
+ end
@@ -0,0 +1,20 @@
1
+ require 'legion/transport/version'
2
+
3
+ module Legion
4
+ # Base Legion::Transport
5
+ module Transport
6
+ if RUBY_ENGINE == 'jruby'
7
+ require 'marchhare'
8
+ CONNECTOR = 'marchhare'.freeze
9
+ else
10
+ require 'bunny'
11
+ CONNECTOR = 'bunny'.freeze
12
+ end
13
+ end
14
+
15
+ require 'legion/transport/common'
16
+ require 'legion/transport/queue'
17
+ require 'legion/transport/exchange'
18
+ require 'legion/transport/consumer'
19
+ require 'legion/transport/message'
20
+ end
@@ -0,0 +1,71 @@
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.new(options).channel
8
+ end
9
+
10
+ def channel_valid?(channel = @channel)
11
+ return false if channel.nil?
12
+ return false unless channel.instance_of?(::Bunny::Channel)
13
+ return false if channel.closed?
14
+
15
+ true
16
+ end
17
+
18
+ def options_builder(first, *args)
19
+ final_options = nil
20
+ args.each do |option|
21
+ final_options = if final_options.nil?
22
+ deep_merge(first, option)
23
+ else
24
+ deep_merge(final_options, option)
25
+ end
26
+ end
27
+ final_options
28
+ end
29
+
30
+ # rubocop:disable all
31
+ def deep_merge(original, new)
32
+ {} unless original.is_a?(Hash) && new.is_a?(Hash)
33
+ original unless new.is_a? Hash
34
+ new unless original.is_a? Hash
35
+ new if original.nil? || original.empty?
36
+ original if new.nil? || new.empty?
37
+
38
+ new.each do |k, v|
39
+ unless original.key?(k)
40
+ original[k] = v
41
+ next
42
+ end
43
+
44
+ original[k.to_sym] = if [original[k.to_sym], new[k.to_sym]].all? { |a| a.is_a? Hash }
45
+ deep_merge(original[k], new[k])
46
+ else
47
+ new[k]
48
+ end
49
+ end
50
+ original
51
+ end
52
+ # rubocop:enable all
53
+
54
+ def closed?
55
+ @channel.closed?
56
+ end
57
+
58
+ def channel(options = @options || {})
59
+ @channel if channel_valid?
60
+ @channel = options[:channel] if channel_valid?(options[:channel])
61
+ @channel = open_channel(options) unless channel_valid?(@channel)
62
+ @channel
63
+ end
64
+
65
+ def close(_options = @options || {})
66
+ false if @channel.closed?
67
+ @channel.close
68
+ end
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,18 @@
1
+ module Legion
2
+ module Transport
3
+ class Connection
4
+ @@session = nil # rubocop:disable Style/ClassVars
5
+ attr_accessor :session, :channel
6
+
7
+ if RUBY_ENGINE == 'jruby'
8
+ require 'march-hare'
9
+ require 'legion/transport/connections/marchhare'
10
+ include Legion::Transport::Connections::Marchhare
11
+ else
12
+ require 'bunny'
13
+ require 'legion/transport/connections/bunny'
14
+ include Legion::Transport::Connections::Bunny
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,53 @@
1
+ require 'legion/transport/connections/common'
2
+
3
+ module Legion
4
+ module Transport
5
+ module Connections
6
+ module Bunny
7
+ @@session = nil # rubocop:disable Style/ClassVars
8
+ include Legion::Transport::Connections::Common
9
+ attr_accessor :session, :channel
10
+
11
+ def initialize(options = {})
12
+ options = options_builder(options)
13
+ create_session(options)
14
+ @channel = @session.create_channel
15
+ Legion::Settings[:transport][:rabbitmq][:connected] = true
16
+ end
17
+
18
+ def create_session(options = {})
19
+ Legion::Logging.debug 'Creating Legion::Transport session'
20
+ if options[:create_new_session]
21
+ @session = ::Bunny.new(options)
22
+ @session.start
23
+ elsif @@session.nil? || @@session.closed?
24
+ @@session = ::Bunny.new(options) # rubocop:disable Style/ClassVars
25
+ @@session.start
26
+ @session = @@session
27
+ else
28
+ Legion::Logging.debug "Using session:#{@@session.channel_id_allocator} from @@session"
29
+ @session = @@session
30
+ end
31
+ rescue NameError
32
+ Legion::Logging.debug 'Legion::Transport::Connection doesn\'t have a class variable session'
33
+ @session = ::Bunny.new(options)
34
+ @session.start
35
+ end
36
+
37
+ def valid_session?(session = @session)
38
+ false if session.nil? || session.empty?
39
+ false unless session.is_a? Bunny::Session
40
+ false if session.closed?
41
+ true
42
+ end
43
+
44
+ def valid_channel?(channel = @channel)
45
+ false if channel.nil? || session.empty?
46
+ false unless channel.is_a? Bunny::Session
47
+ false if channel.closed?
48
+ true
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,36 @@
1
+ module Legion
2
+ module Transport
3
+ module Connections
4
+ module Common
5
+ def options_builder(options = {})
6
+ settings = Legion::Settings[:transport][:rabbitmq] if Legion::Settings[:transport].is_a? Hash
7
+ hash = default_options.merge(settings) unless settings.nil?
8
+ hash = {} if settings.nil?
9
+ hash = hash.merge(options) if options.is_a? Hash
10
+ hash
11
+ end
12
+
13
+ def url_builder(options = default_options)
14
+ "amqp://#{options[:user]}:#{options[:password]}@#{options[:host]}:#{options[:port]}"
15
+ end
16
+
17
+ def default_options
18
+ default = {}
19
+ default[:read_timeout] = 30
20
+ default[:heartbeat] = 1
21
+ default[:automatically_recover] = true
22
+ default[:continuation_timeout] = 4000
23
+ default[:network_recovery_interval] = 5
24
+ default[:host] = '127.0.0.1'
25
+ default[:port] = '5672'
26
+ default[:user] = 'guest'
27
+ default[:password] = 'guest'
28
+ default[:vhost] = '/'
29
+ default[:log_level] = :info
30
+
31
+ default
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end