Module: Fluent::Compat::CallSuperMixin

Defined in:
lib/fluent/compat/call_super_mixin.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.prepended(klass) ⇒ Object

This mixin is to prepend to 3rd party plugins of v0.12 APIs. In past, there were not strong rule to call super in #start, #before_shutdown and #shutdown. But v0.14 API requires to call super in these methods to setup/teardown plugin helpers and others. This mixin prepends method calls to call super forcedly if checker returns false (it shows Fluent::Plugin::Base#methods wasn’t called)



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/fluent/compat/call_super_mixin.rb', line 25

def self.prepended(klass)
  @@_super_start ||= {}
  @@_super_before_shutdown ||= {}
  @@_super_shutdown ||= {}

  # ancestors[0]: this module
  # ancestors[1]: prepended class (plugin itself)
  method_search = ->(ancestors, method){
    closest = ancestors[2, ancestors.size - 2].index{|m| m.method_defined?(method) }
    ancestors[2 + closest].instance_method(method)
  }
  @@_super_start[klass]           = method_search.call(klass.ancestors, :start) # this returns Fluent::Compat::*#start (or helpers on it)
  @@_super_before_shutdown[klass] = method_search.call(klass.ancestors, :before_shutdown)
  @@_super_shutdown[klass]        = method_search.call(klass.ancestors, :shutdown)
end

Instance Method Details

#before_shutdownObject



50
51
52
53
54
55
56
# File 'lib/fluent/compat/call_super_mixin.rb', line 50

def before_shutdown
  super
  unless self.before_shutdown?
    log.warn "super was not called in #before_shutdown: calling it forcedly", plugin: self.class
    @@_super_before_shutdown[self.class].bind(self).call
  end
end

#shutdownObject



67
68
69
70
71
72
73
# File 'lib/fluent/compat/call_super_mixin.rb', line 67

def shutdown
  super
  unless self.shutdown?
    log.warn "super was not called in #shutdown: calling it forcedly", plugin: self.class
    @@_super_shutdown[self.class].bind(self).call
  end
end

#startObject



41
42
43
44
45
46
47
48
# File 'lib/fluent/compat/call_super_mixin.rb', line 41

def start
  super
  unless self.started?
    @@_super_start[self.class].bind(self).call
    # #super will reset logdev (especially in test), so this warn should be after calling it
    log.warn "super was not called in #start: called it forcedly", plugin: self.class
  end
end

#stopObject



58
59
60
61
62
63
64
65
# File 'lib/fluent/compat/call_super_mixin.rb', line 58

def stop
  klass = self.class
  @@_super_start.delete(klass)
  @@_super_before_shutdown.delete(klass)
  @@_super_shutdown.delete(klass)

  super
end