Class: Jackal::Callback
- Inherits:
-
Carnivore::Callback
- Object
- Carnivore::Callback
- Jackal::Callback
- Includes:
- Bogo::Constants, Bogo::Memoization, Utils::Config, Utils::Events, Utils::Payload
- Defined in:
- lib/jackal/callback.rb
Overview
Jackal customized callback
Instance Attribute Summary collapse
-
#formatters ⇒ Array<Formatter>
readonly
Formatters applied on complete.
-
#pre_formatters ⇒ Array<Formatter>
readonly
Formatters applied prior.
Instance Method Summary collapse
-
#apply_formatters!(payload) ⇒ Smash
Apply configured formatters to payload.
- #asset_store ⇒ Jackal::Assets::Store
-
#completed(payload, message) ⇒ Object
Mark payload complete and forward.
-
#failed(payload, message, reason = 'No reason provided') ⇒ Object
Send payload to error handler.
-
#failure_wrap(message) ⇒ Object
Executes block and catches unexpected exceptions if encountered.
-
#forward(payload, dest = nil) ⇒ Object
Forward payload to output source.
-
#initialize(*_) ⇒ self
constructor
Create new instance.
-
#job_completed(name, payload, message) ⇒ Object
Mark job as completed.
- #process_manager ⇒ Utils::Process
-
#setup_formatters ⇒ TrueClass, FalseClass
Initialize any required formatters.
-
#valid?(message) ⇒ TrueClass, FalseClass
Validity of message.
Methods included from Utils::Events
Methods included from Utils::Config
#app_config, #app_host, #config, #config_path, #destination, extended, included, #service_config, #service_name, #source_prefix
Methods included from Utils::Payload
Constructor Details
#initialize(*_) ⇒ self
Create new instance
26 27 28 29 30 31 |
# File 'lib/jackal/callback.rb', line 26 def initialize(*_) super if(service_config[:formatters]) setup_formatters end end |
Instance Attribute Details
#formatters ⇒ Array<Formatter> (readonly)
Returns formatters applied on complete.
19 20 21 |
# File 'lib/jackal/callback.rb', line 19 def formatters @formatters end |
#pre_formatters ⇒ Array<Formatter> (readonly)
Returns formatters applied prior.
21 22 23 |
# File 'lib/jackal/callback.rb', line 21 def pre_formatters @pre_formatters end |
Instance Method Details
#apply_formatters!(payload) ⇒ Smash
Apply configured formatters to payload
171 172 173 174 175 176 177 178 179 |
# File 'lib/jackal/callback.rb', line 171 def apply_formatters!(payload) formatters.each do |formatter| begin formatter.format(payload) rescue => e error "Formatter error encountered (<#{formatter}>): #{e.class} - #{e}" end end end |
#asset_store ⇒ Jackal::Assets::Store
the assets library is NOT a dependency of jackal and must be included at runtime!
60 61 62 63 64 65 |
# File 'lib/jackal/callback.rb', line 60 def asset_store memoize(:asset_store) do require 'jackal-assets' Jackal::Assets::Store.new end end |
#completed(payload, message) ⇒ Object
Mark payload complete and forward
129 130 131 132 133 |
# File 'lib/jackal/callback.rb', line 129 def completed(payload, ) .confirm! info "Processing of #{} complete on this callback" forward(payload, source.name) end |
#failed(payload, message, reason = 'No reason provided') ⇒ Object
Send payload to error handler
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/jackal/callback.rb', line 105 def failed(payload, , reason='No reason provided') error "Processing of #{} failed! Reason: #{reason}" unless(payload[:error]) payload.set(:error, reason) end .confirm! dest = destination(:error, payload) source = Carnivore::Supervisor.supervisor[dest] if(source) if(formatters) apply_formatters!(payload) end error "Sending #{} to error handler: #{source}" source.transmit(payload) else error "No error source found for generated source path: #{dest}" info "Processing of message #{} has completed. Message now discarded." end end |
#failure_wrap(message) ⇒ Object
Executes block and catches unexpected exceptions if encountered
87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/jackal/callback.rb', line 87 def failure_wrap() abort 'Failure wrap requires block for execution' unless block_given? begin payload = unpack() yield payload rescue => e error "!!! Unexpected failure encountered -> #{e.class}: #{e}" debug "#{e.class}: #{e}\n#{(e.backtrace || []).join("\n")}" payload.set(:error, "#{e.class}: #{e.}") failed(payload, , e.) end end |
#forward(payload, dest = nil) ⇒ Object
Forward payload to output source
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/jackal/callback.rb', line 138 def forward(payload, dest=nil) unless(dest) dest = destination(:output, payload) end source = Carnivore::Supervisor.supervisor[dest] if(source) if(formatters) apply_formatters!(payload) end info "Forwarding payload to output destination... (#{source})" debug "Forwarded payload: #{payload.pretty_inspect}" source.transmit(payload) else warn "No destination source found for generated source path: #{dest}" info "Processing of message has completed. Message now discarded." end end |
#job_completed(name, payload, message) ⇒ Object
Mark job as completed
161 162 163 164 165 |
# File 'lib/jackal/callback.rb', line 161 def job_completed(name, payload, ) info "Processing of message #{} has completed within this component #{name}" .confirm! forward(payload) end |
#process_manager ⇒ Utils::Process
68 69 70 71 72 |
# File 'lib/jackal/callback.rb', line 68 def process_manager memoize(:process_manager) do Utils::Process.new(app_config.fetch(:process_manager, Smash.new)) end end |
#setup_formatters ⇒ TrueClass, FalseClass
Initialize any required formatters
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/jackal/callback.rb', line 36 def setup_formatters f_config = service_config[:formatters] case f_config when Hash @formatters = f_config.fetch(:pre, []).map do |klass_name| constantize(klass_name).new(self) end @pre_formatters = f_config.fetch(:post, []).map do |klass_name| constantize(klass_name).new(self) end when Array @formatters = f_config.map do |klass_name| constantize(klass_name).new(self) end @pre_formatters = [] else error "Formatters configuration error. Unable to process type `#{f_config.class}`." false end end |
#valid?(message) ⇒ TrueClass, FalseClass
Validity of message
78 79 80 81 |
# File 'lib/jackal/callback.rb', line 78 def valid?() m = unpack() block_given? ? yield(m) : true end |