diff options
author | Alexander Sulfrian <alexander@sulfrian.net> | 2016-05-10 02:08:05 +0200 |
---|---|---|
committer | Alexander Sulfrian <alexander@sulfrian.net> | 2016-05-10 02:15:37 +0200 |
commit | 844eafc830247c704a620ebdd54835e343ce1ad5 (patch) | |
tree | 48e839bea8b9dd49f0cfd20427189dd268782568 | |
parent | 25d7a6b1bf59ac15e2f11fd776f6ae7f16237c0e (diff) | |
download | bot-844eafc830247c704a620ebdd54835e343ce1ad5.tar.gz bot-844eafc830247c704a620ebdd54835e343ce1ad5.tar.bz2 bot-844eafc830247c704a620ebdd54835e343ce1ad5.zip |
Watcher: Only restart failed processes
If a process gets killed or dies because of some other reason, the watcher
should only restart this single process. It therefore needs a map between
the child process id and the configuration.
-rwxr-xr-x | __init__.py | 14 |
1 files changed, 9 insertions, 5 deletions
diff --git a/__init__.py b/__init__.py index b3ea420..9c6bcdc 100755 --- a/__init__.py +++ b/__init__.py @@ -14,7 +14,7 @@ class Watcher(object): # Cf. http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/496735 def __init__(self): self._configs = [] - self._children = [] + self._children = {} signal.signal(signal.SIGTERM, self.sig_term) def add(self, config): @@ -23,7 +23,7 @@ class Watcher(object): def _start(self, config): child = os.fork() if child != 0: - self._children.append(child) + self._children[child] = config else: signal.signal(signal.SIGTERM, signal.SIG_DFL) run_phenny(config) @@ -33,18 +33,22 @@ class Watcher(object): self._start(config) def watch(self): + self.run() + alive = True while alive: - self.run() - try: proc = None while proc not in self._children: (proc, _) = os.wait() + + config = self._children[proc] + del self._children[proc] + self._start(config, True) except KeyboardInterrupt: alive = False - self.kill() + self.kill() sys.exit() def kill(self): |