Module: Bannister

Defined in:
lib/bannister.rb,
lib/bannister/apache.rb,
lib/bannister/recurse.rb,
lib/bannister/version.rb

Defined Under Namespace

Classes: Apache, Recurse

Constant Summary collapse

SCRIPTS =

List of scripts in script/startup sorted asciibetically. This is the order they will be launched in, but there is no further dependency checking between the scripts.

Dir['script/startup/*'].sort
LAUNCHED_PIDS =

A list of pids of the started scripts. These will be sent TERM signals when bannister is stopped.

[]
PID_FILENAME =

The location of the pidfile for the master process. This will be de-hard-coded in a future release.

"pids/bannister.pid"
VERSION =
"0.0.2"

Instance Method Summary collapse

Instance Method Details

#err(msg) ⇒ Object

Used when you have erred. Shows the passed error message and exits with a status of 1.

Parameters:

  • msg (String)

    Message to display.



95
96
97
98
# File 'lib/bannister.rb', line 95

def err(msg)
  $stderr.puts(msg)
  exit(1)
end

#quitObject

Shut down all the managed processes, remove the bannister pidfile and exit.



67
68
69
70
71
# File 'lib/bannister.rb', line 67

def quit
  kill_all()
  remove_pid()
  exit(0)
end

#runObject

Launch the managed processes in screen so that they are all foreground processes.

If run from inside an existing screen session, it will be reused, and the managed processes will be added to it. Otherwise a new screen session is started.

Starting a new screen session is done by writing a temporary screenrc to /tmp, so $HOME/.screenrc is ignored.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/bannister.rb', line 40

def run()
  screenrc_filename = "/tmp/screenrc.#{$$}"

  if ENV['STY'] && !ENV['STY'].empty?
    # then we already have a screen, so reuse it
    # by simply running all the commands

    SCRIPTS.each do |script|
      system( command_for_script(script) )
    end

  else # create it. The easiest way to do this is to write 
    # out a screenrc containing the relevant commands and
    # let screen launch them
    begin
      write_screenrc(screenrc_filename)

      system "screen","-c",screenrc_filename
    ensure
      FileUtils.rm_f screenrc_filename
    end

  end
end

#startObject

Daemonize and launch the managed processes.



21
22
23
24
25
26
27
28
# File 'lib/bannister.rb', line 21

def start()
  if running?
    err "This app is already running!  Run #{$0} stop first."
  else
    daemonize(){ do_start() }
    wait_for_all()
  end
end

#stopObject

Send a TERM signal to a #start’ed bannister process.



76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/bannister.rb', line 76

def stop()
  old_pid = read_pid()
  if old_pid
    begin
      Process.kill("TERM", old_pid)
      Process.waitpid(old_pid)
    rescue SystemCallError 
      # which gets thrown if the old process is already dead
      nil
    end
    remove_pid()
  end
end