Module: Pidly::Callbacks

Included in:
Control
Defined in:
lib/pidly/callbacks.rb

Overview

Pidly before/after callbacks

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(receiver) ⇒ Object

Extend and include callback methods

Parameters:

  • receiver (Class)

    The calling class



113
114
115
116
# File 'lib/pidly/callbacks.rb', line 113

def self.included(receiver)
  puts receiver.class
  receiver.extend self
end

Instance Method Details

#add_callback(callback, invoke) { ... } ⇒ Object

Add callback

Parameters:

  • callback (Symbol)

    Callback method name

  • invoke (Symbol, nil)

    Method to call

Yields:

  • Code to be executed upon callback invocation



104
105
106
# File 'lib/pidly/callbacks.rb', line 104

def add_callback(callback, invoke)
  Control.class_variable_set(:"@@#{callback}", invoke)
end

#after_stop(callback = nil) { ... } ⇒ Object

After stop

Right after the daemon is instructed to stop the following callback will be invoked and executed.

Examples:

after_start :method_name
# OR
after_start { puts "#{@pid} was just killed!" }

Parameters:

  • callback (Symbol) (defaults to: nil)

    Method name

Yields:

  • Code to be executed upon callback invocation



75
76
77
# File 'lib/pidly/callbacks.rb', line 75

def after_stop(callback=nil, &block)
  add_callback(:after_stop, (callback || block))
end

#before_start(callback = nil) { ... } ⇒ Object

Before start

Right before the daemon is instructed to start the following callback will be invoked and executed.

Examples:

before_start :method_name
# OR
before_start { puts "#{@pid} is about to start!" }

Parameters:

  • callback (Symbol) (defaults to: nil)

    Method name

Yields:

  • Code to be executed upon callback invocation



21
22
23
# File 'lib/pidly/callbacks.rb', line 21

def before_start(callback=nil, &block)
  add_callback(:before_start, (callback || block))
end

#error(callback = nil) { ... } ⇒ Object

Error

If the daemon encounters an error or an exception is raised the following callback will be invoked and executed.

Examples:

error :send_error_email
# OR
error { puts "ZOMG! #{@name} failed!" }

Parameters:

  • callback (Symbol) (defaults to: nil)

    Method name

Yields:

  • Code to be executed upon callback invocation



93
94
95
# File 'lib/pidly/callbacks.rb', line 93

def error(callback=nil, &block)
  add_callback(:error, (callback || block))
end

#start(callback = nil) { ... } ⇒ Object

Start

When the daemon is instructed to start the following callback will be invoked and executed.

Examples:

start :method_name
# OR
start { puts "Daemon Started!" }

Parameters:

  • callback (Symbol) (defaults to: nil)

    Method name

Yields:

  • Code to be executed upon callback invocation



39
40
41
# File 'lib/pidly/callbacks.rb', line 39

def start(callback=nil, &block)
  add_callback(:start, (callback || block))
end

#stop(callback = nil) { ... } ⇒ Object

Stop

When the daemon is instructed to stop the following callback will be invoked and executed.

Examples:

stop :method_name
# OR
stop { puts "Attempting to stop #{@name} with pid #{@pid}!" }

Parameters:

  • callback (Symbol) (defaults to: nil)

    Method name

Yields:

  • Code to be executed upon callback invocation



57
58
59
# File 'lib/pidly/callbacks.rb', line 57

def stop(callback=nil, &block)
  add_callback(:stop, (callback || block))
end