The code in this module drives the “main” method
bin/habitat simply does the following:
import habitat
habitat.main.Program().main()
get_options reads command line options and a configuration file
This function parses command line options, and reads a configuration file (which must be in the ConfigParser format).
It will read default_configuration_file and will ignore any errors that occur while doing so, unless a different config file is specified at the command line (failures on an explicitly stated config file will raise an execption).
Command line options have priority over options from a config file.
setup_logging initalises the Python logging module.
It will initalise the ‘habitat’ logger and creates one, two, or no Handlers, depending on the values provided for log_file_level and log_stderr_level.
Program provides the main(), shutdown() and reload() methods
The main method of habitat
This method does the following:
The Program thread processes SHUTDOWN and RELOAD events
In order to make shutdown() and reload() return instantly, the actual work requested by calling those methods is done by this thread.
This class listens for signals
It responds to the following signals. When it receives one, it calls the appropriate method of Program
The documentation for the signal module contains information on the various signal constant definitions.
SIGUSR1 is meant for internal use only, and is used to terminate the signal-listening thread when the program wishes to shut down. (see SignalListener.exit())
setup() installs signal handlers for the signals that we want
Must be called in the MainThread
listen() listens for signals delivered to the process forever
It calls signal.pause() indefinitely, meaning that any signal sent to the process can be caught instantly and unobtrusivly.
Must be called in the MainThread
exit() terminates the listen() loop
It raises SIGUSR1 in this process, causing the infinite listen() loop to exit (SignalListener.handle() will call sys.exit())