zenaton 0.4.1 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. checksums.yaml +4 -4
  2. data/.circleci/config.yml +79 -47
  3. data/.gitignore +7 -0
  4. data/.rubocop.yml +4 -0
  5. data/Appraisals +13 -0
  6. data/CHANGELOG.md +29 -1
  7. data/Gemfile.lock +18 -9
  8. data/gemfiles/.bundle/config +2 -0
  9. data/gemfiles/rails_5.0.gemfile +7 -0
  10. data/gemfiles/rails_5.1.gemfile +7 -0
  11. data/gemfiles/rails_5.2.gemfile +7 -0
  12. data/lib/zenaton/client.rb +70 -3
  13. data/lib/zenaton/contexts/task.rb +33 -0
  14. data/lib/zenaton/contexts/workflow.rb +17 -0
  15. data/lib/zenaton/contexts.rb +7 -0
  16. data/lib/zenaton/engine.rb +16 -0
  17. data/lib/zenaton/interfaces/task.rb +30 -1
  18. data/lib/zenaton/interfaces/workflow.rb +15 -0
  19. data/lib/zenaton/query/builder.rb +5 -5
  20. data/lib/zenaton/refinements/big_decimal.rb +25 -0
  21. data/lib/zenaton/refinements/class.rb +21 -0
  22. data/lib/zenaton/refinements/complex.rb +22 -0
  23. data/lib/zenaton/refinements/date.rb +26 -0
  24. data/lib/zenaton/refinements/date_time.rb +40 -0
  25. data/lib/zenaton/refinements/exception.rb +24 -0
  26. data/lib/zenaton/refinements/object.rb +15 -0
  27. data/lib/zenaton/refinements/open_struct.rb +26 -0
  28. data/lib/zenaton/refinements/range.rb +21 -0
  29. data/lib/zenaton/refinements/rational.rb +22 -0
  30. data/lib/zenaton/refinements/regexp.rb +22 -0
  31. data/lib/zenaton/refinements/struct.rb +24 -0
  32. data/lib/zenaton/refinements/symbol.rb +21 -0
  33. data/lib/zenaton/refinements/time.rb +32 -0
  34. data/lib/zenaton/refinements.rb +16 -0
  35. data/lib/zenaton/services/graphql.rb +71 -0
  36. data/lib/zenaton/services/http.rb +14 -9
  37. data/lib/zenaton/services/properties.rb +11 -57
  38. data/lib/zenaton/services/serializer.rb +2 -0
  39. data/lib/zenaton/traits/with_timestamp.rb +4 -2
  40. data/lib/zenaton/traits/zenatonable.rb +9 -0
  41. data/lib/zenaton/version.rb +1 -1
  42. data/zenaton.gemspec +2 -0
  43. metadata +54 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6e51aae796ecf047bcbc9c4b3b4e4fd971191a1ebee24075b9576df8248c9c3a
4
- data.tar.gz: a53ce602e5edeab270bebb30b11d108c568c35e1994002376989d1e9a1ce1f3d
3
+ metadata.gz: 987a8312a73e5b97d12089a115fcfa4cb15876c019dbc671377ea1a8129e024f
4
+ data.tar.gz: 9cff61f323b485d832b95d00e3c4d90874966690f334a9f652c3fa41c749b24d
5
5
  SHA512:
6
- metadata.gz: 51cef86daef5b85dd46fbc4d98305d58ad277b4ee36360266a66268ecc86a9ec1fd5d3bc1ec0c0e1702af33c1f2998471f80828f52d9e88f5425512d2767d4f5
7
- data.tar.gz: 73230ed0fa51c178553ed28a39d24f21841b2f825d558453733de02c71a871a7ca29bfb65bd734f7d0f9db71f4e6035f24f5253bd5eec51f7dfdbd951e0e22a4
6
+ metadata.gz: 3482971a72f81d455dd6ff780ed421dbc4f4033d7e45a2b5334ef3da2592d2a983700b6750575147169db0ca94a59d02042742391e654f45e44ddc5a1960b8be
7
+ data.tar.gz: 8eca6ac2710232efcb022421fd7f20a9ba64e97716ca2dcc819f6effca6d144403b0941e688ef96ff91d0ea30663911b7c3b223b5ea51a4a8cd237ff9d85fe22
data/.circleci/config.yml CHANGED
@@ -1,64 +1,96 @@
1
- version: 2
1
+ version: 2.1
2
2
 
3
- shared: &shared
4
- working_directory: ~/repo
3
+ commands:
4
+ test:
5
+ description: Run automated tests
5
6
 
6
- steps:
7
- - checkout
7
+ steps:
8
+ - checkout
9
+ - restore_cache:
10
+ keys:
11
+ - v3-dependencies-{{ checksum "Gemfile.lock" }}
12
+ - v3-dependencies-
8
13
 
9
- - restore_cache:
10
- keys:
11
- - v1-dependencies-{{ checksum "Gemfile.lock" }}
12
- - v1-dependencies-
14
+ - run:
15
+ name: install dependencies
16
+ command: |
17
+ bundle install --jobs=4 --retry=3 --path vendor/bundle
13
18
 
14
- - run:
15
- name: install dependencies
16
- command: |
17
- bundle install --jobs=4 --retry=3 --path vendor/bundle
19
+ - run:
20
+ name: Install Rails gemfiles
21
+ command: |
22
+ bundle exec appraisal install
18
23
 
19
- - save_cache:
20
- paths:
21
- - ./vendor/bundle
22
- key: v1-dependencies-{{ checksum "Gemfile.lock" }}
24
+ - save_cache:
25
+ paths:
26
+ - ./vendor/bundle
27
+ key: v3-dependencies-{{ checksum "Gemfile.lock" }}
23
28
 
24
- - run:
25
- name: run code linter
26
- command: |
27
- bundle exec rubocop
29
+ - run:
30
+ name: run code linter
31
+ command: |
32
+ bundle exec rubocop --config .rubocop.yml
28
33
 
29
- - run:
30
- name: run tests
31
- command: |
32
- bundle exec rake
34
+ - run:
35
+ name: run tests
36
+ command: |
37
+ bundle exec rake
33
38
 
34
- - run:
35
- name: audit dependencies for known CVEs
36
- command: |
37
- gem install bundler-audit
38
- bundle audit check --update
39
+ - run:
40
+ name: audit dependencies for known CVEs
41
+ command: |
42
+ gem install bundler-audit
43
+ bundle audit check --update
39
44
 
40
- - store_artifacts:
41
- path: ./coverage
42
- destination: coverage
45
+ - run:
46
+ name: Run tests without Rails
47
+ command: |
48
+ bundle exec appraisal rspec \
49
+ --format RspecJunitFormatter \
50
+ --out /tmp/test-results/rspec/results.xml \
51
+ --format progress
52
+
53
+ - run:
54
+ name: Run tests with Rails
55
+ command: |
56
+ bundle exec appraisal rspec \
57
+ --format RspecJunitFormatter \
58
+ --out /tmp/test-results/rspec/results.xml \
59
+ --format progress \
60
+ -r rails_helper.rb
61
+
62
+ - store_test_results:
63
+ path: /tmp/test-results
64
+ - store_artifacts:
65
+ path: ./coverage
66
+ destination: coverage
43
67
 
44
68
  jobs:
45
- "ruby-2.3":
46
- <<: *shared
47
- docker:
48
- - image: circleci/ruby:2.3
49
- "ruby-2.4":
50
- <<: *shared
51
- docker:
52
- - image: circleci/ruby:2.4
53
- "ruby-2.5":
54
- <<: *shared
69
+ run_tests:
70
+ description: Run the tests
55
71
  docker:
56
- - image: circleci/ruby:2.5
72
+ - image: "circleci/ruby:<<parameters.ruby>>"
73
+ parameters:
74
+ ruby:
75
+ description: The ruby version to use
76
+ type: enum
77
+ enum: ["2.3", "2.4", "2.5", "2.6"]
78
+ steps:
79
+ - test
57
80
 
58
81
  workflows:
59
82
  version: 2
60
83
  build:
61
84
  jobs:
62
- - "ruby-2.3"
63
- - "ruby-2.4"
64
- - "ruby-2.5"
85
+ - run_tests:
86
+ name: "Ruby 2.3"
87
+ ruby: "2.3"
88
+ - run_tests:
89
+ name: "Ruby 2.4"
90
+ ruby: "2.4"
91
+ - run_tests:
92
+ name: "Ruby 2.5"
93
+ ruby: "2.5"
94
+ - run_tests:
95
+ name: "Ruby 2.6"
96
+ ruby: "2.6"
data/.gitignore CHANGED
@@ -6,6 +6,13 @@
6
6
  /pkg/
7
7
  /spec/reports/
8
8
  /tmp/
9
+ /vendor/
9
10
 
10
11
  # rspec failure tracking
11
12
  .rspec_status
13
+
14
+ # appraisal lock files
15
+ /gemfiles/*.lock
16
+
17
+ # asdf ruby version file
18
+ .tool-versions
data/.rubocop.yml CHANGED
@@ -3,6 +3,10 @@ require: rubocop-rspec
3
3
  AllCops:
4
4
  TargetRubyVersion: 2.3
5
5
 
6
+ Exclude:
7
+ - 'gemfiles/**/*'
8
+ - 'vendor/bundle/**/*'
9
+
6
10
  Metrics/BlockLength:
7
11
  Enabled: true
8
12
  Exclude:
data/Appraisals ADDED
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ appraise 'rails-5.0' do
4
+ gem 'rails', '~> 5.0.0'
5
+ end
6
+
7
+ appraise 'rails-5.1' do
8
+ gem 'rails', '~> 5.1.0'
9
+ end
10
+
11
+ appraise 'rails-5.2' do
12
+ gem 'rails', '~> 5.2.0'
13
+ end
data/CHANGELOG.md CHANGED
@@ -9,6 +9,32 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
9
9
 
10
10
  ### Added
11
11
 
12
+ ### Fixed
13
+
14
+ ## [0.5.0] - 2019-08-27
15
+ ### Changes
16
+ - No longer load JSON core extensions. Use our own refinements to avoid clashes
17
+ with frameworks and user code.
18
+
19
+ ### Added
20
+ - (De)Serialization support for instances of Class.
21
+ - Execution context for tasks and workflows
22
+ - Optional `on_error_retry_delay` method handling task failures and specifying
23
+ how many seconds to wait before retrying.
24
+ - Added scheduling tasks and workflows feature.
25
+
26
+ ### Fixed
27
+ - Backport of ActiveSupport's `next_occurring` for older versions.
28
+
29
+ ## [0.4.2] - 2019-08-05
30
+ ### Added
31
+
32
+ - Added `intent_id` when dispatching workflows and tasks, sending events and
33
+ pausing/resuming/stoping workflows.
34
+
35
+ ### Fixed
36
+ - Fixed an error caused by a bad class name serialization when `as_json` is overrided (in rails for example).
37
+
12
38
  ## [0.4.1] - 2019-06-04
13
39
  ### Changes
14
40
  - Fix symbol json encoding breaking compatibility with some gems
@@ -85,7 +111,9 @@ available.
85
111
  ### Added
86
112
  - Initial release.
87
113
 
88
- [Unreleased]: https://github.com/zenaton/zenaton-ruby/compare/v0.4.1...HEAD
114
+ [Unreleased]: https://github.com/zenaton/zenaton-ruby/compare/v0.5.0...HEAD
115
+ [0.5.0]: https://github.com/zenaton/zenaton-ruby/compare/v0.4.2...v0.5.0
116
+ [0.4.2]: https://github.com/zenaton/zenaton-ruby/compare/v0.4.1...v0.4.2
89
117
  [0.4.1]: https://github.com/zenaton/zenaton-ruby/compare/v0.4.0...v0.4.1
90
118
  [0.4.0]: https://github.com/zenaton/zenaton-ruby/compare/v0.3.1...v0.4.0
91
119
  [0.3.1]: https://github.com/zenaton/zenaton-ruby/compare/v0.3.0...v0.3.1
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- zenaton (0.4.1)
4
+ zenaton (0.5.0)
5
5
  activesupport
6
6
  tzinfo-data
7
7
 
@@ -13,8 +13,12 @@ GEM
13
13
  i18n (>= 0.7, < 2)
14
14
  minitest (~> 5.1)
15
15
  tzinfo (~> 1.1)
16
- addressable (2.5.2)
16
+ addressable (2.6.0)
17
17
  public_suffix (>= 2.0.2, < 4.0)
18
+ appraisal (2.2.0)
19
+ bundler
20
+ rake
21
+ thor (>= 0.14.0)
18
22
  ast (2.4.0)
19
23
  coderay (1.1.2)
20
24
  concurrent-ruby (1.1.5)
@@ -22,7 +26,7 @@ GEM
22
26
  safe_yaml (~> 1.0.0)
23
27
  diff-lcs (1.3)
24
28
  docile (1.3.1)
25
- hashdiff (0.3.7)
29
+ hashdiff (1.0.0)
26
30
  i18n (1.6.0)
27
31
  concurrent-ruby (~> 1.0)
28
32
  jaro_winkler (1.5.1)
@@ -36,7 +40,7 @@ GEM
36
40
  pry (0.11.3)
37
41
  coderay (~> 1.1.0)
38
42
  method_source (~> 0.9.0)
39
- public_suffix (3.0.2)
43
+ public_suffix (3.1.1)
40
44
  rainbow (3.0.0)
41
45
  rake (10.5.0)
42
46
  rspec (3.7.0)
@@ -52,6 +56,8 @@ GEM
52
56
  diff-lcs (>= 1.2.0, < 2.0)
53
57
  rspec-support (~> 3.7.0)
54
58
  rspec-support (3.7.1)
59
+ rspec_junit_formatter (0.4.1)
60
+ rspec-core (>= 2, < 4, != 2.12.0)
55
61
  rubocop (0.58.2)
56
62
  jaro_winkler (~> 1.5.1)
57
63
  parallel (~> 1.10)
@@ -63,33 +69,36 @@ GEM
63
69
  rubocop-rspec (1.27.0)
64
70
  rubocop (>= 0.56.0)
65
71
  ruby-progressbar (1.9.0)
66
- safe_yaml (1.0.4)
72
+ safe_yaml (1.0.5)
67
73
  simplecov (0.16.1)
68
74
  docile (~> 1.1)
69
75
  json (>= 1.8, < 3)
70
76
  simplecov-html (~> 0.10.0)
71
77
  simplecov-html (0.10.2)
78
+ thor (0.20.3)
72
79
  thread_safe (0.3.6)
73
80
  timecop (0.9.1)
74
81
  tzinfo (1.2.5)
75
82
  thread_safe (~> 0.1)
76
- tzinfo-data (1.2019.1)
83
+ tzinfo-data (1.2019.2)
77
84
  tzinfo (>= 1.0.0)
78
85
  unicode-display_width (1.4.0)
79
86
  vcr (4.0.0)
80
- webmock (3.4.2)
87
+ webmock (3.6.2)
81
88
  addressable (>= 2.3.6)
82
89
  crack (>= 0.3.2)
83
- hashdiff
90
+ hashdiff (>= 0.4.0, < 2.0.0)
84
91
 
85
92
  PLATFORMS
86
93
  ruby
87
94
 
88
95
  DEPENDENCIES
96
+ appraisal
89
97
  bundler (~> 1.16)
90
98
  pry
91
99
  rake (~> 10.0)
92
100
  rspec (~> 3.0)
101
+ rspec_junit_formatter
93
102
  rubocop
94
103
  rubocop-rspec
95
104
  simplecov
@@ -99,4 +108,4 @@ DEPENDENCIES
99
108
  zenaton!
100
109
 
101
110
  BUNDLED WITH
102
- 1.17.2
111
+ 1.17.3
@@ -0,0 +1,2 @@
1
+ ---
2
+ BUNDLE_RETRY: "1"
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "rails", "~> 5.0.0"
6
+
7
+ gemspec path: "../"
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "rails", "~> 5.1.0"
6
+
7
+ gemspec path: "../"
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "rails", "~> 5.2.0"
6
+
7
+ gemspec path: "../"
@@ -1,6 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'securerandom'
3
4
  require 'singleton'
5
+ require 'zenaton/services/graphql'
4
6
  require 'zenaton/services/http'
5
7
  require 'zenaton/services/properties'
6
8
  require 'zenaton/services/serializer'
@@ -12,6 +14,7 @@ module Zenaton
12
14
  include Singleton
13
15
 
14
16
  ZENATON_API_URL = 'https://api.zenaton.com/v1' # Zenaton api url
17
+ ZENATON_GATEWAY_URL = 'https://gateway.zenaton.com/api' # Gateway url
15
18
  ZENATON_WORKER_URL = 'http://localhost' # Default worker url
16
19
  DEFAULT_WORKER_PORT = 4001 # Default worker port
17
20
  WORKER_API_VERSION = 'v_newton' # Default worker api version
@@ -22,6 +25,7 @@ module Zenaton
22
25
  APP_ID = 'app_id' # Parameter name for the application ID
23
26
  API_TOKEN = 'api_token' # Parameter name for the API token
24
27
 
28
+ ATTR_INTENT_ID = 'intent_id' # Internal parameter for retries
25
29
  ATTR_ID = 'custom_id' # Parameter name for custom ids
26
30
  ATTR_NAME = 'name' # Parameter name for workflow names
27
31
  ATTR_CANONICAL = 'canonical_name' # Parameter name for version name
@@ -59,6 +63,7 @@ module Zenaton
59
63
  # @private
60
64
  def initialize
61
65
  @http = Services::Http.new
66
+ @graphql = Services::GraphQL.new(http: @http)
62
67
  @serializer = Services::Serializer.new
63
68
  @properties = Services::Properties.new
64
69
  end
@@ -79,6 +84,17 @@ module Zenaton
79
84
  end
80
85
  end
81
86
 
87
+ def gateway_url
88
+ ENV['ZENATON_GATEWAY_URL'] || ZENATON_GATEWAY_URL
89
+ end
90
+
91
+ def gateway_headers
92
+ {
93
+ 'app-id' => @app_id,
94
+ 'api-token' => @api_token
95
+ }
96
+ end
97
+
82
98
  # Gets the url for zenaton api
83
99
  # @param resource [String] the endpoint for the api
84
100
  # @param params [Hash|String] query params to be url encoded
@@ -98,11 +114,10 @@ module Zenaton
98
114
  # Start a single task
99
115
  # @param task [Zenaton::Interfaces::Task]
100
116
  def start_task(task)
101
- max_processing_time = if task.respond_to?(:max_processing_time)
102
- task.max_processing_time
103
- end
117
+ max_processing_time = task.try(:max_processing_time)
104
118
  @http.post(
105
119
  worker_url('tasks'),
120
+ ATTR_INTENT_ID => SecureRandom.uuid,
106
121
  ATTR_PROG => PROG,
107
122
  ATTR_NAME => class_name(task),
108
123
  ATTR_DATA => @serializer.encode(@properties.from(task)),
@@ -115,6 +130,7 @@ module Zenaton
115
130
  def start_workflow(flow)
116
131
  @http.post(
117
132
  instance_worker_url,
133
+ ATTR_INTENT_ID => SecureRandom.uuid,
118
134
  ATTR_PROG => PROG,
119
135
  ATTR_CANONICAL => canonical_name(flow),
120
136
  ATTR_NAME => class_name(flow),
@@ -123,6 +139,55 @@ module Zenaton
123
139
  )
124
140
  end
125
141
 
142
+ def start_scheduled_task(task, cron)
143
+ res = @graphql.request(
144
+ gateway_url,
145
+ Services::GraphQL::CREATE_TASK_SCHEDULE,
146
+ create_task_schedule_input(task, cron),
147
+ gateway_headers
148
+ )
149
+ res && res['createTaskSchedule']
150
+ end
151
+
152
+ def start_scheduled_workflow(flow, cron)
153
+ res = @graphql.request(
154
+ gateway_url,
155
+ Services::GraphQL::CREATE_WORKFLOW_SCHEDULE,
156
+ create_workflow_schedule_input(flow, cron),
157
+ gateway_headers
158
+ )
159
+ res && res['createWorkflowSchedule']
160
+ end
161
+
162
+ # rubocop:disable Metrics/MethodLength
163
+ def create_workflow_schedule_input(flow, cron)
164
+ {
165
+ 'createWorkflowScheduleInput' => {
166
+ 'intentId' => SecureRandom.uuid,
167
+ 'environmentName' => @app_env,
168
+ 'cron' => cron,
169
+ 'workflowName' => class_name(flow),
170
+ 'canonicalName' => canonical_name(flow) || class_name(flow),
171
+ 'programmingLanguage' => PROG.upcase,
172
+ 'properties' => @serializer.encode(@properties.from(flow))
173
+ }
174
+ }
175
+ end
176
+ # rubocop:enable Metrics/MethodLength
177
+
178
+ def create_task_schedule_input(task, cron)
179
+ {
180
+ 'createTaskScheduleInput' => {
181
+ 'intentId' => SecureRandom.uuid,
182
+ 'environmentName' => @app_env,
183
+ 'cron' => cron,
184
+ 'taskName' => class_name(task),
185
+ 'programmingLanguage' => PROG.upcase,
186
+ 'properties' => @serializer.encode(@properties.from(task))
187
+ }
188
+ }
189
+ end
190
+
126
191
  # Stops a workflow
127
192
  # @param workflow_name [String] the class name of the workflow
128
193
  # @param custom_id [String] the custom ID of the workflow (if any)
@@ -171,6 +236,7 @@ module Zenaton
171
236
  # @return [NilClass]
172
237
  def send_event(workflow_name, custom_id, event)
173
238
  body = {
239
+ ATTR_INTENT_ID => SecureRandom.uuid,
174
240
  ATTR_PROG => PROG,
175
241
  ATTR_NAME => workflow_name,
176
242
  ATTR_ID => custom_id,
@@ -249,6 +315,7 @@ module Zenaton
249
315
  params = { ATTR_ID => custom_id }
250
316
  url = instance_worker_url(params)
251
317
  options = {
318
+ ATTR_INTENT_ID => SecureRandom.uuid,
252
319
  ATTR_PROG => PROG,
253
320
  ATTR_NAME => workflow_name,
254
321
  ATTR_MODE => mode
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Zenaton
4
+ module Contexts
5
+ # Represents the current runtime context of a Task.
6
+ #
7
+ # The information provided by the context can be useful to alter the
8
+ # behaviour of the task.
9
+ #
10
+ # For example, you can use the attempt index to know if a task has been
11
+ # automatically retried or not and how many times, and decide to do
12
+ # something when you did not expect the task to be retried more than X
13
+ # times.
14
+ #
15
+ # You can also use the attempt number in the `on_error_retry_delay` method
16
+ # of a task in order to implement complex retry strategies.
17
+ class Task
18
+ # @return [String] The UUID identifying the current task
19
+ attr_reader :id
20
+
21
+ # @return [Integer] The number of times this task has been automatically
22
+ # retried. This counter is reset if you issue a manual retry from your
23
+ # dashboard
24
+ attr_reader :retry_index
25
+
26
+ # @return [Zenaton::Contexts::Task] a new execution context for a task
27
+ def initialize(**kwargs)
28
+ @id = kwargs[:id]
29
+ @retry_index = kwargs[:retry_index]
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Zenaton
4
+ module Contexts
5
+ # Represents the current runtime context of a Workflow.
6
+ class Workflow
7
+ # @return [String] The UUID identifying the current workflow
8
+ attr_reader :id
9
+
10
+ # @return [Zenaton::Contexts::Workflow] a new execution context for a
11
+ # workflow
12
+ def initialize(**kwargs)
13
+ @id = kwargs[:id]
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Zenaton
4
+ # Represents the current runtime context of a Workflow or Task.
5
+ module Contexts
6
+ end
7
+ end
@@ -24,6 +24,22 @@ module Zenaton
24
24
  @processor = nil
25
25
  end
26
26
 
27
+ # Executes scheduling jobs synchronously
28
+ # @param jobs [Array<Zenaton::Interfaces::Job>]
29
+ # @param cron String
30
+ # @return [Array<String>, nil] the results if executed locally, or nil
31
+ def schedule(jobs, cron)
32
+ jobs.map(&method(:check_argument))
33
+ jobs.map do |job|
34
+ if job.is_a? Interfaces::Workflow
35
+ @client.start_scheduled_workflow(job, cron)
36
+ else
37
+ @client.start_scheduled_task(job, cron)
38
+ end
39
+ end
40
+ nil
41
+ end
42
+
27
43
  # Executes jobs synchronously
28
44
  # @param jobs [Array<Zenaton::Interfaces::Job>]
29
45
  # @return [Array<String>, nil] the results if executed locally, or nil
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'zenaton/interfaces/job'
4
+ require 'zenaton/contexts/task'
4
5
 
5
6
  module Zenaton
6
7
  module Interfaces
@@ -9,7 +10,35 @@ module Zenaton
9
10
  # Child classes should implement the handle method
10
11
  def handle
11
12
  raise NotImplemented,
12
- "Your workflow does not implement the `handle' method"
13
+ "Your workflow does not implement the `#handle' method"
14
+ end
15
+
16
+ # (Optional) Implement this method for automatic retrial of task in
17
+ # case of failures.
18
+ # @param _exception [Exception] the reason for the task failure.
19
+ # @return [Integer, FalseClass, NilClass] the non-negative amount of
20
+ # seconds to wait before automatically retrying this task. Falsy values
21
+ # will avoid retrial. Other values will cause the retrial to fail.
22
+ def on_error_retry_delay(_exception)
23
+ nil
24
+ end
25
+
26
+ # @return [Zenaton::Contexts::Task] the task execution context
27
+ def context
28
+ @context || Contexts::Task.new
29
+ end
30
+
31
+ # @private
32
+ # Sets a new context if none has been set yet.
33
+ # This is called from the zenaton agent and will raise if called twice.
34
+ # @raise [ArgumentError] when the context was already set.
35
+ def add_context(**attributes)
36
+ message = <<~ERROR
37
+ Context has already been set and cannot be mutated.
38
+ ERROR
39
+ raise ArgumentError, message if @context
40
+
41
+ @context = Contexts::Task.new(attributes)
13
42
  end
14
43
  end
15
44
  end
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'zenaton/interfaces/job'
4
+ require 'zenaton/contexts/workflow'
4
5
 
5
6
  module Zenaton
6
7
  module Interfaces
@@ -18,6 +19,20 @@ module Zenaton
18
19
  def id
19
20
  nil
20
21
  end
22
+
23
+ # @return [Zenaton::Contexts::Workflow] the workflow execution context
24
+ def context
25
+ @context || Contexts::Workflow.new
26
+ end
27
+
28
+ # @private
29
+ # Sets a new context if none has been set yet.
30
+ # This is called from the zenaton agent and will raise if called twice.
31
+ # @raise [ArgumentError] when the context was already set.
32
+ def add_context(**attributes)
33
+ raise ArgumentError if @context
34
+ @context = Contexts::Workflow.new(attributes)
35
+ end
21
36
  end
22
37
  end
23
38
  end