autonomous restart and furlough methods work

workspace
Brendan Howell 8 years ago
parent ffe60c5540
commit ffa4abb0cd

@ -88,6 +88,7 @@ class Bureau(object):
# setup log file # setup log file
# TODO: make log level globally and bureau-specific via config # TODO: make log level globally and bureau-specific via config
# TODO: custom handler to print errors to small printer
logfile = os.path.join(basepath, self.prefix + ".log") logfile = os.path.join(basepath, self.prefix + ".log")
logging.basicConfig(filename=logfile, level=logging.DEBUG) logging.basicConfig(filename=logfile, level=logging.DEBUG)
self.log = logging.getLogger(self.prefix) self.log = logging.getLogger(self.prefix)
@ -110,7 +111,7 @@ class Bureau(object):
self._recv.bind("ipc://" + self.prefix + ".ipc") self._recv.bind("ipc://" + self.prefix + ".ipc")
self.log.debug("bureau " + self.name + " waiting for messages") self.log.debug("bureau " + self.name + " waiting for messages")
self.log.debug("commands: ") self.log.debug("commands: ")
self.log.debug(self.commands) self.log.debug(str(self.commands))
def load_config(self): def load_config(self):
""" """
@ -210,8 +211,8 @@ class Bureau(object):
self.send("IR", "addapi", api_detail) self.send("IR", "addapi", api_detail)
self.log.debug("registered:") self.log.debug("registered:")
self.log.debug(self.commands) self.log.debug(str(self.commands))
self.log.debug(self.api) self.log.debug(str(self.api))
def print_full(self, template, **kwargs): def print_full(self, template, **kwargs):
"""print a full page (A4) document """ """print a full page (A4) document """
@ -240,7 +241,7 @@ class Bureau(object):
# TODO: make paper size a config variable # TODO: make paper size a config variable
pdfpath = tempfile.mkstemp(".pdf")[1] pdfpath = tempfile.mkstemp(".pdf")[1]
self.log.debug("rendering with: ", self.html2pdf + htmlpath + " " + pdfpath) self.log.debug("rendering with: " + self.html2pdf + htmlpath + " " + pdfpath)
subprocess.call(self.html2pdf + htmlpath + " " + pdfpath + subprocess.call(self.html2pdf + htmlpath + " " + pdfpath +
" A4 1920px", shell=True) " A4 1920px", shell=True)
subprocess.call("lpr " + pdfpath, shell=True) subprocess.call("lpr " + pdfpath, shell=True)
@ -314,12 +315,12 @@ class Bureau(object):
time.sleep(0.05) # don't waste CPU time.sleep(0.05) # don't waste CPU
continue continue
try: try:
self.log.debug("got message:", msg) self.log.debug("got message:" + msg)
dot = msg.find(".") dot = msg.find(".")
ref = msg[:dot] ref = msg[:dot]
if (dot < len(msg) - 1) and (dot > 0): if (dot < len(msg) - 1) and (dot > 0):
self.log.debug("msg length:", len(msg)) self.log.debug("msg length: %d", len(msg))
self.log.debug("dot at", dot) self.log.debug("dot at %d", dot)
# TODO: maybe trim off the trailing "." for convenience # TODO: maybe trim off the trailing "." for convenience
data = msg[dot + 1:] data = msg[dot + 1:]
# data = str(data) # force to be a string # data = str(data) # force to be a string
@ -327,9 +328,9 @@ class Bureau(object):
data = None data = None
self.log.debug("data: " + str(data)) self.log.debug("data: " + str(data))
except IndexError as err: except IndexError as err:
self.log.warning("invalid message: ", err) self.log.warning("invalid message: %s", err)
continue continue
self.log.debug(("got method: " + ref)) self.log.debug("got method: " + ref)
if (ref in self.commands) or (ref in self.api): if (ref in self.commands) or (ref in self.api):
# catch TypeErrors for case of bogus params # catch TypeErrors for case of bogus params
@ -354,7 +355,7 @@ class Bureau(object):
# print("invalid data for command '{}': {}".format(ref, data)) # print("invalid data for command '{}': {}".format(ref, data))
# self._recv.send_unicode("Error. Invalid or missing data.") # self._recv.send_unicode("Error. Invalid or missing data.")
except KeyError as err: except KeyError as err:
self.log.warning(err) self.log.warning(str(err))
self.log.warning("You are calling a command as an API or vice-versa.") self.log.warning("You are calling a command as an API or vice-versa.")
self._recv.send_unicode( self._recv.send_unicode(
"Error. Command called as API or API as command.") "Error. Command called as API or API as command.")

@ -1,5 +1,6 @@
import json import json
import os.path import os
import signal
import subprocess import subprocess
import code128 import code128
@ -133,7 +134,9 @@ class InhumanResources(Bureau):
also help restore a messed up office to normal operation. also help restore a messed up office to normal operation.
""" """
# TODO: find the PID of mgmt and send it a HUP signal # TODO: find the PID of mgmt and send it a HUP signal
pass with open("mgmt.pid") as pfile:
pid = int(pfile.read())
os.kill(pid, signal.SIGHUP)
def main(): def main():

@ -74,6 +74,9 @@ class Management:
main loop for Management, will restart crashed bureaus main loop for Management, will restart crashed bureaus
and reload everything when sent a SIGHUP and reload everything when sent a SIGHUP
""" """
with open("mgmt.pid", "w") as pfile:
pfile.write(str(os.getpid()))
signal.signal(signal.SIGHUP, self.reload) signal.signal(signal.SIGHUP, self.reload)
self._start_bureaus() self._start_bureaus()

Loading…
Cancel
Save