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.
55 lines
1.3 KiB
Python
55 lines
1.3 KiB
Python
# -*- coding: utf-8 -*-
|
|
# SPDX-FileCopyrightText: 2025 KUNBUS GmbH
|
|
# SPDX-License-Identifier: GPL-2.0-or-later
|
|
"""Main application of revpi-middleware daemon."""
|
|
from logging import getLogger
|
|
|
|
from .daemon import MiddlewareDaemon
|
|
from . import proginit as pi
|
|
|
|
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:
|
|
import signal
|
|
|
|
# Parse command line arguments
|
|
pi.init_app()
|
|
|
|
root = MiddlewareDaemon()
|
|
|
|
# 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.
|
|
pi.cleanup()
|
|
|
|
return return_code
|
|
|
|
|
|
if __name__ == "__main__":
|
|
exit(main())
|