Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accept running as daemon #1541

Merged
merged 2 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
4 changes: 3 additions & 1 deletion 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 Expand Up @@ -263,7 +265,7 @@ def sigchld_handler(self, *a):
if pid == 0: # Still alive
continue
except ChildProcessError:
pass
pid = self.pids.get("name", "<unknown>")

logger.warning(
"Background service dead. Restarting. service=%s pid=%s", name, pid
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