This repository has been archived by the owner on Jul 28, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add execution context for tasks and workflows (#79)
* Add execution context for tasks and workflows * Rename attempt_index to retry_index * Make sure json is required
- Loading branch information
Showing
12 changed files
with
314 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# frozen_string_literal: true | ||
|
||
module Zenaton | ||
# Represents the current runtime context of a Workflow or Task. | ||
module Contexts | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# frozen_string_literal: true | ||
|
||
module Zenaton | ||
module Contexts | ||
# Represents the current runtime context of a Task. | ||
# | ||
# The information provided by the context can be useful to alter the | ||
# behaviour of the task. | ||
# | ||
# For example, you can use the attempt index to know if a task has been | ||
# automatically retried or not and how many times, and decide to do | ||
# something when you did not expect the task to be retried more than X | ||
# times. | ||
# | ||
# You can also use the attempt number in the `on_error_retry_delay` method | ||
# of a task in order to implement complex retry strategies. | ||
class Task | ||
# @return [String] The UUID identifying the current task | ||
attr_reader :id | ||
|
||
# @return [Integer] The number of times this task has been automatically | ||
# retried. This counter is reset if you issue a manual retry from your | ||
# dashboard | ||
attr_reader :retry_index | ||
|
||
# @return [Zenaton::Contexts::Task] a new execution context for a task | ||
def initialize(**kwargs) | ||
@id = kwargs[:id] | ||
@retry_index = kwargs[:retry_index] | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# frozen_string_literal: true | ||
|
||
module Zenaton | ||
module Contexts | ||
# Represents the current runtime context of a Workflow. | ||
class Workflow | ||
# @return [String] The UUID identifying the current workflow | ||
attr_reader :id | ||
|
||
# @return [Zenaton::Contexts::Workflow] a new execution context for a | ||
# workflow | ||
def initialize(**kwargs) | ||
@id = kwargs[:id] | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
|
||
require 'json' | ||
require 'zenaton/services/properties' | ||
require 'json' | ||
|
||
module Zenaton | ||
module Services | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'zenaton/contexts/task' | ||
|
||
RSpec.describe Zenaton::Contexts::Task do | ||
describe 'initialization' do | ||
subject(:task_context) { described_class.new(**args) } | ||
|
||
context 'when nothing is passed' do | ||
let(:args) { {} } | ||
|
||
it 'has no id' do | ||
expect(task_context.id).to be_nil | ||
end | ||
|
||
it 'has no attempt index' do | ||
expect(task_context.retry_index).to be_nil | ||
end | ||
end | ||
|
||
context 'when id and attempt index are provided' do | ||
let(:args) { { id: 'some-uuid', retry_index: 10 } } | ||
|
||
it 'sets the id' do | ||
expect(task_context.id).to eq('some-uuid') | ||
end | ||
|
||
it 'sets the attempt index' do | ||
expect(task_context.retry_index).to eq(10) | ||
end | ||
end | ||
|
||
context 'when other arguments are passed' do | ||
let(:args) { { some: 'invalid', extra: 'attributes' } } | ||
|
||
it 'has no id' do | ||
expect(task_context.id).to be_nil | ||
end | ||
|
||
it 'has no attempt index' do | ||
expect(task_context.retry_index).to be_nil | ||
end | ||
|
||
it 'does not set getter methods' do | ||
expect { task_context.some }.to raise_error NoMethodError | ||
end | ||
|
||
it 'does not set the extra attributes' do | ||
expect(task_context.instance_variables).to eq(%i[@id @retry_index]) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'zenaton/contexts/workflow' | ||
|
||
RSpec.describe Zenaton::Contexts::Workflow do | ||
describe 'initialization' do | ||
subject(:flow_context) { described_class.new(**args) } | ||
|
||
context 'when nothing is passed' do | ||
let(:args) { {} } | ||
|
||
it 'has no id' do | ||
expect(flow_context.id).to be_nil | ||
end | ||
end | ||
|
||
context 'when id is provided' do | ||
let(:args) { { id: 'some-uuid' } } | ||
|
||
it 'sets the id' do | ||
expect(flow_context.id).to eq('some-uuid') | ||
end | ||
end | ||
|
||
context 'when other arguments are passed' do | ||
let(:args) { { some: 'invalid', extra: 'attributes' } } | ||
|
||
it 'has no id' do | ||
expect(flow_context.id).to be_nil | ||
end | ||
|
||
it 'does not set getter methods' do | ||
expect { flow_context.some }.to raise_error NoMethodError | ||
end | ||
|
||
it 'does not set the extra attributes' do | ||
expect(flow_context.instance_variables).to eq([:@id]) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters