Class: LogStash::Filters::Ruby::Script::Context

Inherits:
Object
  • Object
show all
Includes:
Util::Loggable
Defined in:
lib/logstash/filters/ruby/script/context.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(script, parameters) ⇒ Context

Returns a new instance of Context.



10
11
12
13
14
15
16
17
# File 'lib/logstash/filters/ruby/script/context.rb', line 10

def initialize(script, parameters)
  @script = script
  @script_path = @script.script_path
  @parameters = parameters
  @test_contexts = []
  @script_lock = Mutex.new
  @concurrency = :single
end

Instance Attribute Details

#execution_contextObject (readonly)

Returns the value of attribute execution_context.



7
8
9
# File 'lib/logstash/filters/ruby/script/context.rb', line 7

def execution_context
  @execution_context
end

#scriptObject (readonly)

Returns the value of attribute script.



7
8
9
# File 'lib/logstash/filters/ruby/script/context.rb', line 7

def script
  @script
end

Instance Method Details

#concurrency(type) ⇒ Object



47
48
49
# File 'lib/logstash/filters/ruby/script/context.rb', line 47

def concurrency(type)
  @concurrency = type
end

#execute_filter(event) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/logstash/filters/ruby/script/context.rb', line 51

def execute_filter(event)
  if @concurrency == :shared
    @script_lock.synchronize { @execution_context.filter(event) }
  else
    @execution_context.filter(event)
  end
end

#execute_registerObject



43
44
45
# File 'lib/logstash/filters/ruby/script/context.rb', line 43

def execute_register()
  @execution_context.register(@parameters)
end

#execute_testsObject



59
60
61
62
63
64
65
66
67
68
# File 'lib/logstash/filters/ruby/script/context.rb', line 59

def execute_tests
  @test_contexts.
    map(&:execute).
    reduce({:passed => 0, :failed => 0, :errored => 0}) do |acc,res|
      acc[:passed] += res[:passed]
      acc[:failed] += res[:failed]
      acc[:errored] += res[:errored]
      acc
    end
end

#load_execution_context(ec) ⇒ Object



34
35
36
# File 'lib/logstash/filters/ruby/script/context.rb', line 34

def load_execution_context(ec)
  ec.instance_eval(@script.content, @script_path, 1)
end

#load_scriptObject



38
39
40
41
# File 'lib/logstash/filters/ruby/script/context.rb', line 38

def load_script
  @execution_context = self.make_execution_context(:main, false)
  load_execution_context(@execution_context)
end

#make_execution_context(name, test_mode) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/logstash/filters/ruby/script/context.rb', line 19

def make_execution_context(name, test_mode)
  execution_context = LogStash::Filters::Ruby::Script::ExecutionContext.new(name, logger)

  # Proxy all the methods from this instance needed to be run from the execution context
  this = self # We need to use a clojure to retain access to this object
  # If we aren't in test mode we define the test. If we *are* then we don't define anything
  # since our tests are already defined
  if test_mode
    execution_context.define_singleton_method(:test) {|name,&block| nil }
  else
    execution_context.define_singleton_method(:test) {|name,&block| this.test(name, &block) }
  end
  execution_context
end

#test(name, &block) ⇒ Object



70
71
72
73
74
# File 'lib/logstash/filters/ruby/script/context.rb', line 70

def test(name, &block)
  test_context = LogStash::Filters::Ruby::Script::TestContext.new(self, name)
  test_context.instance_eval(&block)
  @test_contexts << test_context
end