Class: Jackal::Callback

Inherits:
Carnivore::Callback
  • Object
show all
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

Instance Method Summary collapse

Methods included from Utils::Events

#event!

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

#new_payload, #unpack

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

#formattersArray<Formatter> (readonly)

Returns formatters applied on complete.

Returns:

  • (Array<Formatter>)

    formatters applied on complete



19
20
21
# File 'lib/jackal/callback.rb', line 19

def formatters
  @formatters
end

#pre_formattersArray<Formatter> (readonly)

Returns formatters applied prior.

Returns:

  • (Array<Formatter>)

    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

Parameters:

  • payload (Smash)

Returns:

  • (Smash)


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_storeJackal::Assets::Store

Note:

the assets library is NOT a dependency of jackal and must be included at runtime!

Returns:

  • (Jackal::Assets::Store)


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

Parameters:

  • payload (Hash)
  • message (Carnivore::Message)


129
130
131
132
133
# File 'lib/jackal/callback.rb', line 129

def completed(payload, message)
  message.confirm!
  info "Processing of #{message} complete on this callback"
  forward(payload, source.name)
end

#failed(payload, message, reason = 'No reason provided') ⇒ Object

Send payload to error handler

Parameters:

  • payload (Hash)
  • message (Carnivore::Message)
  • reason (String) (defaults to: 'No reason provided')


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, message, reason='No reason provided')
  error "Processing of #{message} failed! Reason: #{reason}"
  unless(payload[:error])
    payload.set(:error, reason)
  end
  message.confirm!
  dest = destination(:error, payload)
  source = Carnivore::Supervisor.supervisor[dest]
  if(source)
    if(formatters)
      apply_formatters!(payload)
    end
    error "Sending #{message} to error handler: #{source}"
    source.transmit(payload)
  else
    error "No error source found for generated source path: #{dest}"
    info "Processing of message #{message} has completed. Message now discarded."
  end
end

#failure_wrap(message) ⇒ Object

Executes block and catches unexpected exceptions if encountered

Parameters:

  • message (Carnivore::Message)

Returns:

  • (Object)


87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/jackal/callback.rb', line 87

def failure_wrap(message)
  abort 'Failure wrap requires block for execution' unless block_given?
  begin
    payload = unpack(message)
    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.message}")
    failed(payload, message, e.message)
  end
end

#forward(payload, dest = nil) ⇒ Object

Forward payload to output source

Parameters:

  • payload (Hash)


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

Parameters:

  • name (String)
  • payload (Hash)
  • message (Carnivore::Message)


161
162
163
164
165
# File 'lib/jackal/callback.rb', line 161

def job_completed(name, payload, message)
  info "Processing of message #{message} has completed within this component #{name}"
  message.confirm!
  forward(payload)
end

#process_managerUtils::Process

Returns:



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_formattersTrueClass, FalseClass

Initialize any required formatters

Returns:

  • (TrueClass, FalseClass)


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

Parameters:

  • message (Carnivore::Message)

Returns:

  • (TrueClass, FalseClass)


78
79
80
81
# File 'lib/jackal/callback.rb', line 78

def valid?(message)
  m = unpack(message)
  block_given? ? yield(m) : true
end