forked from theforeman/foreman-tasks
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Better organization of dynflow-related stuff
Besides better structure, it also makes sure the world initialization happens after all engines were able to influence the dynflow configuration. From now on, to specify that another engine requires dynflow runtime, it should call `ForemanTasks.dyntask.require!` instead of `ForemanTasks.dyntask.initialize`.
- Loading branch information
Showing
8 changed files
with
100 additions
and
77 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
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 |
---|---|---|
@@ -1,67 +1,20 @@ | ||
require 'foreman_tasks/version' | ||
require 'foreman_tasks/engine' | ||
require 'foreman_tasks/dynflow_configuration' | ||
require 'foreman_tasks/dynflow_persistence' | ||
require 'foreman_tasks/dynflow' | ||
|
||
module ForemanTasks | ||
|
||
def self.dynflow_initialized? | ||
!@world.nil? | ||
end | ||
|
||
def self.dynflow_initialize | ||
return @world if @world | ||
dynflow_configuration.initialize_world.tap do |world| | ||
@world = world | ||
|
||
ActionDispatch::Reloader.to_prepare do | ||
ForemanTasks.eager_load! | ||
world.reload! | ||
end | ||
|
||
at_exit { world.terminate.wait } | ||
|
||
# for now, we can check the consistency only when there | ||
# is no remote executor. We should be able to check the consistency | ||
# every time the new world is created when there is a register | ||
# of executors | ||
world.consistency_check unless dynflow_configuration.remote? | ||
end | ||
end | ||
|
||
def self.dynflow_configuration | ||
@configuration ||= ForemanTasks::DynflowConfiguration.new | ||
end | ||
|
||
def self.world | ||
if @world | ||
return @world | ||
else | ||
raise "The Dynflow world was not initialized yet. "\ | ||
"If your plugin uses it, make sure to call ForemanTasks.dynflow_initialize "\ | ||
"in after_initialize block" | ||
end | ||
def self.dynflow | ||
@dynflow ||= ForemanTasks::Dynflow.new | ||
end | ||
|
||
def self.trigger(action, *args, &block) | ||
world.trigger action, *args, &block | ||
dynflow.world.trigger action, *args, &block | ||
end | ||
|
||
def self.async_task(action, *args, &block) | ||
run = trigger(action, *args, &block) | ||
ForemanTasks::Task::DynflowTask.find_by_external_id(run.id) | ||
end | ||
|
||
def self.eager_load_paths | ||
@eager_load_paths ||= [] | ||
end | ||
|
||
def self.eager_load! | ||
eager_load_paths.each do |load_path| | ||
# todo: does the reloading work now?x | ||
Dir.glob("#{load_path}/**/*.rb").sort.each do |file| | ||
require_dependency file | ||
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,73 @@ | ||
module ForemanTasks | ||
# Class for configuring and preparing the Dynflow runtime environment. | ||
class Dynflow | ||
require 'foreman_tasks/dynflow/configuration' | ||
require 'foreman_tasks/dynflow/persistence' | ||
|
||
attr_reader :config | ||
|
||
def initialize | ||
@config = ForemanTasks::Dynflow::Configuration.new | ||
@required = false | ||
end | ||
|
||
# call this method if your engine uses Dynflow | ||
def require! | ||
@required = true | ||
end | ||
|
||
def initialized? | ||
!@world.nil? | ||
end | ||
|
||
def initialize! | ||
return unless @required | ||
return @world if @world | ||
config.initialize_world.tap do |world| | ||
@world = world | ||
|
||
ActionDispatch::Reloader.to_prepare do | ||
ForemanTasks.dynflow.eager_load_actions! | ||
world.reload! | ||
end | ||
|
||
at_exit { world.terminate.wait } | ||
|
||
# for now, we can check the consistency only when there | ||
# is no remote executor. We should be able to check the consistency | ||
# every time the new world is created when there is a register | ||
# of executors | ||
world.consistency_check unless config.remote? | ||
end | ||
end | ||
|
||
def world | ||
if @world | ||
return @world | ||
else | ||
raise "The Dynflow world was not initialized yet. "\ | ||
"If your plugin uses it, make sure to call ForemanTasks.dynflow.require! "\ | ||
"in some initializer" | ||
end | ||
end | ||
|
||
def web_console | ||
::Dynflow::WebConsole.setup do | ||
before do | ||
# TODO: propper authentication | ||
User.current = User.first | ||
end | ||
|
||
set(:world) { ForemanTasks.dynflow.world } | ||
end | ||
end | ||
|
||
def eager_load_actions! | ||
config.eager_load_paths.each do |load_path| | ||
Dir.glob("#{load_path}/**/*.rb").sort.each do |file| | ||
require_dependency file | ||
end | ||
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