Class: Aef::Init

Inherits:
Object
  • Object
show all
Defined in:
lib/aef/init.rb,
lib/aef/init/version.rb

Overview

Clean and simple *nix init scripts with Ruby

Constant Summary collapse

VERSION =

The currently loaded library version

Using Semantic Versioning (2.0.0-rc.1) rules

See Also:

'2.0.1'.freeze

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.default_command(command = false) ⇒ Object

Note:

This used to be implicitly set to “restart” but in practice caused far too much unwanted service restarts, so it is now not enabled by default.

The default command to be called if no command is specified on the commandline.



56
57
58
59
60
61
62
# File 'lib/aef/init.rb', line 56

def default_command(command = false)
  if command.equal?(false)
    @default_command
  else
    @default_command = command.to_sym
  end
end

.stop_start_delay(seconds = false) ⇒ Float

The delay in seconds between the call of the stop and the start method in the predefined restart method.

Parameters:

  • seconds (Numeric, false) (defaults to: false)

    time in seconds, if false is given, the value will be returned and not modified.

Returns:

  • (Float)

    time in seconds



76
77
78
79
80
81
82
# File 'lib/aef/init.rb', line 76

def stop_start_delay(seconds = false)
  if seconds.equal?(false)
    @stop_start_delay ||= 0.0
  else
    @stop_start_delay = seconds.to_f
  end
end

Class Method Details

.parseObject

Call this to begin commandline parse.

If an invalid command is specified on the commandline, a usage example is displayed. If no command is specified, the default command is started, if one is set.



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/aef/init.rb', line 89

def parse
  command = ARGV.shift || :default
    
  valid_commands = []
    
  ancestors.each do |klass|
    valid_commands += klass.public_instance_methods(false)
    break if klass == Aef::Init
  end

  valid_commands = valid_commands.sort.map(&:to_sym).uniq!
    
  command = command.to_sym
    
  if command == :default && default_command
    new.send(default_command)
  elsif valid_commands.include?(command)
    new.send(command)
  else
    puts "Usage: #$PROGRAM_NAME {#{valid_commands.join('|')}}"; exit false
  end
end

Instance Method Details

#restartObject

By default restart simply calls stop and then start



127
128
129
130
131
# File 'lib/aef/init.rb', line 127

def restart
  stop
  sleep self.class.stop_start_delay
  start
end

#startObject

The start method needs to be implemented in a subclass



115
116
117
118
# File 'lib/aef/init.rb', line 115

def start
  warn 'start method needs to be implemented'
  exit false
end

#stopObject

The stop method needs to be implemented in a subclass



121
122
123
124
# File 'lib/aef/init.rb', line 121

def stop
  warn 'stop method needs to be implemented'
  exit false
end