forked from zammad/zammad
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsequencer.rb
98 lines (83 loc) · 2.63 KB
/
sequencer.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
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
def self.process(sequence, **args)
new(sequence, **args).process
end
# 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
# 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 [email protected]?
@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
log_start_finish(log_level[:start_finish], "Sequence '#{sequence.name}'") do
sequence.units.each_with_index do |unit, index|
state.process do
log_start_finish(log_level[:unit], "Sequence '#{sequence.name}' Unit '#{unit.name}' (index: #{index})") do
unit.process(state)
end
end
end
end
state.to_h.tap do |result|
logger.public_send(log_level[:result]) { "Returning Sequence '#{sequence.name}' result: #{result.inspect}" }
end
end
private
def state
@state ||= Sequencer::State.new(sequence,
parameters: @parameters,
expecting: @expecting)
end
def log_level
@log_level ||= self.class.log_level_for(:sequence)
end
end