Class: Serfx::Utils::Handler::SerfEvent

Inherits:
Object
  • Object
show all
Defined in:
lib/serfx/utils/handler.rb

Overview

when serf agent invokes a handler it passes the event payload through STDIN. while event metadata such as event type, name etc is passed as a set of environment variables.

SerfEvent

encapsulates such event.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env = ENV, stdin = STDIN) ⇒ SerfEvent

Returns a new instance of SerfEvent.

Parameters:

  • env (Hash) (defaults to: ENV)

    environment

  • stdin (IO) (defaults to: STDIN)

    stadard input stream for the event



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/serfx/utils/handler.rb', line 33

def initialize(env = ENV, stdin = STDIN)
  @environment = {}
  @payload = nil
  @name = nil
  env.keys.select { |k| k =~ /^SERF/ }.each do | k|
    @environment[k] = env[k].strip
  end
  @type = @environment['SERF_EVENT']
  case @type
  when 'query'
    @name = @environment['SERF_QUERY_NAME']
    begin
      @payload = stdin.read_nonblock(4096).strip
    rescue Errno::EAGAIN, EOFError
    end
  when 'user'
    @name = @environment['SERF_USER_EVENT']
    begin
      @payload = stdin.read_nonblock(4096).strip
    rescue Errno::EAGAIN, EOFError
    end
  end
end

Instance Attribute Details

#environmentObject (readonly)

Returns the value of attribute environment.



29
30
31
# File 'lib/serfx/utils/handler.rb', line 29

def environment
  @environment
end

#nameObject (readonly)

Returns the value of attribute name.



29
30
31
# File 'lib/serfx/utils/handler.rb', line 29

def name
  @name
end

#payloadObject (readonly)

Returns the value of attribute payload.



29
30
31
# File 'lib/serfx/utils/handler.rb', line 29

def payload
  @payload
end

#typeObject (readonly)

Returns the value of attribute type.



29
30
31
# File 'lib/serfx/utils/handler.rb', line 29

def type
  @type
end