feat: Add daemon mode and signal handling to main application

Introduced command line arguments for daemon mode and configuration file
support. Enhanced the application with signal handling for reload, log
rotation, and termination. Updated the main function for improved
structure and robustness.
This commit is contained in:
2025-04-18 14:14:13 +02:00
parent 964e0b997c
commit 06d33b218f

View File

@@ -4,27 +4,51 @@
"""Main application of revpi-middleware daemon.""" """Main application of revpi-middleware daemon."""
from logging import getLogger from logging import getLogger
from .daemon import MiddlewareDaemon
from . import proginit as pi from . import proginit as pi
log = getLogger(__name__) log = getLogger(__name__)
# Configure command line arguments for the daemon
if pi.can_be_forked():
# Show the parameter only on systems that support fork call
pi.parser.add_argument(
"-d",
"--daemon",
action="store_true",
dest="daemon",
help="run program as a daemon in background",
)
pi.parser.add_argument(
"-c",
"--conffile",
dest="conffile",
default="/etc/{0}/{0}.conf".format(pi.programname),
help="application configuration file",
)
def main() -> int: def main() -> int:
""" import signal
This is the main entry point.
We do not pack this to a 'if __name__ == "__main__"' thing to be able to # Parse command line arguments
call this function from different places. The __main__ module and entry pi.init_app()
points in the setup.py will call this function and linke to have an int
as return value for the exit code.
"""
log.debug("Enter main() function")
log.warning(f"This is the empty '{pi.programname}' application so far") root = MiddlewareDaemon()
log.debug("Leave main() function") # Set signals
signal.signal(signal.SIGHUP, lambda n, f: root.reload_config())
signal.signal(signal.SIGUSR1, lambda n, f: root.rotate_logfile())
signal.signal(signal.SIGINT, lambda n, f: root.stop())
signal.signal(signal.SIGTERM, lambda n, f: root.stop())
return_code = root.start()
# Finally, call the cleanup function of proginit to flush all log buffers. # Finally, call the cleanup function of proginit to flush all log buffers.
pi.cleanup() pi.cleanup()
return 0 return return_code
if __name__ == "__main__":
exit(main())