From 9371de61e3a8e179cc1f615d4eb7ce1652bbda0e Mon Sep 17 00:00:00 2001 From: Sameer Rahmani Date: Tue, 19 Apr 2022 14:52:32 +0100 Subject: [PATCH] Daemonize rigel --- builder | 2 +- rigel/requirements.txt | 2 ++ rigel/server.py | 35 +++++++++++++++++++++++++++++------ 3 files changed, 32 insertions(+), 7 deletions(-) diff --git a/builder b/builder index a65b052..fdc8358 100755 --- a/builder +++ b/builder @@ -124,7 +124,7 @@ function run() { ## Setup the working directory and make it ready for developmen function rigel() { ## Run the rigel server # shellcheck source=.venv/bin/activate . "$ME/.venv/bin/activate" - "$ME/rigel/server.py" "$ME/.env" + "$ME/rigel/server.py" "$ME/.env" $1 deactivate } diff --git a/rigel/requirements.txt b/rigel/requirements.txt index 31e7542..483b6f7 100644 --- a/rigel/requirements.txt +++ b/rigel/requirements.txt @@ -1,3 +1,5 @@ tts==0.6 python-dotenv simpleaudio +python-daemon==2.3 +python-pidfile==3.0 diff --git a/rigel/server.py b/rigel/server.py index 5960f88..e342e7f 100755 --- a/rigel/server.py +++ b/rigel/server.py @@ -12,23 +12,46 @@ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # You should have received a copy of the GNU General Public License - +import os import sys +import signal import asyncio + +import daemon import dotenv +import pidfile from tcp import start, synth -def main(): - config = dotenv.dotenv_values(sys.argv[1]) - +def main(conf): try: - asyncio.run(start(config, synth(config))) + asyncio.run(start(config, synth(conf))) except KeyboardInterrupt: sys.exit() if __name__ == "__main__": - main() + config = dotenv.dotenv_values(sys.argv[1]) + action = sys.argv[2] + pfile = config.get("PID_FILE", "/var/run/rigel.pid") + pidlock = pidfile.PIDFile(pfile) + + if action == "status": + print("Running" if pidlock.is_running else "Not running.") + elif action == "stop": + if pidlock.is_running: + print("Terminating...") + f = open(pfile, "r") + pid = int(f.read()) + os.kill(pid, signal.SIGTERM) + else: + print("Not running.") + elif action == "start": + + with daemon.DaemonContext(stdout=sys.stdout, + stderr=sys.stderr, + pidfile=pidlock): + + main(config)