|
6 | 6 | # |
7 | 7 | # See: https://github.com/Dynflow/dynflow/pull/446 |
8 | 8 |
|
9 | | -# Defer extension until after ForemanTasks module is loaded |
10 | | -Rails.application.config.to_prepare do |
11 | | - module ForemanTasks |
12 | | - # Chain execution plans so that a new plan waits until prerequisite plans finish before executing. |
13 | | - # This is useful for coordinating dependent tasks where one task should only run after |
14 | | - # other tasks have completed successfully. |
15 | | - # |
16 | | - # The chained plan will remain in 'scheduled' state until all prerequisite plans |
17 | | - # reach 'stopped' state (regardless of success/failure). |
18 | | - # |
19 | | - # @param plan_uuids [String, Array<String>] UUID(s) of prerequisite execution plan(s) |
20 | | - # @param action [Class] Action class to execute |
21 | | - # @param args Arguments to pass to the action |
22 | | - # @return [ForemanTasks::Task::DynflowTask] The chained task that will wait for prerequisites |
23 | | - # |
24 | | - # @example Chain a task to wait for another task |
25 | | - # task1 = ForemanTasks.async_task(SomeAction) |
26 | | - # task2 = ForemanTasks.chain(task1.external_id, AnotherAction, arg1, arg2) |
27 | | - # # task2 will only execute after task1 completes |
28 | | - # |
29 | | - # @example Chain a task to wait for multiple tasks |
30 | | - # task1 = ForemanTasks.async_task(Action1) |
31 | | - # task2 = ForemanTasks.async_task(Action2) |
32 | | - # task3 = ForemanTasks.chain([task1.external_id, task2.external_id], Action3) |
33 | | - # # task3 will only execute after both task1 and task2 complete |
34 | | - def self.chain(plan_uuids, action, *args) |
35 | | - result = dynflow.world.chain(plan_uuids, action, *args) |
36 | | - # The ForemanTasks record may not exist yet for delayed plans, |
37 | | - # so we need to find or create it and properly initialize it |
38 | | - ForemanTasks::Task.find_by(:external_id => result.id) || |
39 | | - begin |
40 | | - delayed_plan = dynflow.world.persistence.load_delayed_plan(result.id) |
41 | | - execution_plan = dynflow.world.persistence.load_execution_plan(result.id) |
42 | | - ForemanTasks::Task::DynflowTask.new_for_execution_plan(execution_plan).tap do |task| |
43 | | - task.update_from_dynflow(execution_plan, delayed_plan) |
44 | | - end |
| 9 | +module ForemanTasksChaining |
| 10 | + # Chain execution plans so that a new plan waits until prerequisite plans finish before executing. |
| 11 | + # This is useful for coordinating dependent tasks where one task should only run after |
| 12 | + # other tasks have completed successfully. |
| 13 | + # |
| 14 | + # The chained plan will remain in 'scheduled' state until all prerequisite plans |
| 15 | + # reach 'stopped' state (regardless of success/failure). |
| 16 | + # |
| 17 | + # @param plan_uuids [String, Array<String>] UUID(s) of prerequisite execution plan(s) |
| 18 | + # @param action [Class] Action class to execute |
| 19 | + # @param args Arguments to pass to the action |
| 20 | + # @return [ForemanTasks::Task::DynflowTask] The chained task that will wait for prerequisites |
| 21 | + # |
| 22 | + # @example Chain a task to wait for another task |
| 23 | + # task1 = ForemanTasks.async_task(SomeAction) |
| 24 | + # task2 = ForemanTasks.chain(task1.external_id, AnotherAction, arg1, arg2) |
| 25 | + # # task2 will only execute after task1 completes |
| 26 | + # |
| 27 | + # @example Chain a task to wait for multiple tasks |
| 28 | + # task1 = ForemanTasks.async_task(Action1) |
| 29 | + # task2 = ForemanTasks.async_task(Action2) |
| 30 | + # task3 = ForemanTasks.chain([task1.external_id, task2.external_id], Action3) |
| 31 | + # # task3 will only execute after both task1 and task2 complete |
| 32 | + def chain(plan_uuids, action, *args) |
| 33 | + result = dynflow.world.chain(plan_uuids, action, *args) |
| 34 | + # The ForemanTasks record may not exist yet for delayed plans, |
| 35 | + # so we need to find or create it and properly initialize it |
| 36 | + ForemanTasks::Task.find_by(:external_id => result.id) || |
| 37 | + begin |
| 38 | + delayed_plan = dynflow.world.persistence.load_delayed_plan(result.id) |
| 39 | + execution_plan = dynflow.world.persistence.load_execution_plan(result.id) |
| 40 | + ForemanTasks::Task::DynflowTask.new_for_execution_plan(execution_plan).tap do |task| |
| 41 | + task.update_from_dynflow(execution_plan, delayed_plan) |
45 | 42 | end |
46 | | - end |
| 43 | + end |
47 | 44 | end |
48 | 45 | end |
| 46 | + |
| 47 | +# Defer extension until after ForemanTasks module is loaded |
| 48 | +Rails.application.config.to_prepare do |
| 49 | + ForemanTasks.singleton_class.prepend(ForemanTasksChaining) |
| 50 | +end |
0 commit comments