Skip to content

Commit

Permalink
Extract the world configuration outside of the world
Browse files Browse the repository at this point in the history
This allows us to customize the dynflow world from Foreman/other
plugins. Also, not we don't have to create custom world class for us,
and the DynflowConfiguration class should be more explanatory, than
just passing a hash.

Also, it's worth considering adopting this approach of world
configuration in Dynflow core: configuration object producing the
world instances with given configuration. We could get rid of the
Dynflow::SimpleWorld as well, and just have default configuration
implementation.
  • Loading branch information
iNecas committed Jan 3, 2014
1 parent 738a3f1 commit 6545881
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 23 deletions.
24 changes: 22 additions & 2 deletions lib/foreman_tasks.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'foreman_tasks/version'
require 'foreman_tasks/engine'
require 'foreman_tasks/world'
require 'foreman_tasks/dynflow_configuration'
require 'foreman_tasks/dynflow_persistence'

module ForemanTasks
Expand All @@ -10,7 +10,27 @@ def self.dynflow_initialized?
end

def self.dynflow_initialize
@world ||= ForemanTasks::World.new
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
Expand Down
78 changes: 78 additions & 0 deletions lib/foreman_tasks/dynflow_configuration.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
module ForemanTasks
class DynflowConfiguration

# for logging action related info (such as exceptions raised in side
# the actions' methods
attr_accessor :action_logger

# for logging dynflow related info about the progress of the execution etc.
attr_accessor :dynflow_logger

# the number of threads in the pool handling the execution
attr_accessor :pool_size

# set true if the executor runs externally
attr_accessor :remote
alias_method :remote?, :remote

# if remote set to true, use this path for socket communication
# between this process and the external executor
attr_accessor :remote_socket_path

# what persistence adapater should be used, by default, it uses Sequlel
# adapter based on Rails app database.yml configuration
attr_accessor :persistence_adapter

# what transaction adapater should be used, by default, it uses the ActiveRecord
# based adapter, expecting ActiveRecord is used as ORM in the application
attr_accessor :transaction_adapter

def initialize
self.action_logger = Rails.logger
self.dynflow_logger = Rails.logger
self.pool_size = 5
self.remote = false
self.remote_socket_path = File.join(Rails.root, "tmp", "dynflow_socket")
self.persistence_adapter = default_persistence_adapter
self.transaction_adapter = Dynflow::TransactionAdapters::ActiveRecord.new
end

def initialize_world(world_class = Dynflow::World)
world_class.new(world_options) do |world|
{ executor: self.initialize_executor(world) }
end
end

protected

# generates the options hash consumable by the Dynflow's world
def world_options
{ logger_adapter: Dynflow::LoggerAdapters::Delegator.new(action_logger, dynflow_logger),
executor_class: Dynflow::Executors::Parallel, # TODO configurable Parallel or Remote
pool_size: 5,
persistence_adapter: persistence_adapter,
transaction_adapter: transaction_adapter }
end


def default_persistence_adapter
ForemanTasks::DynflowPersistence.new(default_sequel_adatper_options)
end

def default_sequel_adatper_options
db_config = ActiveRecord::Base.configurations[Rails.env].dup
db_config['adapter'] = 'postgres' if db_config['adapter'] == 'postgresql'
db_config['adapter'] = 'sqlite' if db_config['adapter'] == 'sqlite3'
return db_config
end

def initialize_executor(world)
if self.remote?
Dynflow::Executors::RemoteViaSocket.new(world, self.remote_socket_path)
else
Dynflow::Executors::Parallel.new(world, self.pool_size)
end
end

end
end
21 changes: 0 additions & 21 deletions lib/foreman_tasks/world.rb

This file was deleted.

0 comments on commit 6545881

Please sign in to comment.