Skip to content

Commit ca5711e

Browse files
ianballouadamruzicka
authored andcommitted
Refs #38991 - create chain() foreman-tasks method
1 parent 2fdfa0f commit ca5711e

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

lib/foreman_tasks.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,18 @@ def self.delay(action, delay_options, *args)
6262
ForemanTasks::Task::DynflowTask.where(:external_id => result.id).first!
6363
end
6464

65+
# Chain a task to wait for prerequisite task(s) to finish before executing.
66+
# The chained task remains 'scheduled' until all prerequisites reach 'stopped' state.
67+
#
68+
# @param plan_uuids [String, Array<String>] UUID(s) of prerequisite execution plan(s)
69+
# @param action [Class] Action class to execute
70+
# @param args Arguments to pass to the action
71+
# @return [ForemanTasks::Task::DynflowTask] The chained task
72+
def self.chain(plan_uuids, action, *args)
73+
result = dynflow.world.chain(plan_uuids, action, *args)
74+
ForemanTasks::Task::DynflowTask.where(:external_id => result.id).first!
75+
end
76+
6577
def self.register_scheduled_task(task_class, cronline)
6678
ForemanTasks::RecurringLogic.transaction(isolation: :serializable) do
6779
return if ForemanTasks::RecurringLogic.joins(:tasks)

test/unit/chaining_test.rb

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
require 'foreman_tasks_test_helper'
2+
3+
module ForemanTasks
4+
class ChainingTest < ActiveSupport::TestCase
5+
include ForemanTasks::TestHelpers::WithInThreadExecutor
6+
7+
before do
8+
User.current = User.where(:login => 'apiadmin').first
9+
end
10+
11+
it 'creates a scheduled task chained to a prerequisite execution plan' do
12+
prerequisite_plan = ForemanTasks.dynflow.world.plan(Support::DummyDynflowAction)
13+
14+
task = ForemanTasks.chain(prerequisite_plan.id, Support::DummyDynflowAction)
15+
16+
assert_kind_of ForemanTasks::Task::DynflowTask, task
17+
assert_predicate task, :scheduled?
18+
19+
dependencies = ForemanTasks.dynflow.world.persistence.find_execution_plan_dependencies(task.execution_plan.id)
20+
assert_includes dependencies, prerequisite_plan.id
21+
end
22+
23+
it 'accepts multiple prerequisite execution plans' do
24+
prerequisite_plan_1 = ForemanTasks.dynflow.world.plan(Support::DummyDynflowAction)
25+
prerequisite_plan_2 = ForemanTasks.dynflow.world.plan(Support::DummyDynflowAction)
26+
27+
task = ForemanTasks.chain([prerequisite_plan_1.id, prerequisite_plan_2.id], Support::DummyDynflowAction)
28+
29+
dependencies = ForemanTasks.dynflow.world.persistence.find_execution_plan_dependencies(task.execution_plan.id)
30+
assert_includes dependencies, prerequisite_plan_1.id
31+
assert_includes dependencies, prerequisite_plan_2.id
32+
end
33+
end
34+
end
35+

0 commit comments

Comments
 (0)