added kill and restart args to stop or restart the daemon if running...

This commit is contained in:
Johannes Findeisen 2022-08-25 02:34:25 +02:00
commit eba54b8cb0
3 changed files with 25 additions and 13 deletions

View file

@ -1,12 +1,11 @@
{
"interval": 60,
"database_type": "mariadb",
"database": "uplink",
"database_host": "127.0.0.1",
"database_user": "USER",
"database_password": "PASSWORD",
"mode": "cron",
"log_level": "ERROR",
"log_level": "info",
"uplinks": [
{ "provider": "Cable Provider", "ip": "192.168.0.1", "password": "1234" },
{ "provider": "DSL Provider", "ip": "192.168.1.1", "password": "1234" }

View file

@ -21,7 +21,6 @@ import signal
logger = getLogger(__name__)
# TODO: Check why pid files are not being deleted when sending the TERM signal
class Daemon:
"""
A generic daemon class.
@ -46,7 +45,7 @@ class Daemon:
logger.error('fork #1 failed: {0}\n'.format(err))
sys.exit(1)
# decouple from parent environme
# decouple from parent environment
os.chdir('/')
os.setsid()
os.umask(0)
@ -95,7 +94,7 @@ class Daemon:
pid = None
if pid:
message = "pid_file {0} already exist. Daemon already running?\n"
message = "pid_file {0} already exist. Daemon already running?"
logger.error(message.format(self.pid_file))
sys.exit(1)
@ -116,7 +115,7 @@ class Daemon:
pid = None
if not pid:
message = "pid_file {0} does not exist. Daemon not running?\n"
message = "pid_file {0} does not exist. Daemon not running?"
logger.error(message.format(self.pid_file))
# not an error in a restart
return

28
uplink
View file

@ -56,7 +56,13 @@ def parse_args():
help="cron mode executes the script only once without loop (default: false)")
mode.add_argument("-d", "--daemon", default=False, dest="daemon", action="store_true",
help="run as native daemon (default: false)")
help="run as native daemon (default: false)")
parser.add_argument("-k", "--kill", default=False, dest="kill", action="store_true",
help="kill the daemon if it is running (default: false)")
parser.add_argument("-r", "--restart", default=False, dest="restart", action="store_true",
help="restart the daemon if it is running(default: false)")
mode.add_argument("-f", "--foreground", default=False, dest="foreground", action="store_true",
help="run looped in foreground (default: false)")
@ -66,12 +72,14 @@ def parse_args():
"(default: 60)")
"""
TODO: maybe make database connection information as args... not sure.
parser.add_argument("-b", "--database", metavar="DATABASE", help="database to use")
parser.add_argument("-u", "--user", type=str, help="database username")
parser.add_argument("-p", "--password", type=str, help="database password")
"""
parser.add_argument("-l", "--logfile", metavar="LOGFILE", help="logfile to use")
parser.add_argument("-n", "--logcount", default=5, type=int,
@ -132,7 +140,7 @@ def main():
config_data = configfile.read()
config = json.loads(config_data)
except Exception as err:
logger.error(str("uplink: configuration error! " + str(err)))
logger.error(str("uplink: configuration error!\n" + str(err)))
sys.exit(1)
try:
@ -145,24 +153,30 @@ def main():
# interval set in args is overriding configuration and default
config["interval"] = args.interval
uplink = Uplink("/tmp/uplink.pid", config)
pid_file = "/tmp/uplink.pid"
u = Uplink(pid_file, config)
if args.cron:
from threading import Thread
for i in range(len(config["uplinks"])):
t = Thread(target=uplink.fetch_data, args=(config, i))
t = Thread(target=u.fetch_data, args=(config, i))
t.start()
elif args.daemon:
uplink.start()
if args.kill:
u.stop()
elif args.restart:
u.restart()
else:
u.start()
elif args.foreground:
from threading import Thread
while True:
for i in range(len(config["uplinks"])):
t = Thread(target=uplink.fetch_data, args=(config, i))
t = Thread(target=u.fetch_data, args=(config, i))
t.start()
try:
time.sleep(config["interval"])
except KeyboardInterrupt:
except KeyboardInterrupt as err:
logger.info(str("uplink: program terminated by user!"))
sys.exit(0)
else: