Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix not executing multiple fork tasks #64

Merged
merged 1 commit into from
May 31, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions lib/autoload/kuroko2/workflow/task/fork.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@ module Workflow
module Task
class Fork < Base
def execute
children = token.children
if children.empty?
if fork_children_ids.empty?
message = "(token #{token.uuid}) Start to fork."

token.job_instance.logs.info(message)
Kuroko2.logger.info(message)
extract_child_nodes

:pass
elsif children.all?(&:finished?)
elsif token.children.where(id: fork_children_ids).all?(&:finished?)
message = "(token #{token.uuid}) All children are finished."

token.job_instance.logs.info(message)
Expand All @@ -32,6 +31,11 @@ def validate

private

def fork_children_ids
token.context['fork_children_ids'] ||= {}
token.context['fork_children_ids'][token.path] ||= []
end

def extract_child_nodes
node.children.each do |child|
create_child_token(child_node: child)
Expand All @@ -44,6 +48,7 @@ def create_child_token(child_node:, env: {})
attributes['context']['ENV'] = (attributes['context']['ENV'] || {}).merge(env)

Token.create!(attributes).tap do |created|
fork_children_ids << created.id
message = "(token #{created.uuid}) New token are created for #{child_node.path}"
created.job_instance.logs.info(message)
Kuroko2.logger.info(message)
Expand Down
67 changes: 58 additions & 9 deletions spec/workflow/task/fork_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@

module Kuroko2::Workflow::Task
describe Fork do
let(:node) { Kuroko2::Workflow::ScriptParser.new(token.script).parse.find(token.path) }
let(:parser) { Kuroko2::Workflow::ScriptParser.new(token.script) }
let(:node) { parser.parse.find(token.path) }
let(:definition) { create(:job_definition) }
let(:instance) { create(:job_instance, job_definition: definition) }

let(:token) do
Kuroko2::Token.create(uuid: SecureRandom.uuid, path: '/0-fork', job_definition: definition, job_instance: instance, script: <<-EOF)
fork:
noop: noop1
noop: noop2
EOF
end

describe '#execute' do
let(:token) do
Kuroko2::Token.create(uuid: SecureRandom.uuid, path: '/0-fork', job_definition: definition, job_instance: instance, script: <<-EOF.strip_heredoc)
fork:
noop: noop1
noop: noop2
EOF
end

subject(:task) { Fork.new(node, token) }
let(:children) { token.children }

Expand All @@ -26,5 +27,53 @@ module Kuroko2::Workflow::Task
expect(children.second.script).to eq "noop: noop2\n"
end
end

context 'There are multiple fork tasks' do
let(:token) do
Kuroko2::Token.create(uuid: SecureRandom.uuid, job_definition: definition, job_instance: instance, script: <<-EOF.strip_heredoc)
noop: noop1
fork:
noop: noop2
noop: noop3
fork:
noop: noop5
noop: noop6
EOF
end

it 'processes each fork tasks' do
expect(node.children.first.path).to eq('/0-noop')
expect(node.children.second.path).to eq('/1-fork')
expect(node.children.second.children.first.path).to eq('/1-fork/0-noop')
expect(node.children.second.children.second.path).to eq('/1-fork/1-noop')
expect(node.children.third.path).to eq('/2-fork')
expect(node.children.third.children.first.path).to eq('/2-fork/0-noop')
expect(node.children.third.children.second.path).to eq('/2-fork/1-noop')

Kuroko2::Workflow::Engine.new.process_all
expect(token.reload.path).to eq('/0-noop')

Kuroko2::Workflow::Engine.new.process_all # create children1
expect(token.reload.path).to eq('/1-fork')
Kuroko2::Workflow::Engine.new.process_all # process children1-1
expect(token.reload.path).to eq('/1-fork')
Kuroko2::Workflow::Engine.new.process_all # process children1-2
expect(token.reload.path).to eq('/1-fork')
Kuroko2::Workflow::Engine.new.process_all # check all children1 finished
expect(token.reload.path).to eq('/1-fork')

Kuroko2::Workflow::Engine.new.process_all # create children2
expect(token.reload.path).to eq('/2-fork')
Kuroko2::Workflow::Engine.new.process_all # process children2-1
expect(token.reload.path).to eq('/2-fork')
Kuroko2::Workflow::Engine.new.process_all # process children2-2
expect(token.reload.path).to eq('/2-fork')
Kuroko2::Workflow::Engine.new.process_all # check all children2 finished
expect(token.reload.path).to eq('/2-fork')

Kuroko2::Workflow::Engine.new.process_all
expect(Kuroko2::Token.where(id: token.id)).not_to exist
end
end
end
end