lex-tasker 0.1.3 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a86403f5cd4a6634b29ca4000c36734bb53d4f5913682e2c4f60c97defa6f590
4
- data.tar.gz: 4dfce03dd9c7ac9b8d5ccfe21d085328fce549c25367b088a33b3df563f6f0b5
3
+ metadata.gz: 8086ae496d1e741099cd0f91cd01cfaa91820518d3a93e00262ae04eeea471a5
4
+ data.tar.gz: 219a9d573a6e17923f01b2528fb91c1235dc1e62cf7b2dcdf9624dd99abc8d75
5
5
  SHA512:
6
- metadata.gz: 2017fff11b5c75e1701cce065e7cc38b61bd2b7dbd7cb0de72d0441df9a74e93bac47a9ad9191c3c10c80548b6aa13847ffc26f8189b35fc87ae72311c3e1a3b
7
- data.tar.gz: d501e798ea13a21333a2f67975d412338a929a23c5e2345af28f88d19be2e0420af9d0ec781cb06033ce2536dd40b33d04c96ada2aa85a8491b5f99fc7a5b0c6
6
+ metadata.gz: c7c51a2a36129f803d34b2b7aaecc43c06bdd619fabd8dd8ee37b0cf9a38a1ff68b8c0983efd3b742e22332bfc05afdb8507a04c7a62f0500ec22fb621e9ba6e
7
+ data.tar.gz: 7a98de6ea1d7b663992457e2c61df4a46c984ac41af4f30218404ea3798ecd0da3b8a880a86128637bb36105f971bc09a5c577934e1a7b2de633edefcb1ad14e
@@ -5,57 +5,35 @@ orbs:
5
5
  jobs:
6
6
  "rubocop":
7
7
  docker:
8
- - image: circleci/ruby:2.5-node
8
+ - image: circleci/ruby:2.7
9
9
  steps:
10
10
  - checkout
11
11
  - ruby/load-cache
12
12
  - ruby/install-deps
13
+ - run:
14
+ name: Update bundler
15
+ command: gem update bundler
13
16
  - run:
14
17
  name: Run Rubocop
15
18
  command: bundle exec rubocop
16
19
  - ruby/save-cache
17
- "ruby-two-five":
18
- docker:
19
- - image: circleci/ruby:2.5
20
- - image: memcached:1.5-alpine
21
- steps:
22
- - checkout
23
- - ruby/load-cache
24
- - ruby/install-deps
25
- - ruby/run-tests
26
- - ruby/save-cache
27
- "ruby-two-six":
28
- docker:
29
- - image: circleci/ruby:2.6
30
- - image: memcached:1.5-alpine
31
- steps:
32
- - checkout
33
- - ruby/load-cache
34
- - ruby/install-deps
35
- - ruby/run-tests
36
- - ruby/save-cache
37
- "ruby-two-seven":
20
+ "rspec":
38
21
  docker:
39
22
  - image: circleci/ruby:2.7
40
- - image: memcached:1.5-alpine
41
23
  steps:
42
24
  - checkout
43
25
  - ruby/load-cache
44
26
  - ruby/install-deps
27
+ - run:
28
+ name: Update bundler
29
+ command: gem update bundler
45
30
  - ruby/run-tests
46
31
  - ruby/save-cache
47
-
48
32
  workflows:
49
33
  version: 2
50
34
  rubocop-rspec:
51
35
  jobs:
52
36
  - rubocop
53
- - ruby-two-five:
54
- requires:
55
- - rubocop
56
- - ruby-two-six:
57
- requires:
58
- - ruby-two-five
59
- - ruby-two-seven:
37
+ - rspec:
60
38
  requires:
61
- - ruby-two-five
39
+ - rubocop
@@ -0,0 +1,69 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ lex-tasker (0.2.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ ast (2.4.1)
10
+ codecov (0.2.12)
11
+ json
12
+ simplecov
13
+ diff-lcs (1.4.4)
14
+ docile (1.3.2)
15
+ json (2.3.1)
16
+ parallel (1.19.2)
17
+ parser (2.7.2.0)
18
+ ast (~> 2.4.1)
19
+ rainbow (3.0.0)
20
+ rake (13.0.1)
21
+ regexp_parser (1.8.2)
22
+ rexml (3.2.4)
23
+ rspec (3.10.0)
24
+ rspec-core (~> 3.10.0)
25
+ rspec-expectations (~> 3.10.0)
26
+ rspec-mocks (~> 3.10.0)
27
+ rspec-core (3.10.0)
28
+ rspec-support (~> 3.10.0)
29
+ rspec-expectations (3.10.0)
30
+ diff-lcs (>= 1.2.0, < 2.0)
31
+ rspec-support (~> 3.10.0)
32
+ rspec-mocks (3.10.0)
33
+ diff-lcs (>= 1.2.0, < 2.0)
34
+ rspec-support (~> 3.10.0)
35
+ rspec-support (3.10.0)
36
+ rspec_junit_formatter (0.4.1)
37
+ rspec-core (>= 2, < 4, != 2.12.0)
38
+ rubocop (1.1.0)
39
+ parallel (~> 1.10)
40
+ parser (>= 2.7.1.5)
41
+ rainbow (>= 2.2.2, < 4.0)
42
+ regexp_parser (>= 1.8)
43
+ rexml
44
+ rubocop-ast (>= 1.0.1)
45
+ ruby-progressbar (~> 1.7)
46
+ unicode-display_width (>= 1.4.0, < 2.0)
47
+ rubocop-ast (1.1.0)
48
+ parser (>= 2.7.1.5)
49
+ ruby-progressbar (1.10.1)
50
+ simplecov (0.19.1)
51
+ docile (~> 1.1)
52
+ simplecov-html (~> 0.11)
53
+ simplecov-html (0.12.3)
54
+ unicode-display_width (1.7.0)
55
+
56
+ PLATFORMS
57
+ ruby
58
+
59
+ DEPENDENCIES
60
+ bundler (>= 2)
61
+ codecov
62
+ lex-tasker!
63
+ rake
64
+ rspec
65
+ rspec_junit_formatter
66
+ rubocop
67
+
68
+ BUNDLED WITH
69
+ 2.1.4
data/README.md CHANGED
@@ -1,28 +1,3 @@
1
1
  # Legion::Extensions::Tasker
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/legion/extensions/tasker`. To experiment with that code, run `bin/console` for an interactive prompt.
4
-
5
-
6
- ## Installation
7
-
8
- Add this line to your application's Gemfile:
9
-
10
- ```ruby
11
- gem 'lex-tasker'
12
- ```
13
-
14
- And then execute:
15
-
16
- $ bundle
17
-
18
- Or install it yourself as:
19
-
20
- $ gem install lex-tasker
21
-
22
- ## Usage
23
-
24
- TODO: Write usage instructions here
25
-
26
- ## License
27
-
28
- The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
3
+ A core LEX
@@ -0,0 +1,69 @@
1
+ image: ruby:2.7
2
+
3
+ pipelines:
4
+ tags:
5
+ "v*":
6
+ - step:
7
+ name: Rubocop
8
+ caches:
9
+ - bundler
10
+ script:
11
+ - gem install rubocop
12
+ - gem update rubocop
13
+ - rubocop
14
+ - step:
15
+ name: RSpec
16
+ caches:
17
+ - bundler
18
+ script:
19
+ - gem install bundler
20
+ - gem update bundler
21
+ - bundle update
22
+ - bundle exec rspec
23
+ - step:
24
+ name: Push to RubyGems
25
+ deployment: RubyGems
26
+ script:
27
+ - gem install bundler gem-release rspec
28
+ - bundle install
29
+ - (umask 077 ; echo $gem_creds | base64 --decode > ~/.gem/credentials)
30
+ - gem release
31
+ artifacts:
32
+ - pkg/**
33
+ branches:
34
+ master:
35
+ - step:
36
+ caches:
37
+ - bundler
38
+ script:
39
+ - gem install rubocop
40
+ - gem update rubocop
41
+ - rubocop
42
+ - step:
43
+ caches:
44
+ - bundler
45
+ script:
46
+ - gem install bundler
47
+ - gem update bundler
48
+ - bundle update
49
+ - bundle exec rspec
50
+ develop:
51
+ - step:
52
+ caches:
53
+ - bundler
54
+ script:
55
+ - gem install rubocop
56
+ - gem update rubocop
57
+ - rubocop
58
+ - step:
59
+ caches:
60
+ - bundler
61
+ script:
62
+ - gem install bundler
63
+ - gem update bundler
64
+ - bundle update
65
+ - bundle exec rspec
66
+
67
+ definitions:
68
+ caches:
69
+ bundler: /usr/local/bundle
@@ -5,7 +5,7 @@ require 'legion/extensions/tasker/version'
5
5
  Gem::Specification.new do |spec|
6
6
  spec.name = 'lex-tasker'
7
7
  spec.version = Legion::Extensions::Tasker::VERSION
8
- spec.authors = ['Miverson']
8
+ spec.authors = ['Esity']
9
9
  spec.email = ['matthewdiverson@gmail.com']
10
10
 
11
11
  spec.summary = 'LEX::Tasker manages tasks status from Legion'
@@ -14,18 +14,20 @@ Gem::Specification.new do |spec|
14
14
  spec.license = 'MIT'
15
15
  spec.required_ruby_version = Gem::Requirement.new('>= 2.5.0')
16
16
 
17
- # Specify which files should be added to the gem when it is released.
18
- # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
19
17
  spec.files = Dir.chdir(File.expand_path(__dir__)) do
20
18
  `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
21
19
  end
22
- spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.metadata['homepage_uri'] = spec.homepage
21
+ spec.metadata['source_code_uri'] = 'https://bitbucket.org/legion-io/lex-tasker/src'
22
+ spec.metadata['documentation_uri'] = 'https://legionio.atlassian.net/wiki/spaces/LEX/pages/7733408'
23
+ spec.metadata['changelog_uri'] = 'https://legionio.atlassian.net/wiki/spaces/LEX/pages/12156936'
24
+ spec.metadata['bug_tracker_uri'] = 'https://bitbucket.org/legion-io/lex-tasker/issues'
23
25
  spec.require_paths = ['lib']
24
26
 
25
- spec.add_development_dependency 'bundler'
27
+ spec.add_development_dependency 'bundler', '>= 2'
26
28
  spec.add_development_dependency 'codecov'
27
29
  spec.add_development_dependency 'rake'
28
30
  spec.add_development_dependency 'rspec'
31
+ spec.add_development_dependency 'rspec_junit_formatter'
29
32
  spec.add_development_dependency 'rubocop'
30
- spec.add_development_dependency 'rubocop-performance'
31
33
  end
@@ -1,10 +1,17 @@
1
1
  require 'legion/extensions/tasker/version'
2
- require 'legion/extensions'
3
2
 
4
3
  module Legion
5
4
  module Extensions
6
5
  module Tasker
7
- extend Legion::Extensions::Core
6
+ extend Legion::Extensions::Core if Legion::Extensions.const_defined? :Core
7
+
8
+ def self.data_required?
9
+ true
10
+ end
11
+
12
+ def data_required?
13
+ true
14
+ end
8
15
  end
9
16
  end
10
17
  end
@@ -5,9 +5,21 @@ module Legion::Extensions::Tasker
5
5
  'fetch'
6
6
  end
7
7
 
8
+ def runner_class
9
+ Legion::Extensions::Tasker::Runners::FetchDelayed
10
+ end
11
+
8
12
  def use_runner?
9
13
  false
10
14
  end
15
+
16
+ def check_subtask?
17
+ false
18
+ end
19
+
20
+ def generate_task?
21
+ false
22
+ end
11
23
  end
12
24
  end
13
25
  end
@@ -1,10 +1,6 @@
1
1
  module Legion::Extensions::Tasker
2
2
  module Actor
3
3
  class Log < Legion::Extensions::Actors::Subscription
4
- def runner_function
5
- 'add_log'
6
- end
7
-
8
4
  def check_subtask?
9
5
  false
10
6
  end
@@ -0,0 +1,21 @@
1
+ module Legion::Extensions::Tasker
2
+ module Actor
3
+ class TaskManager < Legion::Extensions::Actors::Subscription
4
+ def use_runner?
5
+ true
6
+ end
7
+
8
+ def check_subtask?
9
+ true
10
+ end
11
+
12
+ def generate_task?
13
+ true
14
+ end
15
+
16
+ def prefetch
17
+ 1
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,11 @@
1
+ module Legion
2
+ module Extensions
3
+ module Tasker
4
+ module Helpers
5
+ module Base
6
+ def send_task(**opts); end
7
+ end
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,66 @@
1
+ module Legion
2
+ module Extensions
3
+ module Tasker
4
+ module Helpers
5
+ module FetchDelayed
6
+ def find_trigger(runner_class:, function:, **)
7
+ sql = "SELECT `functions`.`id` as `function_id`, `runner_id`, `runners`.`namespace`
8
+ FROM `legion`.`functions`
9
+ INNER JOIN `legion`.`runners` ON (`functions`.`runner_id` = `runners`.`id`)
10
+ WHERE `functions`.`name` = '#{function}'
11
+ AND `runners`.`namespace` = '#{runner_class}' LIMIT 1;"
12
+
13
+ cache = Legion::Cache.get(sql)
14
+ return cache unless cache.nil?
15
+
16
+ results = Legion::Data::Connection.sequel.fetch(sql).first
17
+ Legion::Cache.set(sql, results) if results.is_a?(Hash) && results.count.positive?
18
+ results
19
+ end
20
+
21
+ def find_delayed(**)
22
+ sql = '
23
+ SELECT tasks.id, tasks.relationship_id, tasks.function_id, tasks.created,
24
+ relationships.delay as relationship_delay, relationships.chain_id,
25
+ functions.name as function_name,
26
+ runners.namespace as class, runners.id as runner_id,
27
+ CONCAT( `exchange`, ".",`queue`,".",`functions`.`name`) AS runner_routing_key
28
+ FROM legion.tasks
29
+ INNER JOIN legion.functions ON (functions.id = tasks.function_id)
30
+ INNER JOIN legion.runners ON (runners.id = functions.runner_id)
31
+ INNER JOIN `legion`.`extensions` ON (`runners`.`extension_id` = `extensions`.`id`)
32
+ LEFT JOIN legion.relationships ON (relationships.id = tasks.relationship_id)
33
+ WHERE status = \'task.delayed\';'
34
+
35
+ Legion::Data::Connection.sequel.fetch(sql)
36
+ end
37
+
38
+ def find_subtasks(trigger_id:, **)
39
+ sql = "
40
+ SELECT
41
+ `relationships`.`id` as `relationship_id`, `debug`,
42
+ `chain_id`, `allow_new_chains`, `delay`, `trigger_id`, `action_id`, `conditions`, `transformation`,
43
+ `runners`.`namespace`, `runners`.`id` as `runner_id`, `runners`.`queue`,
44
+ `runners`.`namespace` as runner_class,
45
+ `functions`.`id` as `function_id`, `functions`.`name` as `function`,
46
+ `extensions`.`exchange`,
47
+ CONCAT( `exchange`, '.',`queue`,'.',`functions`.`name`) AS runner_routing_key
48
+ FROM `legion`.`relationships`
49
+ INNER JOIN `legion`.`functions` ON (`functions`.`id` = `relationships`.`action_id`)
50
+ INNER JOIN `legion`.`runners` ON (`functions`.`runner_id` = `runners`.`id`)
51
+ INNER JOIN `legion`.`extensions` ON (`runners`.`extension_id` = `extensions`.`id`)
52
+ WHERE `relationships`.`trigger_id` = #{trigger_id} AND `relationships`.`active` = 1;
53
+ "
54
+
55
+ cache = Legion::Cache.get(sql)
56
+ return cache unless cache.nil?
57
+
58
+ results = Legion::Data::Connection.sequel.fetch(sql)
59
+ Legion::Cache.set(sql, results, ttl: 5) if results.is_a?(Hash) && results.count.positive?
60
+ results
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,49 @@
1
+ module Legion
2
+ module Extensions
3
+ module Tasker
4
+ module Helpers
5
+ module FindSubtask
6
+ def find_trigger(runner_class:, function:, **)
7
+ sql = "SELECT `functions`.`id` as `function_id`, `runner_id`, `runners`.`namespace`
8
+ FROM `legion`.`functions`
9
+ INNER JOIN `legion`.`runners` ON (`functions`.`runner_id` = `runners`.`id`)
10
+ WHERE `functions`.`name` = '#{function}'
11
+ AND `runners`.`namespace` = '#{runner_class}' LIMIT 1;"
12
+
13
+ cache = Legion::Cache.get(sql)
14
+ return cache unless cache.nil?
15
+
16
+ results = Legion::Data::Connection.sequel.fetch(sql).first
17
+ Legion::Cache.set(sql, results) if results.is_a?(Hash) && results.count.positive?
18
+ results
19
+ end
20
+
21
+ def find_subtasks(trigger_id:, **)
22
+ sql = "
23
+ SELECT
24
+ `relationships`.`id` as `relationship_id`, `debug`,
25
+ `chain_id`, `allow_new_chains`, `delay`, `trigger_id`, `action_id`, `conditions`, `transformation`,
26
+ `runners`.`namespace`, `runners`.`id` as `runner_id`, `runners`.`queue`,
27
+ `runners`.`namespace` as runner_class,
28
+ `functions`.`id` as `function_id`, `functions`.`name` as `function`,
29
+ `extensions`.`exchange`,
30
+ CONCAT( `exchange`, '.',`queue`,'.',`functions`.`name`) AS runner_routing_key
31
+ FROM `legion`.`relationships`
32
+ INNER JOIN `legion`.`functions` ON (`functions`.`id` = `relationships`.`action_id`)
33
+ INNER JOIN `legion`.`runners` ON (`functions`.`runner_id` = `runners`.`id`)
34
+ INNER JOIN `legion`.`extensions` ON (`runners`.`extension_id` = `extensions`.`id`)
35
+ WHERE `relationships`.`trigger_id` = #{trigger_id} AND `relationships`.`active` = 1;
36
+ "
37
+
38
+ cache = Legion::Cache.get(sql)
39
+ return cache unless cache.nil?
40
+
41
+ results = Legion::Data::Connection.sequel.fetch(sql)
42
+ Legion::Cache.set(sql, results, ttl: 5) if results.is_a?(Hash) && results.count.positive?
43
+ results
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
@@ -4,114 +4,93 @@ module Legion::Extensions::Tasker
4
4
  module Runners
5
5
  module CheckSubtask
6
6
  include Legion::Extensions::Helpers::Lex
7
+ extend Legion::Extensions::Tasker::Helpers::FindSubtask
7
8
 
8
9
  def check_subtasks(runner_class:, function:, **opts)
9
- runner_record = Legion::Data::Model::Runner[namespace: runner_class]
10
- return if runner_record.nil?
10
+ trigger = find_trigger(runner_class: runner_class, function: function)
11
11
 
12
- function_record = runner_record.functions_dataset[name: function]
13
- return if function_record.nil?
14
-
15
- relationships = function_record.trigger_relationships_dataset.where(:active)
16
- relationships.where(chain_id: opts[:chain_id] || :allow_new_chains) if opts.key? :chain_id
17
- return { success: true, count: relationships.count } if relationships.count.zero?
18
-
19
- relationships.each do |relationship|
20
- unless relationship.values[:allow_new_chains]
21
- next if relationship.chain.nil?
12
+ find_subtasks(trigger_id: trigger[:function_id]).each do |relationship|
13
+ unless relationship[:allow_new_chains]
14
+ next if relationship[:chain_id].nil?
22
15
  next unless opts.key? :chain_id
23
- next unless relationship.values[:chain_id] == opts[:chain_id]
16
+ next unless relationship[:chain_id] == opts[:chain_id]
24
17
  end
25
18
 
26
- action_function = relationship.action
27
- action_runner = action_function.runner
28
-
29
- status = relationship.values[:delay].zero? ? 'conditioner.queued' : 'task.delayed'
30
-
31
- task_id_hash = { runner_class: action_runner.values[:namespace],
32
- function: action_function.values[:name],
33
- status: status,
34
- relationship_id: relationship.values[:id] }
35
- task_id_hash[:payload] = opts
19
+ task_hash = relationship
20
+ task_hash[:status] = relationship[:delay].zero? ? 'conditioner.queued' : 'task.delayed'
21
+ task_hash[:payload] = opts
36
22
 
37
23
  if opts.key? :master_id
38
- task_id_hash[:master_id] = opts[:master_id]
24
+ task_hash[:master_id] = opts[:master_id]
39
25
  elsif opts.key? :parent_id
40
- task_id_hash[:master_id] = opts[:parent_id]
26
+ task_hash[:master_id] = opts[:parent_id]
41
27
  elsif opts.key? :task_id
42
- task_id_hash[:master_id] = opts[:task_id]
28
+ task_hash[:master_id] = opts[:task_id]
43
29
  end
44
30
 
45
- task_id_hash[:parent_id] = opts[:task_id] if opts.key? :task_id
31
+ task_hash[:parent_id] = opts[:task_id] if opts.key? :task_id
32
+ task_hash[:routing_key] = if relationship[:conditions].is_a?(String) && relationship[:conditions].length > 4
33
+ 'task.subtask.conditioner'
34
+ elsif relationship[:transformation].is_a?(String) && relationship[:transformation].length > 4 # rubocop:disable Layout/LineLength
35
+ 'task.subtask.transformation'
36
+ else
37
+ relationship[:runner_routing_key]
38
+ end
39
+
46
40
  if opts[:result].is_a? Array
47
41
  opts[:result].each do |result|
48
- send_task(task_id_hash,
49
- relationship: relationship,
50
- runner_record: runner_record,
51
- function_record: function_record,
52
- action_function: action_function,
53
- action_runner: action_runner,
54
- result: result)
42
+ send_task(results: result,
43
+ trigger_runner_id: trigger[:runner_id],
44
+ trigger_function_id: trigger[:function_id],
45
+ **task_hash)
55
46
  end
56
47
  else
57
- send_task(task_id_hash,
58
- relationship: relationship,
59
- runner_record: runner_record,
60
- function_record: function_record,
61
- action_function: action_function,
62
- action_runner: action_runner,
63
- **opts)
48
+ results = if opts[:results].is_a? Hash
49
+ opts[:results]
50
+ elsif opts[:result].is_a? Hash
51
+ opts[:result]
52
+ else
53
+ opts
54
+ end
55
+ send_task(
56
+ results: results,
57
+ trigger_runner_id: trigger[:runner_id],
58
+ trigger_function_id: trigger[:function_id],
59
+ **task_hash
60
+ )
64
61
  end
65
62
  end
66
- rescue StandardError => e
67
- Legion::Logging.fatal e.message
68
- Legion::Logging.fatal e.backtrace
69
- Legion::Logging.fatal runner_class
70
- Legion::Logging.fatal function
71
- Legion::Logging.fatal opts.keys
72
- Legion::Logging.fatal opts[:entry]
73
63
  end
74
64
 
75
- def send_task(task_id_hash, relationship:, runner_record:, function_record:, action_function:, action_runner:, **opts) # rubocop:disable Layout/LineLength
76
- task_id = Legion::Runner::Status.generate_task_id(**task_id_hash)[:task_id]
77
-
78
- return { status: true } unless relationship.values[:delay].zero?
65
+ def send_task(**opts)
66
+ opts[:results] = opts[:result] if opts.key?(:result) && !opts.key?(:results)
67
+ opts[:success] = if opts.key?(:result) && opts.key?(:success)
68
+ opts[:result][:success]
69
+ elsif opts.key?(:success)
70
+ opts[:success]
71
+ else
72
+ 1
73
+ end
79
74
 
80
- subtask_hash = {
81
- relationship_id: relationship.values[:id],
82
- chain_id: relationship.values[:chain_id],
83
- trigger_runner_id: runner_record.values[:id],
84
- trigger_function_id: function_record.values[:id],
85
- function_id: action_function.values[:id],
86
- function: action_function.values[:name],
87
- runner_id: action_runner.values[:id],
88
- runner_class: action_runner.values[:namespace],
89
- conditions: relationship.values[:conditions],
90
- transformation: relationship.values[:transformation],
91
- debug: relationship.values[:debug] && 1 || 0,
92
- task_id: task_id,
93
- results: opts[:result]
94
- }
75
+ # opts[:task_id] = Legion::Runner::Status.generate_task_id(**opts)[:task_id]
76
+ opts[:task_id] = insert_task(**opts)
77
+ return { status: true } unless opts[:delay].zero?
95
78
 
96
- subtask_hash[:routing_key] = if subtask_hash[:conditions].is_a?(String) && subtask_hash[:conditioners].length > 4 # rubocop:disable Layout/LineLength
97
- 'task.subtask.conditioner'
98
- elsif subtask_hash[:transformation].is_a?(String) && subtask_hash[:transformation].length > 4 # rubocop:disable Layout/LineLength
99
- 'task.subtask.transform'
100
- else
101
- "#{runner_record.extension.values[:exchange]}.#{runner_record.values[:queue]}.#{subtask_hash[:function]}" # rubocop:disable Layout/LineLength
102
- end
79
+ Legion::Transport::Messages::SubTask.new(**opts).publish
80
+ end
103
81
 
104
- subtask_hash[:success] = if opts.nil?
105
- 1
106
- elsif opts.key?(:result)
107
- # opts[:result][:success]
108
- 1
109
- elsif opts.key?(:success)
110
- opts[:success]
111
- else
112
- 1
113
- end
114
- Legion::Transport::Messages::SubTask.new(**subtask_hash).publish
82
+ def insert_task(relationship_id:, function_id:, status: 'task.queued', master_id: nil, parent_id: nil, **opts)
83
+ insert_hash = { relationship_id: relationship_id, function_id: function_id, status: status }
84
+ insert_hash[:master_id] = if master_id.is_a? Integer
85
+ master_id
86
+ elsif parent_id.is_a? Integer
87
+ parent_id
88
+ end
89
+ insert_hash[:parent_id] = parent_id if parent_id.is_a? Integer
90
+ insert_hash[:payload] = Legion::JSON.dump(opts)
91
+ # insert_hash[:function_args] = nil
92
+ # insert_hash[:results] = nil
93
+ Legion::Data::Model::Task.insert(insert_hash)
115
94
  end
116
95
  end
117
96
  end
@@ -1,50 +1,71 @@
1
1
  module Legion::Extensions::Tasker::Runners
2
2
  module FetchDelayed
3
- include Legion::Extensions::Helpers::Lex
3
+ extend Legion::Extensions::Tasker::Helpers::FetchDelayed
4
+ include Legion::Extensions::Helpers::Task
4
5
 
5
6
  def fetch(**_opts)
6
- tasks = Legion::Data::Model::Task.where(status: 'task.delayed')
7
- tasks_pushed = []
8
- log.debug "tasks.count = #{tasks.count}"
9
- tasks.each do |task|
10
- relationship = task.relationship
11
- next if !task.relationship.nil? && Time.now < task.values[:created] + relationship.values[:delay]
12
-
13
- # next if Time.now < task.values[:created] + task.values[:delay]
14
-
15
- subtask = Legion::Transport::Messages::SubTask.new(
16
- relationship_id: relationship.values[:id],
17
- chain_id: relationship.values[:chain_id],
18
- trigger_runner_id: relationship.trigger.runner.values[:id],
19
- trigger_function_id: relationship.values[:trigger_id],
20
- function_id: relationship.action.values[:id],
21
- function: relationship.action.values[:name],
22
- runner_id: relationship.action.values[:runner_id],
23
- runner_class: relationship.action.runner.values[:namespace],
24
- conditions: relationship.values[:conditions],
25
- transformation: relationship.values[:transformation],
26
- # debug: relationship.values[:debug],
27
- task_id: task.values[:id]
28
- # results: task.values[:payload]
29
- )
30
- subtask.publish
31
- task.update(status: 'conditioner.queued')
32
- tasks_pushed.push(task.values[:id])
33
- rescue StandardError => e
34
- task.update(status: 'task.push_exception')
35
- log.error e.message
36
- log.error e.backtrace
7
+ find_delayed.each do |task|
8
+ if task[:relationship_delay].is_a?(Integer) && task[:relationship_delay].positive?
9
+ next if Time.now < task[:created] + task[:relationship_delay] # rubocop:disable Style/SoleNestedConditional
10
+ end
11
+
12
+ if task[:task_delay].is_a?(Integer) && task[:task_delay].positive?
13
+ next if Time.now < task[:created] + task[:task_delay] # rubocop:disable Style/SoleNestedConditional
14
+ end
15
+
16
+ subtask_hash = {
17
+ relationship_id: task[:relationship_id],
18
+ chain_id: task[:chain_id],
19
+ function_id: task[:function_id],
20
+ function: task[:function_name],
21
+ runner_id: task[:runner_id],
22
+ runner_class: task[:runner_class],
23
+ task_id: task[:id],
24
+ exchange: task[:exchange],
25
+ queue: task[:queue]
26
+ }
27
+
28
+ subtask_hash[:conditions] = task[:conditions] if task[:conditions].is_a?(String)
29
+ subtask_hash[:transformation] = task[:transformation] if task[:transformation].is_a?(String)
30
+
31
+ subtask_hash[:routing_key] = if task[:conditions].is_a?(String) && task[:conditions].length > 4
32
+ 'task.subtask.conditioner'
33
+ elsif task[:transformation].is_a?(String) && task[:transformation].length > 4
34
+ 'task.subtask.transformation'
35
+ else
36
+ task[:runner_routing_key]
37
+ end
38
+
39
+ send_task(**subtask_hash)
40
+ case subtask_hash[:routing_key]
41
+ when 'task.subtask.conditioner'
42
+ task_update(task[:id], 'conditioner.queued')
43
+ when 'task.subtask.transformation'
44
+ task_update(task[:id], 'transformer.queued')
45
+ else
46
+ task_update(task[:id], 'task.queued')
47
+ end
37
48
  end
49
+ end
38
50
 
39
- { success: true, count: tasks_pushed.count, tasks: tasks_pushed }
40
- rescue StandardError => e
41
- Legion::Logging.error e.message
42
- Legion::Logging.error e.backtrace
51
+ def send_task(**opts)
52
+ opts[:results] = opts[:result] if opts.key?(:result) && !opts.key?(:results)
53
+ opts[:success] = if opts.key?(:result) && opts.key?(:success)
54
+ opts[:result][:success]
55
+ elsif opts.key?(:success)
56
+ opts[:success]
57
+ else
58
+ 1
59
+ end
60
+ log.debug 'pushing delayed task to worker'
61
+ Legion::Transport::Messages::Task.new(**opts).publish
43
62
  end
44
63
 
45
64
  def push(**_opts)
46
65
  Legion::Extensions::Tasker::Transport::Messages::FetchDelayed.new.publish
47
66
  { success: true }
48
67
  end
68
+
69
+ include Legion::Extensions::Helpers::Lex
49
70
  end
50
71
  end
@@ -0,0 +1,28 @@
1
+ module Legion
2
+ module Extensions
3
+ module Tasker
4
+ module Runners
5
+ module TaskManager
6
+ def purge_old(age: 31, limit: 100, status: 'task.completed', **_opts)
7
+ log.debug("purging old completed tasks with an age > #{age} days, limit: #{limit}")
8
+ dataset = Legion::Data::Model::Task
9
+ .where(Sequel.lit("created <= DATE_SUB(SYSDATE(), INTERVAL #{age} DAY)"))
10
+ .limit(limit)
11
+ dataset.where(status: status) unless ['*', nil, ''].include? status
12
+ log.debug "Deleting #{dataset.count} records" if dataset.count.positive?
13
+ dataset&.delete
14
+ end
15
+
16
+ def expire_queued(age: 1, limit: 10, **) # rubocop:disable Lint/UnusedMethodArgument
17
+ dataset = Legion::Data::Model::Task # rubocop:disable Lint/UselessAssignment
18
+ .where(status: ['conditioner.queued', 'transformer.queued', 'task.queued'])
19
+ .limit(limit)
20
+ end
21
+
22
+ include Legion::Extensions::Helpers::Task
23
+ include Legion::Extensions::Helpers::Lex
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -5,6 +5,12 @@ module Legion::Extensions::Tasker
5
5
 
6
6
  def update_status(task_id:, **opts)
7
7
  task = Legion::Data::Model::Task[task_id]
8
+ if task.nil? || task.values.nil?
9
+ return { success: false, changed: false, task_id: task_id, message: 'task nil' }
10
+ end
11
+
12
+ log.unknown task.class
13
+
8
14
  update_hash = {}
9
15
  %i[status function_args payload results].each do |column|
10
16
  next unless opts.key? column
@@ -19,6 +19,10 @@ module Legion::Extensions::Tasker
19
19
  }, {
20
20
  to: 'fetch_delayed',
21
21
  routing_key: 'fetch.delayed'
22
+ }, {
23
+ from: 'tasker',
24
+ to: 'task_manager',
25
+ routing_key: 'task.task_manager.#'
22
26
  }
23
27
  ]
24
28
  end
@@ -7,7 +7,10 @@ module Legion::Extensions::Tasker
7
7
  end
8
8
 
9
9
  def queue_options
10
- { auto_delete: false }
10
+ {
11
+ auto_delete: false,
12
+ arguments: { 'x-max-priority': 255 }
13
+ }
11
14
  end
12
15
  end
13
16
  end
@@ -5,6 +5,16 @@ module Legion::Extensions::Tasker
5
5
  def queue_name
6
6
  'task.fetch_delayed'
7
7
  end
8
+
9
+ def queue_options
10
+ {
11
+ arguments: {
12
+ 'x-single-active-consumer': true,
13
+ 'x-max-priority': 255,
14
+ 'x-message-ttl': 1
15
+ }
16
+ }
17
+ end
8
18
  end
9
19
  end
10
20
  end
@@ -0,0 +1,15 @@
1
+ module Legion::Extensions::Tasker
2
+ module Transport
3
+ module Queues
4
+ class TaskManager < Legion::Transport::Queue
5
+ def queue_name
6
+ 'tasker.task_manager'
7
+ end
8
+
9
+ def queue_options
10
+ { arguments: { 'x-single-active-consumer': true } }
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -1,7 +1,7 @@
1
1
  module Legion
2
2
  module Extensions
3
3
  module Tasker
4
- VERSION = '0.1.3'.freeze
4
+ VERSION = '0.2.0'.freeze
5
5
  end
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lex-tasker
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
- - Miverson
8
- autorequire:
7
+ - Esity
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-10-15 00:00:00.000000000 Z
11
+ date: 2020-11-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '0'
19
+ version: '2'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: '0'
26
+ version: '2'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: codecov
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -67,7 +67,7 @@ dependencies:
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
69
  - !ruby/object:Gem::Dependency
70
- name: rubocop
70
+ name: rspec_junit_formatter
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
73
  - - ">="
@@ -81,7 +81,7 @@ dependencies:
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
83
  - !ruby/object:Gem::Dependency
84
- name: rubocop-performance
84
+ name: rubocop
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
87
  - - ">="
@@ -106,21 +106,28 @@ files:
106
106
  - ".rspec"
107
107
  - ".rubocop.yml"
108
108
  - Gemfile
109
+ - Gemfile.lock
109
110
  - LICENSE.txt
110
111
  - README.md
111
112
  - Rakefile
112
113
  - bin/console
113
114
  - bin/setup
115
+ - bitbucket-pipelines.yml
114
116
  - lex-tasker.gemspec
115
117
  - lib/legion/extensions/tasker.rb
116
118
  - lib/legion/extensions/tasker/actors/check_subtask.rb
117
119
  - lib/legion/extensions/tasker/actors/fetch_delayed.rb
118
120
  - lib/legion/extensions/tasker/actors/fetch_delayed_push.rb
119
121
  - lib/legion/extensions/tasker/actors/log.rb
122
+ - lib/legion/extensions/tasker/actors/task_manager.rb
120
123
  - lib/legion/extensions/tasker/actors/updater.rb
124
+ - lib/legion/extensions/tasker/helpers/base.rb
125
+ - lib/legion/extensions/tasker/helpers/fetch_delayed.rb
126
+ - lib/legion/extensions/tasker/helpers/find_subtask.rb
121
127
  - lib/legion/extensions/tasker/runners/check_subtask.rb
122
128
  - lib/legion/extensions/tasker/runners/fetch_delayed.rb
123
129
  - lib/legion/extensions/tasker/runners/log.rb
130
+ - lib/legion/extensions/tasker/runners/task_manager.rb
124
131
  - lib/legion/extensions/tasker/runners/updater.rb
125
132
  - lib/legion/extensions/tasker/transport.rb
126
133
  - lib/legion/extensions/tasker/transport/exchanges/task.rb
@@ -130,13 +137,19 @@ files:
130
137
  - lib/legion/extensions/tasker/transport/queues/lex_register.rb
131
138
  - lib/legion/extensions/tasker/transport/queues/subtask.rb
132
139
  - lib/legion/extensions/tasker/transport/queues/task_log.rb
140
+ - lib/legion/extensions/tasker/transport/queues/task_mananger.rb
133
141
  - lib/legion/extensions/tasker/transport/queues/updater.rb
134
142
  - lib/legion/extensions/tasker/version.rb
135
143
  homepage: https://bitbucket.org/legion-io/lex-tasker
136
144
  licenses:
137
145
  - MIT
138
- metadata: {}
139
- post_install_message:
146
+ metadata:
147
+ homepage_uri: https://bitbucket.org/legion-io/lex-tasker
148
+ source_code_uri: https://bitbucket.org/legion-io/lex-tasker/src
149
+ documentation_uri: https://legionio.atlassian.net/wiki/spaces/LEX/pages/7733408
150
+ changelog_uri: https://legionio.atlassian.net/wiki/spaces/LEX/pages/12156936
151
+ bug_tracker_uri: https://bitbucket.org/legion-io/lex-tasker/issues
152
+ post_install_message:
140
153
  rdoc_options: []
141
154
  require_paths:
142
155
  - lib
@@ -151,8 +164,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
151
164
  - !ruby/object:Gem::Version
152
165
  version: '0'
153
166
  requirements: []
154
- rubygems_version: 3.1.2
155
- signing_key:
167
+ rubygems_version: 3.1.4
168
+ signing_key:
156
169
  specification_version: 4
157
170
  summary: LEX::Tasker manages tasks status from Legion
158
171
  test_files: []