legionio 0.3.4 → 0.4.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (89) hide show
  1. checksums.yaml +4 -4
  2. data/.circleci/config.yml +136 -0
  3. data/.gitignore +15 -0
  4. data/.rspec +2 -0
  5. data/.rubocop.yml +94 -0
  6. data/CHANGELOG.md +47 -0
  7. data/Dockerfile +9 -0
  8. data/Gemfile +3 -0
  9. data/LICENSE.txt +21 -0
  10. data/README.md +161 -0
  11. data/Rakefile +32 -0
  12. data/bitbucket-pipelines.yml +19 -0
  13. data/docker_deploy.rb +10 -0
  14. data/exe/legion +4 -0
  15. data/exe/legionio +53 -0
  16. data/exe/lex_gen +5 -0
  17. data/legionio.gemspec +64 -0
  18. data/lib/legion.rb +25 -0
  19. data/lib/legion/cli.rb +56 -0
  20. data/lib/legion/cli/chain.rb +35 -0
  21. data/lib/legion/cli/cohort.rb +10 -0
  22. data/lib/legion/cli/function.rb +41 -0
  23. data/lib/legion/cli/lex/actor.rb +31 -0
  24. data/lib/legion/cli/lex/exchange.rb +32 -0
  25. data/lib/legion/cli/lex/message.rb +32 -0
  26. data/lib/legion/cli/lex/queue.rb +45 -0
  27. data/lib/legion/cli/lex/runner.rb +70 -0
  28. data/lib/legion/cli/lex/templates/actor.erb +6 -0
  29. data/lib/legion/cli/lex/templates/actor_spec.erb +0 -0
  30. data/lib/legion/cli/lex/templates/base/bitbucket.yml.erb +69 -0
  31. data/lib/legion/cli/lex/templates/base/gemfile.erb +3 -0
  32. data/lib/legion/cli/lex/templates/base/gemspec.erb +26 -0
  33. data/lib/legion/cli/lex/templates/base/gitignore.erb +11 -0
  34. data/lib/legion/cli/lex/templates/base/lex.erb +9 -0
  35. data/lib/legion/cli/lex/templates/base/lex_spec.erb +5 -0
  36. data/lib/legion/cli/lex/templates/base/lic.erb +21 -0
  37. data/lib/legion/cli/lex/templates/base/rakefile.erb +6 -0
  38. data/lib/legion/cli/lex/templates/base/readme.md.erb +2 -0
  39. data/lib/legion/cli/lex/templates/base/rubocop.yml.erb +15 -0
  40. data/lib/legion/cli/lex/templates/base/spec_helper.rb.erb +11 -0
  41. data/lib/legion/cli/lex/templates/base/version.erb +7 -0
  42. data/lib/legion/cli/lex/templates/exchange.erb +11 -0
  43. data/lib/legion/cli/lex/templates/exchange_spec.erb +0 -0
  44. data/lib/legion/cli/lex/templates/message.erb +23 -0
  45. data/lib/legion/cli/lex/templates/message_spec.erb +0 -0
  46. data/lib/legion/cli/lex/templates/queue.erb +12 -0
  47. data/lib/legion/cli/lex/templates/queue_helper.erb +24 -0
  48. data/lib/legion/cli/lex/templates/queue_spec.erb +11 -0
  49. data/lib/legion/cli/lex/templates/runner.erb +11 -0
  50. data/lib/legion/cli/lex/templates/runner_spec.erb +11 -0
  51. data/lib/legion/cli/relationship.rb +22 -0
  52. data/lib/legion/cli/task.rb +49 -0
  53. data/lib/legion/cli/trigger.rb +88 -0
  54. data/lib/legion/cli/version.rb +5 -0
  55. data/lib/legion/extensions.rb +219 -0
  56. data/lib/legion/extensions/actors/base.rb +47 -0
  57. data/lib/legion/extensions/actors/defaults.rb +28 -0
  58. data/lib/legion/extensions/actors/every.rb +48 -0
  59. data/lib/legion/extensions/actors/loop.rb +32 -0
  60. data/lib/legion/extensions/actors/nothing.rb +15 -0
  61. data/lib/legion/extensions/actors/once.rb +40 -0
  62. data/lib/legion/extensions/actors/poll.rb +87 -0
  63. data/lib/legion/extensions/actors/subscription.rb +139 -0
  64. data/lib/legion/extensions/builders/actors.rb +61 -0
  65. data/lib/legion/extensions/builders/base.rb +36 -0
  66. data/lib/legion/extensions/builders/helpers.rb +24 -0
  67. data/lib/legion/extensions/builders/runners.rb +58 -0
  68. data/lib/legion/extensions/core.rb +131 -0
  69. data/lib/legion/extensions/data.rb +58 -0
  70. data/lib/legion/extensions/data/migrator.rb +28 -0
  71. data/lib/legion/extensions/data/model.rb +8 -0
  72. data/lib/legion/extensions/helpers/base.rb +82 -0
  73. data/lib/legion/extensions/helpers/cache.rb +23 -0
  74. data/lib/legion/extensions/helpers/core.rb +41 -0
  75. data/lib/legion/extensions/helpers/data.rb +23 -0
  76. data/lib/legion/extensions/helpers/lex.rb +48 -0
  77. data/lib/legion/extensions/helpers/logger.rb +44 -0
  78. data/lib/legion/extensions/helpers/task.rb +60 -0
  79. data/lib/legion/extensions/helpers/transport.rb +44 -0
  80. data/lib/legion/extensions/transport.rb +159 -0
  81. data/lib/legion/lex.rb +89 -0
  82. data/lib/legion/process.rb +124 -0
  83. data/lib/legion/runner.rb +55 -0
  84. data/lib/legion/runner/log.rb +10 -0
  85. data/lib/legion/runner/status.rb +69 -0
  86. data/lib/legion/service.rb +130 -0
  87. data/lib/legion/supervision.rb +15 -0
  88. data/lib/legion/version.rb +3 -0
  89. metadata +244 -39
data/Rakefile ADDED
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
9
+
10
+ task test: :rubocop
11
+ task test: :spec
12
+ task test: :start
13
+
14
+ task :rubocop do
15
+ sh 'rubocop'
16
+ end
17
+
18
+ task :start do
19
+ sh 'bin/legion start -d'
20
+ sh 'bin/legion stop'
21
+ end
22
+
23
+ namespace :deploy do
24
+ require 'legion/version'
25
+ desc 'Deploy alpha/test'
26
+ task :test do
27
+ Gem.yank_gem('legion', "#{Legion::VERSION}a")
28
+ puts Gem.query('legion-transport').inspect
29
+ puts Gem.methods(false)
30
+ puts Legion::VERSION
31
+ end
32
+ end
@@ -0,0 +1,19 @@
1
+ image: ruby:2.7
2
+
3
+ pipelines:
4
+ tags:
5
+ "v*":
6
+ - step:
7
+ name: Push to RubyGems
8
+ deployment: RubyGems
9
+ script:
10
+ - gem install gem-release
11
+ - (umask 077 ; echo $gem_creds | base64 --decode > ~/.gem/credentials)
12
+ - gem release
13
+ artifacts:
14
+ - pkg/**
15
+ - step:
16
+ name: Push to Docker
17
+ deployment: Docker
18
+ script:
19
+ - './docker_deploy.rb'
data/docker_deploy.rb ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require './lib/legion/version'
4
+ puts "Building docker image for Legion v#{Legion::VERSION}"
5
+ system("docker build --tag legionio/legion:v#{Legion::VERSION} .")
6
+ puts 'Pushing to hub.docker.com'
7
+ system("docker push legionio/legion:v#{Legion::VERSION}")
8
+ system("docker tag legionio/legion:v#{Legion::VERSION} legionio/legion:lastest")
9
+ system("docker push legionio/legion:v#{Legion::VERSION}")
10
+ puts 'completed'
data/exe/legion ADDED
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+ require 'thor'
3
+ require 'legion/cli'
4
+ Legion::CLI.start
data/exe/legionio ADDED
@@ -0,0 +1,53 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ $LOAD_PATH << File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
5
+
6
+ require 'optparse'
7
+ options = { action: :run }
8
+
9
+ daemonize_help = 'run daemonized in the background (default: false)'
10
+ pidfile_help = 'the pid filename'
11
+ logfile_help = 'the log filename'
12
+ include_help = 'an additional $LOAD_PATH (may be used more than once)'
13
+ debug_help = 'set $DEBUG to true'
14
+ warn_help = 'enable warnings'
15
+ time_help = 'only run legion for X seconds'
16
+
17
+ op = OptionParser.new
18
+ op.banner = 'An example of how to daemonize a long running Ruby process.'
19
+ op.separator ''
20
+ op.separator 'Usage: server [options]'
21
+ op.separator ''
22
+
23
+ op.separator ''
24
+ op.separator 'Process options:'
25
+ op.on('-d', '--daemonize', daemonize_help) { options[:daemonize] = true }
26
+ op.on('-p', '--pid PIDFILE', pidfile_help) { |value| options[:pidfile] = value }
27
+ op.on('-l', '--log LOGFILE', logfile_help) { |value| options[:logfile] = value }
28
+ op.on('-t', '--time 10', time_help) { |value| options[:time_limit] = value }
29
+
30
+ op.separator ''
31
+ op.separator 'Ruby options:'
32
+ op.on('-I', '--include PATH', include_help) do |value|
33
+ $LOAD_PATH.unshift(*value.split(':').map do |v|
34
+ File.expand_path(v)
35
+ end)
36
+ end
37
+ op.on('--debug', debug_help) { $DEBUG = true }
38
+ op.on('--warn', warn_help) { $-w = true }
39
+
40
+ op.separator ''
41
+ op.separator 'Common options:'
42
+ op.on('-h', '--help') { options[:action] = :help }
43
+ op.on('-v', '--version') { options[:action] = :version }
44
+
45
+ op.separator ''
46
+ op.parse!(ARGV)
47
+
48
+ unless options[:action] == :help
49
+ require 'legion'
50
+ Legion.start
51
+ require 'legion/process'
52
+ Legion::Process.new(options).run!
53
+ end
data/exe/lex_gen ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+ require 'thor'
3
+ require 'legion/lex'
4
+
5
+ Legion::Cli::LexBuilder.start
data/legionio.gemspec ADDED
@@ -0,0 +1,64 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('lib', __dir__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'legion/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'legionio'
9
+ spec.version = Legion::VERSION
10
+ spec.authors = ['Esity']
11
+ spec.email = ['matthewdiverson@gmail.com']
12
+
13
+ spec.summary = 'Legion Core Software to string it all together'
14
+ spec.description = 'Legion Core runs Legion Framwork'
15
+ spec.homepage = 'https://bitbucket.org/legion-io/legion/'
16
+ spec.metadata['source_code_uri'] = 'https://bitbucket.org/legion-io/legion/src/master/'
17
+ spec.metadata['documentation_uri'] = 'https://legionio.atlassian.net/wiki/spaces/LEGION/overview'
18
+ spec.metadata['bug_tracker_uri'] = 'https://legionio.atlassian.net/browse/LEG'
19
+ spec.license = 'MIT'
20
+ spec.required_ruby_version = '>= 2.5.0'
21
+
22
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
23
+ f.match(%r{^(test|spec|features)/})
24
+ end
25
+
26
+ spec.bindir = 'exe'
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ['lib']
29
+
30
+ spec.add_dependency 'concurrent-ruby', '>= 1.1.7'
31
+ spec.add_dependency 'concurrent-ruby-ext', '>= 1.1.7'
32
+ spec.add_dependency 'daemons', '>= 1.3.1'
33
+ spec.add_dependency 'oj', '>= 3.10'
34
+ spec.add_dependency 'thor', '>= 1'
35
+
36
+ spec.add_dependency 'legion-cache'
37
+ spec.add_dependency 'legion-crypt', '>= 0.2.0'
38
+ spec.add_dependency 'legion-exceptions'
39
+ spec.add_dependency 'legion-json'
40
+ spec.add_dependency 'legion-logging'
41
+ spec.add_dependency 'legion-settings'
42
+ spec.add_dependency 'legion-transport', '>= 1.1.9'
43
+
44
+ spec.add_dependency 'lex-node'
45
+
46
+ spec.add_development_dependency 'codecov'
47
+ spec.add_development_dependency 'legion-data'
48
+ spec.add_development_dependency 'rake'
49
+ spec.add_development_dependency 'rspec'
50
+ spec.add_development_dependency 'rspec_junit_formatter'
51
+ spec.add_development_dependency 'rubocop'
52
+ spec.add_development_dependency 'rubocop-rake'
53
+ spec.add_development_dependency 'rubocop-rspec'
54
+ spec.add_development_dependency 'simplecov'
55
+
56
+ spec.add_development_dependency 'lex-conditioner'
57
+ spec.add_development_dependency 'lex-health'
58
+ spec.add_development_dependency 'lex-http'
59
+ spec.add_development_dependency 'lex-lex'
60
+ spec.add_development_dependency 'lex-log'
61
+ spec.add_development_dependency 'lex-scheduler'
62
+ spec.add_development_dependency 'lex-tasker'
63
+ spec.add_development_dependency 'lex-transformer'
64
+ end
data/lib/legion.rb ADDED
@@ -0,0 +1,25 @@
1
+ Process.setproctitle('Legion')
2
+ require 'concurrent'
3
+ require 'securerandom'
4
+ require 'legion/exceptions'
5
+ require 'legion/version'
6
+ require 'legion/process'
7
+ require 'legion/service'
8
+ require 'legion/extensions'
9
+
10
+ module Legion
11
+ attr_reader :service
12
+
13
+ def self.start
14
+ @service = Legion::Service.new
15
+ Legion::Logging.info("Started Legion v#{Legion::VERSION}")
16
+ end
17
+
18
+ def self.shutdown
19
+ @service.shutdown
20
+ end
21
+
22
+ def self.reload
23
+ @service.reload
24
+ end
25
+ end
data/lib/legion/cli.rb ADDED
@@ -0,0 +1,56 @@
1
+ # require 'legion/cli/version'
2
+ require 'thor'
3
+ require 'legion'
4
+ require 'legion/service'
5
+
6
+ require 'legion/lex'
7
+ require 'legion/cli/cohort'
8
+
9
+ require 'legion/cli/relationship'
10
+ require 'legion/cli/task'
11
+ require 'legion/cli/chain'
12
+ require 'legion/cli/trigger'
13
+ require 'legion/cli/function'
14
+
15
+ module Legion
16
+ class CLI < Thor
17
+ include Thor::Actions
18
+ check_unknown_options!
19
+
20
+ def self.exit_on_failure?
21
+ true
22
+ end
23
+
24
+ def self.source_root
25
+ File.dirname(__FILE__)
26
+ end
27
+
28
+ desc 'version', 'Display MyGem version'
29
+ map %w[-v --version] => :version
30
+
31
+ def version
32
+ say "Legion::CLI #{VERSION}"
33
+ end
34
+
35
+ desc 'lex', 'used to build LEXs'
36
+ subcommand 'lex', Legion::Cli::LexBuilder
37
+
38
+ desc 'cohort', ''
39
+ subcommand 'cohort', Legion::Cli::Cohort
40
+
41
+ desc 'function', 'deal with functions'
42
+ subcommand 'function', Legion::Cli::Function
43
+
44
+ desc 'relationship', 'creates and manages relationships'
45
+ subcommand 'relationship', Legion::Cli::Relationship
46
+
47
+ desc 'task', 'creates and manages tasks'
48
+ subcommand 'task', Legion::Cli::Task
49
+
50
+ desc 'chain', 'creates and manages chains'
51
+ subcommand 'chain', Legion::Cli::Chain
52
+
53
+ desc 'trigger', 'sends a task to a worker'
54
+ subcommand 'trigger', Legion::Cli::Trigger
55
+ end
56
+ end
@@ -0,0 +1,35 @@
1
+ module Legion
2
+ class Cli
3
+ class Chain < Thor
4
+ desc 'create', 'create'
5
+ def create(name)
6
+ Legion::Service.new(cache: false, crypt: false, extensions: false, log_level: 'error')
7
+ say "chain created, id: #{Legion::Data::Model::Chain.insert({ name: name })}", :green
8
+ end
9
+
10
+ desc 'show', 'show'
11
+ option :limit, type: :numeric, required: true, default: 10, desc: 'how many tasks to return'
12
+ def show
13
+ Legion::Service.new(cache: false, crypt: false, extensions: false, log_level: 'error')
14
+ rows = [%w[id name active]]
15
+ Legion::Data::Model::Chain.limit(options[:limit]).order(:id).reverse_each do |row|
16
+ rows.push([row.values[:id], row.values[:name], row.values[:active]])
17
+ end
18
+
19
+ print_table rows
20
+ end
21
+
22
+ desc 'delete', 'delete'
23
+ option :confirm, type: :boolean
24
+ def delete(id)
25
+ Legion::Service.new(cache: false, crypt: false, extensions: false, log_level: 'error')
26
+ return if !options[:confirm] && !(yes? "Are you sure you want to delete chain #{id} and all dependent relationships", :red)
27
+
28
+ Legion::Data::Model::Chain[id].delete
29
+ say 'Deleted!'
30
+ end
31
+
32
+ default_task :show
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,10 @@
1
+ module Legion
2
+ class Cli
3
+ class Cohort < Thor
4
+ desc 'import :id', 'imports a cohort for usage'
5
+ def import(id)
6
+ say "Importing Cohort #{id}", :green
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,41 @@
1
+ module Legion
2
+ class Cli
3
+ class Function < Thor
4
+ desc 'find', 'find'
5
+ option :internal, type: :boolean, default: false
6
+ def find
7
+ Legion::Service.new(cache: false, crypt: false, extensions: false, log_level: 'error')
8
+ response = ask 'trigger extension?', limited_to: Legion::Data::Model::Extension.map(:name)
9
+ trigger_extension = Legion::Data::Model::Extension.where(name: response).first
10
+ runners = Legion::Data::Model::Runner.where(extension_id: trigger_extension.values[:id])
11
+ if runners.count == 1
12
+ trigger_runner = runners.first
13
+ say "Auto selecting #{trigger_runner.values[:name]} since it is the only option"
14
+ else
15
+ response = ask 'trigger runner?', limited_to: runners.map(:name)
16
+ trigger_runner = Legion::Data::Model::Runner.where(name: response).where(extension_id: trigger_extension.values[:id]).first
17
+ end
18
+
19
+ functions = Legion::Data::Model::Function.where(runner_id: trigger_runner.values[:id])
20
+
21
+ if functions.count == 1
22
+ trigger_function = functions.first
23
+ say "Auto selecting #{trigger_function.values[:name]} since it is the only option"
24
+ else
25
+ response = ask 'trigger function?', limited_to: Legion::Data::Model::Function.where(runner_id: trigger_runner.values[:id]).map(:name)
26
+ trigger_function = trigger_runner.functions(name: response).first
27
+ end
28
+ # say "#{trigger_runner.values[:namespace]}.#{trigger_function.values[:name]} selected as trigger", :green, :italicized
29
+ # mute { trigger_function.values[:id] }
30
+ say trigger_function.values[:id]
31
+ # puts self.methods(false)
32
+ end
33
+
34
+ desc 'delete', 'delete'
35
+ def delete; end
36
+
37
+ desc 'activate', 'activate'
38
+ def activate; end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,31 @@
1
+ module Legion
2
+ class Cli
3
+ module Lex
4
+ class Actor < Thor
5
+ include Thor::Actions
6
+
7
+ def self.source_root
8
+ File.dirname(__FILE__)
9
+ end
10
+
11
+ no_commands do
12
+ def lex
13
+ Dir.pwd.split('/').last.split('-').last
14
+ end
15
+ end
16
+
17
+ desc 'create :name', 'creates a new actor'
18
+ method_option :type, enum: %w[subscription every poll once loop], default: 'subscription'
19
+ def create(name)
20
+ template('templates/actor.erb', "#{lex}/lib/actors/#{name}.rb", { name: name, lex: lex, type: options[:type] })
21
+ end
22
+
23
+ desc 'delete :name', 'deletes an actor'
24
+ def delete(name)
25
+ remove_file("lib/legion/extensions/#{lex}/actors/#{name}.rb")
26
+ remove_file("spec/actors/#{name}_spec.rb")
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,32 @@
1
+ module Legion
2
+ class Cli
3
+ module Lex
4
+ class Exchange < Thor
5
+ include Thor::Actions
6
+
7
+ def self.source_root
8
+ File.dirname(__FILE__)
9
+ end
10
+
11
+ no_commands do
12
+ def lex
13
+ Dir.pwd.split('/').last.split('-').last
14
+ end
15
+ end
16
+
17
+ desc 'create :name', 'creates a new exchange class'
18
+ def create(name)
19
+ template('templates/queue.erb', "lib/legion/extensions/#{lex}/transport/exchanges/#{name}.rb", { name: name, lex: lex })
20
+ template('templates/queue_spec.erb', "spec/exchanges/#{name}_spec.rb", { name: name, lex: lex })
21
+ end
22
+
23
+ desc 'delete :name', 'deletes an exchange class'
24
+ def delete(name)
25
+ remove_file("lib/legion/extensions/#{lex}/transport/exchanges/#{name}.rb")
26
+ remove_file("spec/exchanges/#{name}_spec.rb")
27
+ remove_file("spec/transport/exchanges/#{name}_spec.rb")
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,32 @@
1
+ module Legion
2
+ class Cli
3
+ module Lex
4
+ class Message < Thor
5
+ include Thor::Actions
6
+
7
+ def self.source_root
8
+ File.dirname(__FILE__)
9
+ end
10
+
11
+ no_commands do
12
+ def lex
13
+ Dir.pwd.split('/').last.split('-').last
14
+ end
15
+ end
16
+
17
+ desc 'create :name', 'creates a new message'
18
+ def create(name)
19
+ template('templates/message.erb', "lib/legion/extensions/#{lex}/transport/messages/#{name}.rb", { name: name, lex: lex })
20
+ template('templates/message_spec.erb', "spec/messages/#{name}_spec.rb", { name: name, lex: lex })
21
+ end
22
+
23
+ desc 'delete :name', 'deletes a message class'
24
+ def delete(name)
25
+ remove_file("lib/legion/extensions/#{lex}/transport/messages/#{name}.rb")
26
+ remove_file("spec/messages/#{name}_spec.rb")
27
+ remove_file("spec/transport/messages/#{name}_spec.rb")
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end