Skip to content

Commit

Permalink
Check parent in subprocess only
Browse files Browse the repository at this point in the history
  • Loading branch information
bersace committed Nov 8, 2024
1 parent 7811504 commit c6f96b2
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 15 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Ensure you use consistent title format.
## UNRELEASED

- Fix register-instance CLI.
- Fix running daemonized.
- Raise proper error on unknown environment.
- Improve alerting performance.

Expand All @@ -27,7 +28,7 @@ Ensure you use consistent title format.
- Drop rhel7 and buster support, minimum versions are now 6.0.2 for tornado
and 1.3.2 for sqlalchemy.
- Drop plugin hotplug. Just restart temBoard UI or agent.
- Drop daemonization. Use nohup or systemd.
- Drop daemonization. Use systemd.

**Other changes**

Expand Down
6 changes: 0 additions & 6 deletions agent/temboardagent/web/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,6 @@
class HTTPDService(syncio.Service):
name = "web"

def __init__(self, app):
self.app = app

def __str__(self):
return self.name

# for services.run
def setup(self, *_, **__):
ServerHandler.server_software = "temBoard-agent/%s" % __version__
Expand Down
2 changes: 2 additions & 0 deletions ui/temboardui/toolkit/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,8 @@ def fork(self, service):
if hasattr(service, "command"):
execute(service)
else:
# Enable parent pid check.
service.ppid = os.getppid()
os._exit(run(service))

def _read_pids(self):
Expand Down
21 changes: 13 additions & 8 deletions ui/temboardui/toolkit/syncio.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,33 @@


class Service:
# Mixin for scheduler and worker pool services
# Mixin for scheduler, worker pool and agent httpd services
# Manages sync loop and signals.
name = None

def __init__(self, app):
self.app = app
self.ppid = None

def __str__(self):
return self.name

# interface for services.run
def create_loop(self):
return Loop(self)
return Loop(self, self.ppid)

# Interface for SignalMultiplexer
def sighup_handler(self, *a):
self.app.reload()


class Loop:
def __init__(self, service):
def __init__(self, service, ppid=None):
self.service = service
self.signalmngr = SignalManager()
self.running = False
# If not None, enable parent pid check.
self.ppid = ppid

def __repr__(self):
return "<Loop %s>" % self.service
Expand All @@ -49,10 +52,9 @@ def remove_signal_handler(self, sig):
# Interface for services.run
def start(self):
with self.signalmngr:
ppid = os.getppid()
self.running = True
while self.running:
if self._stop_with_parent(ppid):
if self._stop_with_parent():
break

self.service.accept()
Expand All @@ -63,10 +65,13 @@ def stop(self):
def close(self):
self.running = False

def _stop_with_parent(self, ppid):
self.running = os.getppid() == ppid
def _stop_with_parent(self):
if self.ppid is None: # Don't check parent.
return
# If ppid changed (parent exited), stop the loop.
self.running = os.getppid() == self.ppid
if not self.running:
logger.info("Parent process exited. Exiting. ppid=%s", ppid)
logger.info("Parent process exited. Exiting. ppid=%s", self.ppid)
return not self.running


Expand Down

0 comments on commit c6f96b2

Please sign in to comment.