Class: Jackal::Loader

Inherits:
Object
  • Object
show all
Defined in:
lib/jackal/loader.rb

Class Method Summary collapse

Class Method Details

.configure!(opts) ⇒ TrueClass

Configure the application

Parameters:

  • opts (Hash)

Returns:

  • (TrueClass)


12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/jackal/loader.rb', line 12

def configure!(opts)
  default_verbosity = nil
  if(ENV['JACKAL_TESTING_MODE'])
    if(!opts[:config])
      Carnivore.configure!(:verify)
    else
      Carnivore.configure!(opts[:config], :force)
    end
    default_verbosity = :fatal
  else
    Carnivore.configure!(opts[:config])
    Carnivore::Config.immutable!
    default_verbosity = :info
  end

  default_verbosity = :debug if ENV['DEBUG']
  const = opts.fetch(:verbosity,
    Carnivore::Config.fetch(:verbosity, default_verbosity)
  ).to_s.upcase

  Zoidberg.logger.level = Zoidberg.logger.class.const_get(const)
  true
end

.process_opts(opts) ⇒ Smash

Scrub and type opts

Parameters:

  • opts (Slop, Hash)

Returns:

  • (Smash)


40
41
42
43
44
# File 'lib/jackal/loader.rb', line 40

def process_opts(opts)
  opts = opts.to_hash.to_smash
  opts.delete_if{|k,v| v.nil?}
  opts
end

.run!(opts) ⇒ Object

Run the jackal

Parameters:

  • opts (Hash)


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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/jackal/loader.rb', line 49

def run!(opts)
  opts = process_opts(opts)
  configure!(opts)

  Carnivore::Config.fetch(:jackal, :require, []).each do |path|
    require path
  end

  begin
    Carnivore::Config.to_smash.each do |namespace, args|
      next unless args.is_a?(Hash)
      args.each do |key, opts|
        next unless opts.is_a?(Hash) && opts[:sources]
        Carnivore::Utils.debug "Processing: #{opts.inspect}"
        Carnivore.configure do
          opts.fetch(:sources, {}).each do |kind, source_args|
            source = Carnivore::Source.build(
              :type => source_args[:type].to_sym,
              :args => source_args.fetch(:args, {}).merge(
                :name => "#{namespace}_#{key}_#{kind}",
                :orphan_callback => lambda{|message|
                  warn "No callbacks matched message. Auto confirming message from source. (#{message})"
                  message.confirm!
                  if(name.to_s.end_with?('input'))
                    destination = Carnivore::Supervisor.supervisor[name.to_s.sub('_input', '_output')]
                    if(destination)
                      warn "Auto pushing orphaned message to next destination (#{message} -> #{destination.name})"
                      begin
                        destination.transmit(Utils.unpack(message))
                      rescue => e
                        error "Failed to auto push message (#{message}): #{e.class} - #{e}"
                      end
                    else
                      warn "Failed to location destination for message forward! (Destination: #{destination} #{message})"
                    end
                  else
                    error "Cannot auto forward from output source. No messages should be encountered here! (#{message})"
                  end
                }
              )
            )
            Carnivore::Utils.info "Registered new source: #{namespace}_#{key}_#{kind}"
            if(kind.to_s == 'input')
              opts.fetch(:callbacks, []).each do |klass_name|
                klass = Utils.constantize(klass_name)
                source.add_callback(klass_name, klass)
              end
            end
          end
        end
      end
    end
    if(events_opts = Carnivore::Config.get(:jackal, :events))
      Carnivore.configure do
        Carnivore::Source.build(
          :type => events_opts[:type].to_sym,
          :args => events_opts.fetch(:args, {}).merge(
            :name => :events
          )
        )
      end
    end
    Jackal::Utils.load_http_hook
    Carnivore.start!
  rescue => e
    $stderr.puts "Unexpected failure encountered: #{e.class}: #{e}"
    if(ENV['DEBUG'])
      $stderr.puts "#{e.class}: #{e}\n#{e.backtrace.join("\n")}"
    end
    exit -1
  end

end