pgbouncemgr/pgbouncemgr/manager.py

77 lines
2.7 KiB
Python

# -*- coding: utf-8 -*-
# pylint: disable=too-few-public-methods,missing-docstring
"""The manager implements the main process that keeps track of changes in the
PostgreSQL cluster and that reconfigures pgbouncer when needed."""
from time import sleep
from argparse import ArgumentParser
from pgbouncemgr.constants import DEFAULT_CONFIG, DEFAULT_LOG_FACILITY
from pgbouncemgr.logger import Logger, ConsoleLog, SyslogLog
from pgbouncemgr.config import Config
from pgbouncemgr.state import State
from pgbouncemgr.drop_privileges import drop_privileges
from pgbouncemgr.state_store import StateStore
from pgbouncemgr.node_poller import NodePoller
class Manager():
def __init__(self, argv=None):
args = _parse_arguments(argv)
self.config = Config(args.config)
self.single_shot = args.single_shot
self._create_logger(args)
self._create_state()
self.node_poller = NodePoller(self.state, self.log)
def _create_logger(self, args):
self.log = Logger()
self.log.append(ConsoleLog(args.verbose, args.debug))
if args.log_facility.lower() != 'none':
self.log.append(SyslogLog("pgbouncemgr", args.log_facility))
def _create_state(self):
self.state = State.from_config(self.config, self.log)
self.state_store = StateStore(self.config.state_file, self.state)
self.state_store.load()
def run(self):
"""Starts the manager process."""
drop_privileges(self.config.run_user, self.config.run_group)
while True:
self.node_poller.poll()
if self.single_shot:
return
sleep(self.config.poll_interval_in_sec)
def _parse_arguments(args):
parser = ArgumentParser(description="pgbouncemgr")
parser.add_argument(
"-v", "--verbose",
default=False, action="store_true",
help="enable verbose console output (default: disabled)")
parser.add_argument(
"-d", "--debug",
default=False, action="store_true",
help="enable debugging console output (default: disabled)")
parser.add_argument(
"-s", "--single-shot",
default=False, action="store_true",
help="do only a single run, instead of running continuously " +
"(default: disabled)")
parser.add_argument(
"-f", "--log-facility",
default=DEFAULT_LOG_FACILITY,
help="syslog facility to use or 'none' to disable syslog logging " +
"(default: %s)" % DEFAULT_LOG_FACILITY)
parser.add_argument(
"--config",
default=DEFAULT_CONFIG,
help="config file (default: %s)" % DEFAULT_CONFIG)
return parser.parse_args(args)
if __name__ == "__main__":
Manager(None).run()