legionio 0.2.0 → 0.3.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (55) hide show
  1. checksums.yaml +4 -4
  2. metadata +22 -104
  3. data/.circleci/config.yml +0 -98
  4. data/.gitignore +0 -14
  5. data/.rspec +0 -2
  6. data/.rubocop.yml +0 -56
  7. data/CHANGELOG.md +0 -7
  8. data/Gemfile +0 -15
  9. data/LICENSE.txt +0 -21
  10. data/README.md +0 -46
  11. data/Rakefile +0 -38
  12. data/bin/console +0 -16
  13. data/bin/legion +0 -61
  14. data/bin/setup +0 -8
  15. data/bin/test +0 -32
  16. data/bitbucket-pipelines.yml +0 -55
  17. data/exe/legion +0 -58
  18. data/legion.gemspec +0 -60
  19. data/lib/legion.rb +0 -21
  20. data/lib/legion/exceptions/handled_task.rb +0 -6
  21. data/lib/legion/exceptions/invalidjson.rb +0 -8
  22. data/lib/legion/exceptions/missingargument.rb +0 -8
  23. data/lib/legion/exceptions/wrongtype.rb +0 -10
  24. data/lib/legion/exceptions/wrongtypes/array.rb +0 -11
  25. data/lib/legion/exceptions/wrongtypes/hash.rb +0 -11
  26. data/lib/legion/exceptions/wrongtypes/integer.rb +0 -11
  27. data/lib/legion/exceptions/wrongtypes/string.rb +0 -11
  28. data/lib/legion/extensions.rb +0 -151
  29. data/lib/legion/extensions/actors/base.rb +0 -53
  30. data/lib/legion/extensions/actors/every.rb +0 -50
  31. data/lib/legion/extensions/actors/loop.rb +0 -34
  32. data/lib/legion/extensions/actors/nothing.rb +0 -15
  33. data/lib/legion/extensions/actors/once.rb +0 -42
  34. data/lib/legion/extensions/actors/poll.rb +0 -90
  35. data/lib/legion/extensions/actors/subscription.rb +0 -120
  36. data/lib/legion/extensions/builders/actors.rb +0 -62
  37. data/lib/legion/extensions/builders/base.rb +0 -38
  38. data/lib/legion/extensions/builders/helpers.rb +0 -26
  39. data/lib/legion/extensions/builders/runners.rb +0 -54
  40. data/lib/legion/extensions/core.rb +0 -84
  41. data/lib/legion/extensions/helpers/base.rb +0 -88
  42. data/lib/legion/extensions/helpers/core.rb +0 -20
  43. data/lib/legion/extensions/helpers/lex.rb +0 -22
  44. data/lib/legion/extensions/helpers/logger.rb +0 -48
  45. data/lib/legion/extensions/helpers/task.rb +0 -42
  46. data/lib/legion/extensions/helpers/transport.rb +0 -45
  47. data/lib/legion/extensions/transport.rb +0 -156
  48. data/lib/legion/process.rb +0 -138
  49. data/lib/legion/runner.rb +0 -57
  50. data/lib/legion/runner/log.rb +0 -12
  51. data/lib/legion/runner/status.rb +0 -72
  52. data/lib/legion/service.rb +0 -80
  53. data/lib/legion/supervison.rb +0 -14
  54. data/lib/legion/version.rb +0 -5
  55. data/settings/client.json +0 -9
@@ -1,57 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'legion/exceptions/handled_task'
4
- require_relative 'runner/log'
5
- require_relative 'runner/status'
6
- require 'legion/transport'
7
- require 'legion/transport/messages/check_subtask'
8
-
9
- module Legion
10
- module Runner
11
- def self.run(runner_class:, function:, task_id: nil, args: nil, check_subtask: true, generate_task: true, parent_id: nil, master_id: nil, **opts) # rubocop:disable Metrics/ParameterLists
12
- runner_class = Kernel.const_get(runner_class) if runner_class.is_a? String
13
-
14
- if task_id.nil? && generate_task
15
- task_gen = Legion::Runner::Status.generate_task_id(
16
- function: function,
17
- runner_class: runner_class,
18
- parent_id: parent_id, master_id: master_id, task_id: task_id, **opts
19
- )
20
- task_id = task_gen[:task_id] unless task_gen.nil?
21
- end
22
-
23
- args = opts if args.nil?
24
- args[:task_id] = task_id unless task_id.nil?
25
- args[:master_id] = master_id unless master_id.nil?
26
- args[:parent_id] = parent_id unless parent_id.nil?
27
-
28
- result = runner_class.send(function, args)
29
- rescue Legion::Exception::HandledTask
30
- status = 'task.exception'
31
- result = { error: {} }
32
- rescue StandardError => e
33
- runner_class.handle_exception(e,
34
- **opts,
35
- runner_class: runner_class,
36
- args: args,
37
- function: function,
38
- task_id: task_id,
39
- generate_task: generate_task,
40
- check_subtask: check_subtask)
41
- status = 'task.exception'
42
- result = { success: false, status: status, error: { message: e.message, backtrace: e.backtrace } }
43
- else
44
- status = 'task.completed'
45
- ensure
46
- Legion::Runner::Status.update(task_id: task_id, status: status) unless task_id.nil?
47
- if check_subtask && status == 'task.completed'
48
- Legion::Transport::Messages::CheckSubtask.new(runner_class: runner_class,
49
- function: function,
50
- result: result,
51
- original_args: args,
52
- **opts).publish
53
- end
54
- return { success: true, status: status, result: result, task_id: task_id } # rubocop:disable Lint/EnsureReturn
55
- end
56
- end
57
- end
@@ -1,12 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Legion
4
- module Runner
5
- module Log
6
- def self.exception(exc, **opts)
7
- Legion::Logging.error exc.message
8
- Legion::Logging.error opts
9
- end
10
- end
11
- end
12
- end
@@ -1,72 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Legion
4
- module Runner
5
- module Status
6
- def self.update(task_id:, status: 'task.completed', **opts)
7
- Legion::Logging.debug "Legion::Runner::Status.update called, #{task_id}, status: #{status}, #{opts}"
8
- return if status.nil?
9
-
10
- if Legion::Settings[:data][:connected]
11
- update_db(task_id: task_id, status: status, **opts)
12
- else
13
- update_rmq(task_id: task_id, status: status, **opts)
14
- end
15
- end
16
-
17
- def self.update_rmq(task_id:, status: 'task.completed', **opts)
18
- return if status.nil?
19
-
20
- Legion::Transport::Messages::TaskUpdate.new(task_id: task_id, status: status, **opts).publish
21
- rescue StandardError => e
22
- Legion::Logging.fatal e.message
23
- Legion::Logging.fatal e.backtrace
24
- retries ||= 0
25
- Legion::Logging.fatal 'Will retry in 3 seconds' if retries < 5
26
- sleep(3)
27
- retry if (retries += 1) < 5
28
- end
29
-
30
- def self.update_db(task_id:, status: 'task.completed', **opts)
31
- return if status.nil?
32
-
33
- task = Legion::Data::Model::Task[task_id]
34
- task.update(status: status)
35
- rescue StandardError => e
36
- Legion::Logging.warn e.message
37
- Legion::Logging.warn 'Legion::Runner.update_status_db failed, defaulting to rabbitmq'
38
- Legion::Logging.warn e.backtrace
39
- update_rmq(task_id: task_id, status: status, **opts)
40
- end
41
-
42
- def self.generate_task_id(runner_class:, function:, status: 'task.queued', **opts)
43
- Legion::Logging.debug "Legion::Runner::Status.generate_task_id called, #{runner_class}, #{function}, status: #{status}, #{opts}"
44
- return nil unless Legion::Settings[:data][:connected]
45
-
46
- runner = Legion::Data::Model::Runner.where(namespace: runner_class.to_s.downcase).first
47
- return nil if runner.nil?
48
-
49
- function = Legion::Data::Model::Function.where(runner_id: runner.values[:id], name: function).first
50
- return nil if function.nil?
51
-
52
- insert = { status: status, function_id: function.values[:id] }
53
- insert[:parent_id] = opts[:task_id] if opts.key? :task_id
54
- insert[:master_id] = opts[:task_id] if opts.key? :task_id
55
- insert[:payload] = Legion::JSON.dump(opts[:payload]) if opts.key? :payload
56
-
57
- %i[function_args master_id parent_id relationship_id].each do |column|
58
- next unless opts.key? column
59
-
60
- insert[column] = opts[column].is_a?(Hash) ? Legion::JSON.dump(opts[column]) : opts[column]
61
- # insert[column] = opts[column] if opts.key? column
62
- end
63
-
64
- { success: true, task_id: Legion::Data::Model::Task.insert(insert), **insert }
65
- rescue StandardError => e
66
- Legion::Logging.error e.message
67
- Legion::Logging.error e.backtrace
68
- raise(e)
69
- end
70
- end
71
- end
72
- end
@@ -1,80 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Legion
4
- class Service
5
- def modules
6
- [Legion::Crypt, Legion::Transport, Legion::Cache, Legion::Data, Legion::Supervision].freeze
7
- end
8
-
9
- def initialize(transport: true, cache: true, data: true, supervision: true, extensions: true, crypt: true, log_level: 'info') # rubocop:disable Metrics/ParameterLists
10
- setup_logging(log_level)
11
- Legion::Logging.debug('Starting Legion::Service')
12
- setup_settings
13
- Legion::Logging.info "node name: #{Legion::Settings[:client][:name]}"
14
-
15
- require 'legion/crypt' if crypt
16
- Legion::Crypt.start if crypt
17
- setup_transport if transport
18
- setup_cache if cache
19
- setup_data if data
20
- setup_supervision if supervision
21
- require 'legion/runner'
22
-
23
- load_extensions if extensions
24
- Legion::Crypt.setup_safe if crypt
25
-
26
- Legion::Settings[:client][:ready] = true
27
- end
28
-
29
- def setup_cache
30
- require 'legion/cache'
31
- Legion::Cache.setup
32
- end
33
-
34
- def setup_data
35
- require 'legion/data'
36
- Legion::Settings.merge_settings(:data, Legion::Data::Settings.default)
37
- Legion::Data.setup
38
- end
39
-
40
- def setup_settings
41
- require 'legion/settings'
42
- Legion::Logging.debug('Loading Legion::Settings')
43
- Legion::Settings.load(config_dir: './settings')
44
- Legion::Logging.info('Legion::Settings Loaded')
45
- end
46
-
47
- def setup_logging(_log_level = 'info')
48
- require 'legion/logging'
49
- Legion::Logging.setup(level: 'info', trace: true)
50
- end
51
-
52
- def setup_transport
53
- require 'legion/transport'
54
- Legion::Settings.merge_settings(:transport, Legion::Transport::Settings.default)
55
- Legion::Transport::Connection.setup
56
- end
57
-
58
- def setup_supervision
59
- require_relative('supervison')
60
- @supervision = Legion::Supervision.setup
61
- end
62
-
63
- def shutdown
64
- Legion::Logging.info('Legion::Service.shutdown was called')
65
- @shutdown = true
66
- Legion::Extensions.shutdown
67
-
68
- sleep(0.2)
69
- Legion::Data.shutdown
70
- Legion::Cache.shutdown
71
- Legion::Transport::Connection.shutdown
72
- Legion::Crypt.shutdown
73
- end
74
-
75
- def load_extensions
76
- require 'legion/runner'
77
- Legion::Extensions.hook_extensions
78
- end
79
- end
80
- end
@@ -1,14 +0,0 @@
1
- module Legion
2
- module Supervision
3
- class << self
4
- attr_accessor :timer_tasks
5
- def setup
6
- @timer_tasks = Concurrent::AtomicReference.new([])
7
- @once_tasks = Concurrent::AtomicReference.new([])
8
- @loop_tasks = Concurrent::AtomicReference.new([])
9
- @poll_tasks = Concurrent::AtomicReference.new([])
10
- @subscriptions = Concurrent::AtomicReference.new([])
11
- end
12
- end
13
- end
14
- end
@@ -1,5 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Legion
4
- VERSION = '0.2.0'
5
- end
@@ -1,9 +0,0 @@
1
- {
2
- "client": {
3
- "keepalive": {
4
- "thresholds": {
5
- "warning": 10
6
- }
7
- }
8
- }
9
- }