refactor management. now reloads all bureaus on HUP, restarts crashed ones.

workspace
Brendan Howell 8 years ago
parent f1961337d3
commit 993c7c3c4e

@ -9,51 +9,85 @@ import configparser
import importlib import importlib
import multiprocessing import multiprocessing
import os import os
import signal
import time import time
def mgmt():
class Management:
""" """
Primary management function. Main loop that starts and runs all bureaus. Management is the executive of the Screenless Office. It can start, stop
Some day this will do nice supervisory things like restart crashed buros. and reconfigure the subordinate bureaus of the office.
""" """
basepath = os.path.expanduser("~/.screenless") def __init__(self):
if not os.path.exists(basepath): basepath = os.path.expanduser("~/.screenless")
os.mkdir(basepath) if not os.path.exists(basepath):
os.chdir(basepath) os.mkdir(basepath)
os.chdir(basepath)
config = configparser.ConfigParser()
procs = {}
self.procs = {}
self.org_chart = []
self._load_config()
try: def _load_config(self):
config.read("mgmt.ini") config = configparser.ConfigParser()
org_chart = config["mgmt"]["bureaus"].split() try:
except KeyError: config.read("mgmt.ini")
config["mgmt"] = {"bureaus": self.org_chart = config["mgmt"]["bureaus"].split()
"ihr typing publicrelations photography jokes"} except KeyError:
with open("mgmt.ini", "w") as configfile: config["mgmt"] = {"bureaus":
config.write(configfile) "ihr typing publicrelations photography jokes"}
print("created new mgmt.ini config file. please modify this to suit.") with open("mgmt.ini", "w") as configfile:
org_chart = config["mgmt"]["bureaus"].split() config.write(configfile)
print("Created new mgmt.ini config file. Please modify to suit.")
self.org_chart = config["mgmt"]["bureaus"].split()
print("org chart:", org_chart) def _start_bureaus(self):
"""
Initialized and start all child bureaus
"""
for buro in self.org_chart:
# TODO: this may need some sanity checking for crash/reload
lib = importlib.import_module("bureau." + buro)
proc = multiprocessing.Process(target=lib.main)
self.procs[buro] = proc
proc.start()
for buro in org_chart: def _stop_bureaus(self):
lib = importlib.import_module("bureau." + buro) """
proc = multiprocessing.Process(target=lib.main) Terminates all running bureau children
procs[buro] = proc """
proc.start() for buro in self.org_chart:
proc = self.procs[buro]
proc.terminate()
while True: def reload(self, signum=None, frame=None):
for buro in org_chart: """
proc = procs[buro] stop all bureaus, reload config, restart all bureaus.
if not proc.is_alive(): Note, this may cause a loss of state in some cases.
print("bureau", buro, "has crashed! Call the consultants!") """
#TODO this should probably restart in some sensible way self._stop_bureaus()
time.sleep(1) self._load_config()
self._start_bureaus()
def run(self):
"""
main loop for Management, will restart crashed bureaus
and reload everything when sent a SIGHUP
"""
signal.signal(signal.SIGHUP, self.reload)
self._start_bureaus()
while True:
for buro in self.org_chart:
proc = self.procs[buro]
if not proc.is_alive():
print("bureau", buro, "has crashed! Call the consultants!")
print("restarting...")
#TODO this should probably only retry a few times
proc.start()
time.sleep(1)
if __name__ == "__main__": if __name__ == "__main__":
mgmt() mgmt = Management()
mgmt.run()

Loading…
Cancel
Save