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,20 @@
1
+ require 'legion/transport/connections/common'
2
+
3
+ module Legion
4
+ module Transport
5
+ module Connections
6
+ module Marchhare
7
+ include Legion::Transport::Connections::Common
8
+ attr_accessor :session, :channel
9
+
10
+ def initialize(options = {})
11
+ options = options_builder(options)
12
+ @session = MarchHare.connect(options)
13
+ @session.start
14
+ @channel = @session.create_channel
15
+ Legion::Settings[:transport][:rabbitmq][:connected] = true
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,6 @@
1
+ module Legion
2
+ module Transport
3
+ class Connection
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,11 @@
1
+ module Legion
2
+ module Transport
3
+ module Consumer
4
+ class Bunny < ::Bunny::Consumer
5
+ def initialize(queue, no_ack = false, exclusive = false, arguments = {})
6
+ super(Legion::Transport::Connections.new.channel, queue, '', no_ack, exclusive, arguments)
7
+ end
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,8 @@
1
+ module Legion
2
+ module Transport
3
+ module Consumer
4
+ module Common
5
+ end
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,11 @@
1
+ module Legion
2
+ module Transport
3
+ module Consumer
4
+ class Marchhare < ::MarchHare::Consumer
5
+ def initialize(queue, no_ack = false, exclusive = false, arguments = {})
6
+ super(Legion::Transport::Connection::Marchhare.new.channel, queue, '', no_ack, exclusive, arguments)
7
+ end
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,13 @@
1
+ module Legion
2
+ module Transport
3
+ if RUBY_ENGINE == 'jruby'
4
+ require 'legion/transport/exchanges/marchhare'
5
+ class Exchange < Legion::Transport::Exchanges::Marchhare
6
+ end
7
+ else
8
+ require 'legion/transport/exchanges/bunny'
9
+ class Exchange < Legion::Transport::Exchanges::Bunny
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,28 @@
1
+ require 'legion/transport/exchanges/common.rb'
2
+
3
+ module Legion
4
+ module Transport
5
+ module Exchanges
6
+ class Bunny < ::Bunny::Exchange
7
+ include Legion::Transport::Exchanges::Common
8
+ def initialize(exchange = exchange_name, options = {})
9
+ retries ||= 0
10
+ @options = options
11
+ @type = options[:type] || default_type
12
+
13
+ super(channel, @type, exchange, options_builder(default_options, exchange_options, @options))
14
+ rescue ::Bunny::PreconditionFailed
15
+ retries.zero? ? retries = 1 : raise
16
+ recreate_exchange(channel, @type, exchange)
17
+ retry
18
+ end
19
+
20
+ def recreate_exchange(channel, type, exchange)
21
+ Legion::Logging.warn "Exchange:#{exchange} exists with wrong parameters, deleting and creating"
22
+ exchange = ::Bunny::Exchange.new(channel, type, exchange, no_declare: true)
23
+ exchange.delete
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,32 @@
1
+ module Legion
2
+ module Transport
3
+ module Exchanges
4
+ module Common
5
+ include Legion::Transport::Common
6
+
7
+ def default_options
8
+ hash = {}
9
+ hash[:durable] = true
10
+ hash[:auto_delete] = false
11
+ hash[:arguments] = {}
12
+ hash
13
+ end
14
+
15
+ def exchange_options
16
+ {}
17
+ end
18
+
19
+ def delete(options = {})
20
+ super(options)
21
+ true
22
+ rescue ::Bunny::PreconditionFailed
23
+ false
24
+ end
25
+
26
+ def default_type
27
+ 'topic'
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,11 @@
1
+ module Legion
2
+ module Transport
3
+ module Exchanges
4
+ class Lex < Legion::Transport::Exchange
5
+ def exchange_name
6
+ 'lex'
7
+ end
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,17 @@
1
+ module Legion
2
+ module Transport
3
+ module Exchanges
4
+ class Marchhare < ::MarchHare::Exchange
5
+ attr_accessor :channel
6
+ def self.create(exchange, _type = :direct, _opts = {})
7
+ @channel = Legion::Transport::Connections::Marchhare.new
8
+ new(@channel.channel, exchange, type: :direct, durable: true)
9
+ end
10
+
11
+ def self.close
12
+ @channel.close
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,11 @@
1
+ module Legion
2
+ module Transport
3
+ module Exchanges
4
+ class Task < Legion::Transport::Exchange
5
+ def exchange_name
6
+ 'task'
7
+ end
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,8 @@
1
+ module Legion
2
+ module Transport
3
+ class Message
4
+ require 'legion/transport/messages/base'
5
+ include Legion::Transport::Messages::Base
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,22 @@
1
+ module Legion
2
+ module Transport
3
+ module Messages
4
+ module Base
5
+ include Legion::Transport::Common
6
+ def publish(_options = {}) # rubocop:disable Metrics/AbcSize
7
+ validate if @valid.nil?
8
+ raise unless @valid
9
+
10
+ thing = exchange.new
11
+ thing.publish(message) if routing_key.nil?
12
+ thing.publish(message, routing_key: routing_key) unless routing_key.nil?
13
+ thing.close
14
+ end
15
+
16
+ def validate
17
+ @valid = true
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,39 @@
1
+ require 'legion/transport/exchanges/lex'
2
+
3
+ module Legion
4
+ module Transport
5
+ module Messages
6
+ class LexRegister < Legion::Transport::Message
7
+ def initialize(namespace, method, options = {})
8
+ @namespace = namespace
9
+ @method = method
10
+ @options = options
11
+ end
12
+
13
+ def exchange
14
+ Legion::Transport::Exchanges::Lex
15
+ end
16
+
17
+ def routing_key
18
+ 'lex.methods.register'
19
+ end
20
+
21
+ def message(namespace = @namespace, method = @method, options = @options)
22
+ obj = { args: { namespace: namespace, method: method } }
23
+ obj[:args][:namespace_queue] = options[:namespace][:queue] unless options[:namespace][:queue].nil?
24
+ obj[:args][:namespace_uri] = options[:namespace][:uri] unless options[:namespace][:uri].nil?
25
+ Legion::JSON.dump(obj)
26
+ end
27
+
28
+ def validate(namespace = @namespace, _method = @method, _options = @options)
29
+ raise unless namespace.is_a? String
30
+
31
+ # raise unless method.is_a? String
32
+ # raise unless options.is_a? Hash
33
+
34
+ @valid = true
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,49 @@
1
+ require 'legion/transport/exchanges/task_update'
2
+
3
+ module Legion
4
+ module Exception
5
+ class InvalidTaskStatus; end
6
+ class InvalidTaskId; end
7
+ end
8
+ end
9
+
10
+ module Legion
11
+ module Transport
12
+ module Messages
13
+ # Used for sending a task update, to be moved
14
+ class TaskUpdate < Legion::Transport::Message
15
+ attr_accessor :status
16
+ attr_reader :task_id, :valid
17
+ def initialize(task_id, status = 'unknown', options = {})
18
+ @status = status
19
+ @task_id = task_id
20
+ @routing_key = options[:routing_key] unless options[:routing_key].nil?
21
+ @valid = validate
22
+ end
23
+
24
+ def exchange
25
+ Legion::Transport::Exchanges::Task
26
+ end
27
+
28
+ def message(status = @status, task_id = @task_id)
29
+ Legion::JSON.dump(status: status, task_id: task_id)
30
+ end
31
+
32
+ def validate(status = @status, task_id = @task_id)
33
+ raise Legion::Exception::InvalidTaskId unless task_id.is_a? Integer
34
+ raise Legion::Exception::InvalidTaskStatus unless valid_status.include? status
35
+
36
+ @valid = true
37
+ end
38
+
39
+ def valid_status
40
+ conditioner = ['conditioner.queued', 'conditioner.failed', 'conditioner.exception']
41
+ transfomer = ['transformer.queued', 'transformer.succeeded', 'transformer.exception']
42
+ task = ['task.scheduled', 'task.queued', 'task.completed']
43
+ status = conditioner + transfomer + task
44
+ status
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,35 @@
1
+ require 'legion/transport/exchanges/task'
2
+
3
+ module Legion
4
+ module Transport
5
+ module Messages
6
+ class TaskCheckSubtask < Legion::Transport::Message
7
+ attr_accessor :status
8
+ attr_reader :task_id, :valid
9
+ def initialize(namespace, method, result, _options = {})
10
+ @namespace = namespace
11
+ @method = method
12
+ @result = result
13
+ @routing_key = 'task.subtask.check'
14
+ validate
15
+ end
16
+
17
+ def exchange
18
+ Legion::Transport::Exchanges::Task
19
+ end
20
+
21
+ def message(namespace = @namespace, method = @method, result = @result)
22
+ Legion::JSON.dump(args: { namespace: namespace, method: method, result: result })
23
+ end
24
+
25
+ def routing_key
26
+ 'task.subtask.check'
27
+ end
28
+
29
+ def validate(_namespace = @namespace, _method = @method, _result = @result, _options = {})
30
+ @valid = true
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,45 @@
1
+ require 'legion/transport/exchanges/task'
2
+
3
+ module Legion
4
+ module Transport
5
+ module Messages
6
+ class TaskSubTask < Legion::Transport::Message
7
+ attr_accessor :status
8
+ attr_reader :task_id, :valid
9
+ def initialize(relationship_id, trigger_id, conditions, transformations, task_id = nil, options = {}) # rubocop:disable Metrics/ParameterLists,Metrics/LineLength
10
+ @relationship_id = relationship_id
11
+ @trigger_id = trigger_id
12
+ @conditions = conditions
13
+ @tranformations = transformations
14
+ @task_id = task_id
15
+ @options = options
16
+ @routing_key = 'task.subtask'
17
+ validate
18
+ end
19
+
20
+ def exchange
21
+ Legion::Transport::Exchanges::Task
22
+ end
23
+
24
+ def message(relationship_id = @relationship_id, trigger_id = @trigger_id, conditions = @conditions, transformations = @tranformations, task_id = @task_id, options = @options) # rubocop:disable Metrics/ParameterLists,Metrics/LineLength
25
+ Legion::JSON.dump(args: {
26
+ relationship_id: relationship_id,
27
+ trigger_id: trigger_id,
28
+ conditions: conditions,
29
+ task_id: task_id,
30
+ transformations: transformations,
31
+ options: options
32
+ })
33
+ end
34
+
35
+ def routing_key
36
+ 'task.subtask'
37
+ end
38
+
39
+ def validate(_namespace = @namespace, _method = @method, _result = @result, _options = {})
40
+ @valid = true
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,52 @@
1
+ require 'legion/transport/exchanges/task'
2
+
3
+ module Legion
4
+ module Exception
5
+ class InvalidTaskStatus; end
6
+ class InvalidTaskId; end
7
+ end
8
+ end
9
+
10
+ module Legion
11
+ module Transport
12
+ module Messages
13
+ class TaskUpdate < Legion::Transport::Message
14
+ attr_accessor :status
15
+ attr_reader :task_id, :valid
16
+ def initialize(task_id, status = 'unknown', _options = {})
17
+ @status = status
18
+ @task_id = task_id
19
+ @routing_key = 'task.update'
20
+ validate
21
+ end
22
+
23
+ def routing_key
24
+ 'task.update'
25
+ end
26
+
27
+ def exchange
28
+ Legion::Transport::Exchanges::Task
29
+ end
30
+
31
+ def message(status = @status, task_id = @task_id, options = {})
32
+ Legion::JSON.dump(args: { status: status, task_id: task_id, options: options })
33
+ end
34
+
35
+ def validate(status = @status, task_id = @task_id)
36
+ raise Legion::Exception::InvalidTaskId unless task_id.is_a? Integer
37
+ raise Legion::Exception::InvalidTaskStatus unless valid_status.include? status
38
+
39
+ @valid = true
40
+ end
41
+
42
+ def valid_status
43
+ conditioner = ['conditioner.queued', 'conditioner.failed', 'conditioner.exception']
44
+ transfomer = ['transformer.queued', 'transformer.succeeded', 'transformer.exception']
45
+ task = ['task.scheduled', 'task.queued', 'task.completed', 'task.exception']
46
+ status = conditioner + transfomer + task
47
+ status
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end