Class: Carnivore::Callback

Inherits:
Object
  • Object
show all
Includes:
Utils::Logging, Celluloid
Defined in:
lib/carnivore/callback.rb

Overview

Payload modifier

Class Attribute Summary (collapse)

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Callback) initialize(name, source, block = nil)

Creates a new callback. Optional block to define callback behavior must be passed as a `Proc` instance, not a block.

Parameters:

  • name (String, Symbol)

    name of the callback

  • block (Proc) (defaults to: nil)

    optionally define the callback behavior



26
27
28
29
30
31
32
33
34
# File 'lib/carnivore/callback.rb', line 26

def initialize(name, source, block=nil)
  @name = name
  @source = source
  if(block.nil? && self.class == Callback)
    raise ArgumentError.new 'Block is required for dynamic callbacks!'
  end
  define_singleton_method(:execute, &block) if block
  setup
end

Class Attribute Details

+ (Object) workers

Define number of workers to create



9
10
11
# File 'lib/carnivore/callback.rb', line 9

def workers
  @workers
end

Instance Attribute Details

- (String, Symbol) name (readonly)

Returns name of callback

Returns:

  • (String, Symbol)

    name of callback



17
18
19
# File 'lib/carnivore/callback.rb', line 17

def name
  @name
end

- (Carnivore::Source) source (readonly)

Returns source this callback is attached

Returns:



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

def source
  @source
end

Instance Method Details

- (Object) call(message)

Execute callback against given message

Parameters:



60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/carnivore/callback.rb', line 60

def call(message)
  begin
    if(valid?(message))
      debug ">> Received message is valid for this callback (#{message})"
      execute(message)
    else
      debug "Invalid message for this callback #{message})"
    end
  rescue => e
    error "[callback: #{self}, source: #{message[:source]}, message: #{message[:message].object_id}]: #{e.class} - #{e}"
    debug "#{e.class}: #{e}\n#{e.backtrace.join("\n")}"
    nil
  end
end

- (String) inspect Also known as: to_s

Provide nice output when printed

Returns:

  • (String)


44
45
46
# File 'lib/carnivore/callback.rb', line 44

def inspect
  "callback<#{self.name}:#{self.object_id}>"
end

- (NilClass) log(*args) Originally defined in module Utils::Logging

Log message

Parameters:

  • args (Object)

    argument list

Returns:

  • (NilClass)

- (Object) setup

Used by custom callback classes for setup



37
38
39
# File 'lib/carnivore/callback.rb', line 37

def setup
  debug 'No custom setup defined'
end

- (TrueClass, FalseClass) valid?(message)

Message is valid for this callback

Parameters:

Returns:

  • (TrueClass, FalseClass)


53
54
55
# File 'lib/carnivore/callback.rb', line 53

def valid?(message)
  true
end