2021-06-01 12:20:20 +00:00
|
|
|
# Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/
|
|
|
|
|
2017-08-14 11:56:23 +00:00
|
|
|
class Sequencer
|
|
|
|
include ::Mixin::RailsLogger
|
|
|
|
include ::Mixin::StartFinishLogger
|
|
|
|
|
|
|
|
attr_reader :sequence
|
|
|
|
|
|
|
|
# Convenience wrapper for instant processing with the given attributes.
|
|
|
|
#
|
|
|
|
# @example
|
|
|
|
# Sequencer.process('Example::Sequence')
|
|
|
|
#
|
|
|
|
# @example
|
|
|
|
# Sequencer.process('Example::Sequence',
|
|
|
|
# parameters: {
|
|
|
|
# some: 'value',
|
|
|
|
# },
|
|
|
|
# expecting: [:result, :answer]
|
|
|
|
# )
|
|
|
|
#
|
|
|
|
# @return [Hash{Symbol => Object}] the final result state attributes and values
|
2021-07-06 07:52:22 +00:00
|
|
|
def self.process(sequence, **args)
|
|
|
|
new(sequence, **args).process
|
2017-08-14 11:56:23 +00:00
|
|
|
end
|
|
|
|
|
2018-06-22 14:36:20 +00:00
|
|
|
# Provides the log level definition for the requested Sequencer component.
|
|
|
|
#
|
|
|
|
# @example
|
|
|
|
# Sequencer.log_level_for(:state)
|
|
|
|
# #=> { get: :debug, set: :debug, ... }
|
|
|
|
#
|
|
|
|
# @return [ActiveSupport::HashWithIndifferentAcces] the log level definition
|
|
|
|
def self.log_level_for(component)
|
|
|
|
Setting.get('sequencer_log_level').with_indifferent_access[component]
|
|
|
|
end
|
|
|
|
|
2017-08-14 11:56:23 +00:00
|
|
|
# Initializes a new Sequencer instance for the given Sequence with parameters and expecting result.
|
|
|
|
#
|
|
|
|
# @example
|
|
|
|
# Sequencer.new('Example::Sequence')
|
|
|
|
#
|
|
|
|
# @example
|
|
|
|
# Sequencer.new('Example::Sequence',
|
|
|
|
# parameters: {
|
|
|
|
# some: 'value',
|
|
|
|
# },
|
|
|
|
# expecting: [:result, :answer]
|
|
|
|
# )
|
|
|
|
def initialize(sequence, parameters: {}, expecting: nil)
|
|
|
|
@sequence = Sequencer::Sequence.constantize(sequence)
|
|
|
|
@parameters = parameters
|
|
|
|
@expecting = expecting
|
|
|
|
|
|
|
|
# fall back to sequence default expecting if no explicit
|
|
|
|
# expecting was given for this sequence
|
|
|
|
return if !@expecting.nil?
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2017-08-14 11:56:23 +00:00
|
|
|
@expecting = @sequence.expecting
|
|
|
|
end
|
|
|
|
|
|
|
|
# Processes the Sequence the instance was initialized with.
|
|
|
|
#
|
|
|
|
# @example
|
|
|
|
# sequence.process
|
|
|
|
#
|
|
|
|
# @return [Hash{Symbol => Object}] the final result state attributes and values
|
|
|
|
def process
|
2018-06-22 14:36:20 +00:00
|
|
|
log_start_finish(log_level[:start_finish], "Sequence '#{sequence.name}'") do
|
2017-08-14 11:56:23 +00:00
|
|
|
|
|
|
|
sequence.units.each_with_index do |unit, index|
|
|
|
|
|
|
|
|
state.process do
|
|
|
|
|
2018-06-22 14:36:20 +00:00
|
|
|
log_start_finish(log_level[:unit], "Sequence '#{sequence.name}' Unit '#{unit.name}' (index: #{index})") do
|
2017-08-14 11:56:23 +00:00
|
|
|
unit.process(state)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
state.to_h.tap do |result|
|
2018-06-22 14:36:20 +00:00
|
|
|
logger.public_send(log_level[:result]) { "Returning Sequence '#{sequence.name}' result: #{result.inspect}" }
|
2017-08-14 11:56:23 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def state
|
|
|
|
@state ||= Sequencer::State.new(sequence,
|
|
|
|
parameters: @parameters,
|
|
|
|
expecting: @expecting)
|
|
|
|
end
|
2018-06-22 14:36:20 +00:00
|
|
|
|
|
|
|
def log_level
|
|
|
|
@log_level ||= self.class.log_level_for(:sequence)
|
|
|
|
end
|
2017-08-14 11:56:23 +00:00
|
|
|
end
|