legionio 0.3.2 → 0.3.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- metadata +4 -94
- data/.circleci/config.yml +0 -119
- data/.gitignore +0 -14
- data/.rspec +0 -2
- data/.rubocop.yml +0 -89
- data/CHANGELOG.md +0 -16
- data/Gemfile +0 -3
- data/LICENSE.txt +0 -21
- data/README.md +0 -49
- data/Rakefile +0 -38
- data/bin/console +0 -16
- data/bin/legion +0 -49
- data/bin/setup +0 -8
- data/bin/test +0 -32
- data/bitbucket-pipelines.yml +0 -55
- data/docs/Makefile +0 -20
- data/docs/_build/doctrees/environment.pickle +0 -0
- data/docs/_build/doctrees/index.doctree +0 -0
- data/docs/_build/doctrees/overview.doctree +0 -0
- data/docs/_build/html/.buildinfo +0 -4
- data/docs/_build/html/_sources/index.rst.txt +0 -28
- data/docs/_build/html/_sources/overview.rst.txt +0 -0
- data/docs/_build/html/_static/alabaster.css +0 -701
- data/docs/_build/html/_static/basic.css +0 -855
- data/docs/_build/html/_static/custom.css +0 -1
- data/docs/_build/html/_static/doctools.js +0 -315
- data/docs/_build/html/_static/documentation_options.js +0 -12
- data/docs/_build/html/_static/file.png +0 -0
- data/docs/_build/html/_static/jquery-3.5.1.js +0 -10872
- data/docs/_build/html/_static/jquery.js +0 -2
- data/docs/_build/html/_static/language_data.js +0 -297
- data/docs/_build/html/_static/minus.png +0 -0
- data/docs/_build/html/_static/plus.png +0 -0
- data/docs/_build/html/_static/pygments.css +0 -82
- data/docs/_build/html/_static/searchtools.js +0 -514
- data/docs/_build/html/_static/underscore-1.3.1.js +0 -999
- data/docs/_build/html/_static/underscore.js +0 -31
- data/docs/_build/html/genindex.html +0 -101
- data/docs/_build/html/index.html +0 -117
- data/docs/_build/html/objects.inv +0 -6
- data/docs/_build/html/overview.html +0 -98
- data/docs/_build/html/search.html +0 -110
- data/docs/_build/html/searchindex.js +0 -1
- data/docs/conf.py +0 -54
- data/docs/index.rst +0 -28
- data/docs/make.bat +0 -35
- data/exe/legion +0 -52
- data/legion.gemspec +0 -59
- data/lib/legion.rb +0 -21
- data/lib/legion/exceptions/handled_task.rb +0 -6
- data/lib/legion/exceptions/invalidjson.rb +0 -5
- data/lib/legion/exceptions/missingargument.rb +0 -6
- data/lib/legion/exceptions/wrongtype.rb +0 -10
- data/lib/legion/exceptions/wrongtypes/array.rb +0 -8
- data/lib/legion/exceptions/wrongtypes/hash.rb +0 -8
- data/lib/legion/exceptions/wrongtypes/integer.rb +0 -8
- data/lib/legion/exceptions/wrongtypes/string.rb +0 -8
- data/lib/legion/extensions.rb +0 -185
- data/lib/legion/extensions/actors/base.rb +0 -49
- data/lib/legion/extensions/actors/every.rb +0 -48
- data/lib/legion/extensions/actors/loop.rb +0 -32
- data/lib/legion/extensions/actors/nothing.rb +0 -15
- data/lib/legion/extensions/actors/once.rb +0 -40
- data/lib/legion/extensions/actors/poll.rb +0 -88
- data/lib/legion/extensions/actors/subscription.rb +0 -138
- data/lib/legion/extensions/builders/actors.rb +0 -61
- data/lib/legion/extensions/builders/base.rb +0 -36
- data/lib/legion/extensions/builders/helpers.rb +0 -24
- data/lib/legion/extensions/builders/runners.rb +0 -58
- data/lib/legion/extensions/core.rb +0 -126
- data/lib/legion/extensions/data.rb +0 -58
- data/lib/legion/extensions/data/migrator.rb +0 -28
- data/lib/legion/extensions/data/model.rb +0 -8
- data/lib/legion/extensions/helpers/base.rb +0 -82
- data/lib/legion/extensions/helpers/cache.rb +0 -23
- data/lib/legion/extensions/helpers/core.rb +0 -41
- data/lib/legion/extensions/helpers/data.rb +0 -23
- data/lib/legion/extensions/helpers/lex.rb +0 -48
- data/lib/legion/extensions/helpers/logger.rb +0 -47
- data/lib/legion/extensions/helpers/task.rb +0 -60
- data/lib/legion/extensions/helpers/transport.rb +0 -44
- data/lib/legion/extensions/transport.rb +0 -159
- data/lib/legion/process.rb +0 -124
- data/lib/legion/runner.rb +0 -55
- data/lib/legion/runner/log.rb +0 -10
- data/lib/legion/runner/status.rb +0 -69
- data/lib/legion/service.rb +0 -99
- data/lib/legion/supervision.rb +0 -15
- data/lib/legion/version.rb +0 -3
@@ -1,49 +0,0 @@
|
|
1
|
-
require 'hashdiff'
|
2
|
-
|
3
|
-
module Legion
|
4
|
-
module Extensions
|
5
|
-
module Actors
|
6
|
-
module Base
|
7
|
-
include Legion::Extensions::Helpers::Lex
|
8
|
-
|
9
|
-
def runner
|
10
|
-
Legion::Runner.run(runner_class: runner_class, function: function, check_subtask: check_subtask?, generate_task: generate_task?)
|
11
|
-
rescue StandardError => e
|
12
|
-
Legion::Logging.error e.message
|
13
|
-
Legion::Logging.error e.backtrace
|
14
|
-
end
|
15
|
-
|
16
|
-
def manual
|
17
|
-
runner_class.send(runner_function, **args)
|
18
|
-
rescue StandardError => e
|
19
|
-
Legion::Logging.error e.message
|
20
|
-
Legion::Logging.error e.backtrace
|
21
|
-
end
|
22
|
-
|
23
|
-
def function
|
24
|
-
nil
|
25
|
-
end
|
26
|
-
|
27
|
-
def use_runner?
|
28
|
-
true
|
29
|
-
end
|
30
|
-
|
31
|
-
def args
|
32
|
-
{}
|
33
|
-
end
|
34
|
-
|
35
|
-
def check_subtask?
|
36
|
-
true
|
37
|
-
end
|
38
|
-
|
39
|
-
def generate_task?
|
40
|
-
false
|
41
|
-
end
|
42
|
-
|
43
|
-
def enabled?
|
44
|
-
true
|
45
|
-
end
|
46
|
-
end
|
47
|
-
end
|
48
|
-
end
|
49
|
-
end
|
@@ -1,48 +0,0 @@
|
|
1
|
-
require_relative 'base'
|
2
|
-
|
3
|
-
module Legion
|
4
|
-
module Extensions
|
5
|
-
module Actors
|
6
|
-
class Every
|
7
|
-
include Legion::Extensions::Actors::Base
|
8
|
-
|
9
|
-
def initialize(**_opts)
|
10
|
-
@timer = Concurrent::TimerTask.new(execution_interval: time, timeout_interval: timeout, run_now: run_now?) do
|
11
|
-
use_runner? ? runner : manual
|
12
|
-
end
|
13
|
-
|
14
|
-
@timer.execute
|
15
|
-
rescue StandardError => e
|
16
|
-
Legion::Logging.error e.message
|
17
|
-
Legion::Logging.error e.backtrace
|
18
|
-
end
|
19
|
-
|
20
|
-
def time
|
21
|
-
1
|
22
|
-
end
|
23
|
-
|
24
|
-
def timeout
|
25
|
-
5
|
26
|
-
end
|
27
|
-
|
28
|
-
def run_now?
|
29
|
-
false
|
30
|
-
end
|
31
|
-
|
32
|
-
def action(**_opts)
|
33
|
-
Legion::Logging.warn 'An extension is using the default block from Legion::Extensions::Runners::Every'
|
34
|
-
end
|
35
|
-
|
36
|
-
def cancel
|
37
|
-
Legion::Logging.debug 'Cancelling Legion Timer'
|
38
|
-
return true unless @timer.respond_to?(:shutdown)
|
39
|
-
|
40
|
-
@timer.shutdown
|
41
|
-
rescue StandardError => e
|
42
|
-
Legion::Logging.error e.message
|
43
|
-
Legion::Logging.error e.backtrace
|
44
|
-
end
|
45
|
-
end
|
46
|
-
end
|
47
|
-
end
|
48
|
-
end
|
@@ -1,32 +0,0 @@
|
|
1
|
-
require_relative 'base'
|
2
|
-
|
3
|
-
module Legion
|
4
|
-
module Extensions
|
5
|
-
module Actors
|
6
|
-
class Loop
|
7
|
-
include Concurrent::Async
|
8
|
-
include Legion::Extensions::Actors::Base
|
9
|
-
|
10
|
-
def initialize
|
11
|
-
@loop = true
|
12
|
-
async.run
|
13
|
-
rescue StandardError => e
|
14
|
-
Legion::Logging.error e
|
15
|
-
Legion::Logging.error e.backtrace
|
16
|
-
end
|
17
|
-
|
18
|
-
def run
|
19
|
-
action while @loop
|
20
|
-
end
|
21
|
-
|
22
|
-
def action(**_opts)
|
23
|
-
Legion::Logging.warn 'An extension is using the default action for a loop'
|
24
|
-
end
|
25
|
-
|
26
|
-
def cancel
|
27
|
-
@loop = false
|
28
|
-
end
|
29
|
-
end
|
30
|
-
end
|
31
|
-
end
|
32
|
-
end
|
@@ -1,40 +0,0 @@
|
|
1
|
-
require_relative 'base'
|
2
|
-
|
3
|
-
module Legion
|
4
|
-
module Extensions
|
5
|
-
module Actors
|
6
|
-
class Once
|
7
|
-
include Legion::Extensions::Actors::Base
|
8
|
-
|
9
|
-
def initialize
|
10
|
-
return if disabled?
|
11
|
-
|
12
|
-
if respond_to? :functions
|
13
|
-
functions.each do
|
14
|
-
function
|
15
|
-
@task = Concurrent::ScheduledTask.execute(delay) do
|
16
|
-
use_runner ? runner : manual
|
17
|
-
end
|
18
|
-
end
|
19
|
-
else
|
20
|
-
@task = Concurrent::ScheduledTask.execute(delay) do
|
21
|
-
use_runner ? runner : manual
|
22
|
-
end
|
23
|
-
end
|
24
|
-
rescue StandardError => e
|
25
|
-
Legion::Logging.error e
|
26
|
-
end
|
27
|
-
|
28
|
-
def delay
|
29
|
-
1.0
|
30
|
-
end
|
31
|
-
|
32
|
-
def cancel
|
33
|
-
return if disabled?
|
34
|
-
|
35
|
-
@task.cancel unless @task.cancelled?
|
36
|
-
end
|
37
|
-
end
|
38
|
-
end
|
39
|
-
end
|
40
|
-
end
|
@@ -1,88 +0,0 @@
|
|
1
|
-
require_relative 'base'
|
2
|
-
require 'hashdiff'
|
3
|
-
require 'time'
|
4
|
-
|
5
|
-
module Legion
|
6
|
-
module Extensions
|
7
|
-
module Actors
|
8
|
-
class Poll
|
9
|
-
include Legion::Extensions::Actors::Base
|
10
|
-
|
11
|
-
def initialize # rubocop:disable Metrics/AbcSize
|
12
|
-
log.debug "Starting timer for #{self.class} with #{{ execution_interval: time, timeout_interval: timeout, run_now: run_now?, check_subtask: check_subtask? }}"
|
13
|
-
@timer = Concurrent::TimerTask.new(execution_interval: time, timeout_interval: timeout, run_now: run_now?) do
|
14
|
-
t1 = Time.now
|
15
|
-
log.debug "Running #{self.class}"
|
16
|
-
old_result = Legion::Cache.get(cache_name)
|
17
|
-
log.debug "Cached value for #{self.class}: #{old_result}"
|
18
|
-
results = Legion::JSON.load(Legion::JSON.dump(manual))
|
19
|
-
Legion::Cache.set(cache_name, results, time * 2)
|
20
|
-
|
21
|
-
unless old_result.nil?
|
22
|
-
results[:diff] = Hashdiff.diff(results, old_result, numeric_tolerance: 0.0, array_path: false) do |_path, obj1, obj2|
|
23
|
-
if int_percentage_normalize.positive? && obj1.is_a?(Integer) && obj2.is_a?(Integer)
|
24
|
-
obj1.between?(obj2 * (1 - int_percentage_normalize), obj2 * (1 + int_percentage_normalize))
|
25
|
-
end
|
26
|
-
end
|
27
|
-
results[:changed] = results[:diff].count.positive?
|
28
|
-
|
29
|
-
Legion::Logging.info results[:diff] if results[:changed]
|
30
|
-
Legion::Transport::Messages::CheckSubtask.new(runner_class: runner_class.to_s,
|
31
|
-
function: runner_function,
|
32
|
-
result: results,
|
33
|
-
type: 'poll_result',
|
34
|
-
polling: true).publish
|
35
|
-
end
|
36
|
-
|
37
|
-
sleep_time = 1 - (Time.now - t1)
|
38
|
-
sleep(sleep_time) if sleep_time.positive?
|
39
|
-
log.debug("#{self.class} result: #{results}")
|
40
|
-
results
|
41
|
-
rescue StandardError => e
|
42
|
-
Legion::Logging.fatal e.message
|
43
|
-
Legion::Logging.fatal e.backtrace
|
44
|
-
end
|
45
|
-
@timer.execute
|
46
|
-
rescue StandardError => e
|
47
|
-
Legion::Logging.error e.message
|
48
|
-
Legion::Logging.error e.backtrace
|
49
|
-
end
|
50
|
-
|
51
|
-
def cache_name
|
52
|
-
"#{lex_name}_#{runner_name}"
|
53
|
-
end
|
54
|
-
|
55
|
-
def int_percentage_normalize
|
56
|
-
0.00
|
57
|
-
end
|
58
|
-
|
59
|
-
def time
|
60
|
-
9
|
61
|
-
end
|
62
|
-
|
63
|
-
def run_now?
|
64
|
-
true
|
65
|
-
end
|
66
|
-
|
67
|
-
def check_subtask?
|
68
|
-
true
|
69
|
-
end
|
70
|
-
|
71
|
-
def timeout
|
72
|
-
5
|
73
|
-
end
|
74
|
-
|
75
|
-
def action(_payload = {})
|
76
|
-
Legion::Logging.warn 'An extension is using the default block from Legion::Extensions::Runners::Every'
|
77
|
-
end
|
78
|
-
|
79
|
-
def cancel
|
80
|
-
Legion::Logging.debug 'Cancelling Legion Poller'
|
81
|
-
@timer.shutdown
|
82
|
-
rescue StandardError => e
|
83
|
-
Legion::Logging.error e.message
|
84
|
-
end
|
85
|
-
end
|
86
|
-
end
|
87
|
-
end
|
88
|
-
end
|
@@ -1,138 +0,0 @@
|
|
1
|
-
require_relative 'base'
|
2
|
-
require 'date'
|
3
|
-
|
4
|
-
module Legion
|
5
|
-
module Extensions
|
6
|
-
module Actors
|
7
|
-
class Subscription
|
8
|
-
include Concurrent::Async
|
9
|
-
include Legion::Extensions::Actors::Base
|
10
|
-
include Legion::Extensions::Helpers::Transport
|
11
|
-
|
12
|
-
def initialize(**_options)
|
13
|
-
super()
|
14
|
-
@queue = queue.new
|
15
|
-
rescue StandardError => e
|
16
|
-
log.fatal e.message
|
17
|
-
log.fatal e.backtrace
|
18
|
-
end
|
19
|
-
|
20
|
-
def create_queue
|
21
|
-
queues.const_set(actor_const, Class.new(Legion::Transport::Queue))
|
22
|
-
exchange_object = default_exchange.new
|
23
|
-
queue_object = Kernel.const_get(queue_string).new
|
24
|
-
|
25
|
-
queue_object.bind(exchange_object, routing_key: actor_name)
|
26
|
-
queue_object.bind(exchange_object, routing_key: "#{lex_name}.#{actor_name}.#")
|
27
|
-
end
|
28
|
-
|
29
|
-
def queue
|
30
|
-
create_queue unless queues.const_defined?(actor_const)
|
31
|
-
Kernel.const_get queue_string
|
32
|
-
end
|
33
|
-
|
34
|
-
def queue_string
|
35
|
-
@queue_string ||= "#{queues}::#{actor_const}"
|
36
|
-
end
|
37
|
-
|
38
|
-
def cancel
|
39
|
-
return true unless @queue.channel.active
|
40
|
-
|
41
|
-
log.debug "Closing subscription to #{@queue.name}"
|
42
|
-
@consumer.cancel
|
43
|
-
@queue.channel.close
|
44
|
-
true
|
45
|
-
end
|
46
|
-
|
47
|
-
def block
|
48
|
-
false
|
49
|
-
end
|
50
|
-
|
51
|
-
def consumers
|
52
|
-
1
|
53
|
-
end
|
54
|
-
|
55
|
-
def manual_ack
|
56
|
-
true
|
57
|
-
end
|
58
|
-
|
59
|
-
def delay_start
|
60
|
-
0
|
61
|
-
end
|
62
|
-
|
63
|
-
def include_metadata_in_message?
|
64
|
-
true
|
65
|
-
end
|
66
|
-
|
67
|
-
def process_message(message, metadata, delivery_info)
|
68
|
-
payload = if metadata.content_encoding && metadata.content_encoding == 'encrypted/cs'
|
69
|
-
Legion::Crypt.decrypt(message, metadata.headers['iv'])
|
70
|
-
elsif metadata.content_encoding && metadata.content_encoding == 'encrypted/pk'
|
71
|
-
Legion::Crypt.decrypt_from_keypair(metadata.headers[:public_key], message)
|
72
|
-
else
|
73
|
-
message
|
74
|
-
end
|
75
|
-
|
76
|
-
message = if metadata.content_type == 'application/json'
|
77
|
-
Legion::JSON.load(payload)
|
78
|
-
else
|
79
|
-
{ value: payload }
|
80
|
-
end
|
81
|
-
if include_metadata_in_message?
|
82
|
-
message = message.merge(metadata.headers.transform_keys(&:to_sym)) unless metadata.headers.nil?
|
83
|
-
message[:routing_key] = if Legion::Transport::TYPE == 'march_hare'
|
84
|
-
metadata.routing_key
|
85
|
-
else
|
86
|
-
delivery_info[:routing_key]
|
87
|
-
end
|
88
|
-
end
|
89
|
-
|
90
|
-
message[:timestamp] = (message[:timestamp_in_ms] / 1000).round if message.key?(:timestamp_in_ms) && !message.key?(:timestamp)
|
91
|
-
message[:datetime] = Time.at(message[:timestamp].to_i).to_datetime.to_s if message.key?(:timestamp)
|
92
|
-
message
|
93
|
-
end
|
94
|
-
|
95
|
-
def find_function(message = {})
|
96
|
-
return runner_function if actor_class.instance_methods(false).include?(:runner_function)
|
97
|
-
return function if actor_class.instance_methods(false).include?(:function)
|
98
|
-
return action if actor_class.instance_methods(false).include?(:action)
|
99
|
-
return message[:function] if message.key? :function
|
100
|
-
|
101
|
-
function
|
102
|
-
end
|
103
|
-
|
104
|
-
def subscribe
|
105
|
-
sleep(delay_start)
|
106
|
-
consumer_tag = "#{Legion::Settings[:client][:name]}_#{lex_name}_#{runner_name}_#{Thread.current.object_id}"
|
107
|
-
on_cancellation = block { cancel }
|
108
|
-
|
109
|
-
@consumer = @queue.subscribe(manual_ack: manual_ack, block: false, consumer_tag: consumer_tag, on_cancellation: on_cancellation) do |*rmq_message|
|
110
|
-
payload = rmq_message.pop
|
111
|
-
metadata = rmq_message.last
|
112
|
-
delivery_info = rmq_message.first
|
113
|
-
|
114
|
-
message = process_message(payload, metadata, delivery_info)
|
115
|
-
if use_runner?
|
116
|
-
Legion::Runner.run(**message,
|
117
|
-
runner_class: runner_class,
|
118
|
-
function: find_function(message),
|
119
|
-
check_subtask: check_subtask?,
|
120
|
-
generate_task: generate_task?)
|
121
|
-
else
|
122
|
-
runner_class.send(find_function(message), **message)
|
123
|
-
end
|
124
|
-
@queue.acknowledge(delivery_info.delivery_tag) if manual_ack
|
125
|
-
|
126
|
-
cancel if Legion::Settings[:client][:shutting_down]
|
127
|
-
rescue StandardError => e
|
128
|
-
Legion::Logging.error e.message
|
129
|
-
Legion::Logging.error e.backtrace
|
130
|
-
Legion::Logging.error message
|
131
|
-
Legion::Logging.error function
|
132
|
-
@queue.reject(delivery_info.delivery_tag) if manual_ack
|
133
|
-
end
|
134
|
-
end
|
135
|
-
end
|
136
|
-
end
|
137
|
-
end
|
138
|
-
end
|
@@ -1,61 +0,0 @@
|
|
1
|
-
require_relative 'base'
|
2
|
-
|
3
|
-
module Legion
|
4
|
-
module Extensions
|
5
|
-
module Builder
|
6
|
-
module Actors
|
7
|
-
include Legion::Extensions::Builder::Base
|
8
|
-
|
9
|
-
attr_reader :actors
|
10
|
-
|
11
|
-
def build_actors
|
12
|
-
@actors = {}
|
13
|
-
require_files(actor_files)
|
14
|
-
build_actor_list
|
15
|
-
build_meta_actor_list
|
16
|
-
end
|
17
|
-
|
18
|
-
def build_actor_list
|
19
|
-
actor_files.each do |file|
|
20
|
-
actor_name = file.split('/').last.sub('.rb', '')
|
21
|
-
actor_class = "#{lex_class}::Actor::#{actor_name.split('_').collect(&:capitalize).join}"
|
22
|
-
@actors[actor_name.to_sym] = {
|
23
|
-
extension: lex_class.to_s.downcase,
|
24
|
-
extension_name: extension_name,
|
25
|
-
actor_name: actor_name,
|
26
|
-
actor_class: Kernel.const_get(actor_class),
|
27
|
-
type: 'literal'
|
28
|
-
}
|
29
|
-
end
|
30
|
-
end
|
31
|
-
|
32
|
-
def build_meta_actor_list
|
33
|
-
@runners.each do |runner, attr|
|
34
|
-
next if @actors[runner.to_sym].is_a? Hash
|
35
|
-
|
36
|
-
actor_class = "#{attr[:extension_class]}::Actor::#{runner.to_s.split('_').collect(&:capitalize).join}"
|
37
|
-
build_meta_actor(runner, attr) unless Kernel.const_defined? actor_class
|
38
|
-
@actors[runner.to_sym] = {
|
39
|
-
extension: attr[:extension],
|
40
|
-
extension_name: attr[:extension_name],
|
41
|
-
actor_name: attr[:runner_name],
|
42
|
-
actor_class: Kernel.const_get(actor_class),
|
43
|
-
type: 'meta'
|
44
|
-
}
|
45
|
-
end
|
46
|
-
end
|
47
|
-
|
48
|
-
def build_meta_actor(runner, attr)
|
49
|
-
define_constant_two('Actor', root: lex_class)
|
50
|
-
|
51
|
-
Kernel.const_get("#{attr[:extension_class]}::Actor")
|
52
|
-
.const_set(runner.to_s.split('_').collect(&:capitalize).join, Class.new(Legion::Extensions::Actors::Subscription))
|
53
|
-
end
|
54
|
-
|
55
|
-
def actor_files
|
56
|
-
@actor_files ||= find_files('actors')
|
57
|
-
end
|
58
|
-
end
|
59
|
-
end
|
60
|
-
end
|
61
|
-
end
|