Switched from JSON to ini files for configuration, lots of code adds, refactoring and even many design changes.
This commit is contained in:
parent
1797c37a1d
commit
779c2f0b48
58 changed files with 1227 additions and 649 deletions
113
monipy/daemon.py
113
monipy/daemon.py
|
|
@ -1,22 +1,25 @@
|
|||
# Copyright (c) 2022 Johannes Findeisen <you@hanez.org>
|
||||
#
|
||||
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
# of this software and associated documentation files (the "Software"), to deal
|
||||
# in the Software without restriction, including without limitation the rights
|
||||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
# copies of the Software, and to permit persons to whom the Software is furnished
|
||||
# to do so, subject to the following conditions:
|
||||
#
|
||||
# The above copyright notice and this permission notice (including the next
|
||||
# paragraph) shall be included in all copies or substantial portions of the
|
||||
# Software.
|
||||
#
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
|
||||
# OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||||
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
|
||||
# OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
"""
|
||||
This file is part of monipy (https://hanez.org/monipy/)
|
||||
Copyright (c) 2022 Johannes Findeisen <you@hanez.org>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is furnished
|
||||
to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice (including the next
|
||||
paragraph) shall be included in all copies or substantial portions of the
|
||||
Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
|
||||
OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||||
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
|
||||
OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
"""
|
||||
|
||||
import atexit
|
||||
import os
|
||||
|
|
@ -26,48 +29,44 @@ import time
|
|||
|
||||
from logging import getLogger
|
||||
|
||||
logger = getLogger('monipy')
|
||||
logger = getLogger('monipyd')
|
||||
|
||||
|
||||
class Daemon:
|
||||
"""
|
||||
Usage: subclass the daemon class and override the run() method.
|
||||
"""
|
||||
# subclass the daemon class and override the run() method.
|
||||
|
||||
def __init__(self, pid_file):
|
||||
self.pid_file = pid_file
|
||||
self.__pid_file = pid_file
|
||||
|
||||
def daemonize(self):
|
||||
"""
|
||||
Daemonize the class using the UNIX double fork mechanism.
|
||||
"""
|
||||
# daemonize the class using the UNIX double fork mechanism.
|
||||
|
||||
# Do first fork
|
||||
# do first fork.
|
||||
try:
|
||||
pid = os.fork()
|
||||
if pid > 0:
|
||||
# Exit first parent
|
||||
# exit first parent.
|
||||
sys.exit(0)
|
||||
except OSError as err:
|
||||
logger.error(str('fork #1 failed: {0}'.format(err)))
|
||||
sys.exit(1)
|
||||
|
||||
# Decouple from parent environment
|
||||
# decouple from parent environment.
|
||||
os.chdir('/')
|
||||
os.setsid()
|
||||
os.umask(0)
|
||||
|
||||
# Do second fork
|
||||
# do second fork.
|
||||
try:
|
||||
pid = os.fork()
|
||||
if pid > 0:
|
||||
# Exit from second parent
|
||||
# Exit from second parent.
|
||||
sys.exit(0)
|
||||
except OSError as err:
|
||||
logger.error(str('fork #2 failed: {0}'.format(err)))
|
||||
sys.exit(1)
|
||||
|
||||
# Redirect standard file descriptors
|
||||
# redirect standard file descriptors.
|
||||
sys.stdout.flush()
|
||||
sys.stderr.flush()
|
||||
si = open(os.devnull, 'r')
|
||||
|
|
@ -78,55 +77,50 @@ class Daemon:
|
|||
os.dup2(so.fileno(), sys.stdout.fileno())
|
||||
os.dup2(se.fileno(), sys.stderr.fileno())
|
||||
|
||||
# Write pidfile
|
||||
atexit.register(self.del_pid)
|
||||
# write pid file.
|
||||
atexit.register(self.delete_pid)
|
||||
|
||||
pid = str(os.getpid())
|
||||
with open(self.pid_file, 'w+') as f:
|
||||
with open(self.__pid_file, 'w+') as f:
|
||||
f.write(pid + '\n')
|
||||
|
||||
def del_pid(self):
|
||||
os.remove(self.pid_file)
|
||||
def delete_pid(self):
|
||||
os.remove(self.__pid_file)
|
||||
|
||||
def start(self):
|
||||
"""
|
||||
Start the daemon.
|
||||
"""
|
||||
# start the daemon. check for a pidfile to see if the daemon already runs before.
|
||||
|
||||
# Check for a pidfile to see if the daemon already runs
|
||||
try:
|
||||
with open(self.pid_file, 'r') as pf:
|
||||
with open(self.__pid_file, 'r') as pf:
|
||||
pid = int(pf.read().strip())
|
||||
except IOError:
|
||||
pid = None
|
||||
|
||||
if pid:
|
||||
message = 'pid_file {0} already exist. daemon already running?'
|
||||
logger.error(str(message.format(self.pid_file)))
|
||||
logger.error(str(message.format(self.__pid_file)))
|
||||
sys.exit(1)
|
||||
|
||||
# Start the daemon
|
||||
# start the daemon.
|
||||
self.daemonize()
|
||||
self.run()
|
||||
|
||||
def stop(self):
|
||||
"""
|
||||
Stop the daemon.
|
||||
"""
|
||||
# stop the daemon.
|
||||
|
||||
# Get the pid from the pid_file
|
||||
# get the pid from the pid file.
|
||||
try:
|
||||
with open(self.pid_file, 'r') as pf:
|
||||
with open(self.__pid_file, 'r') as pf:
|
||||
pid = int(pf.read().strip())
|
||||
except IOError:
|
||||
pid = None
|
||||
|
||||
if not pid:
|
||||
message = 'pid_file {0} does not exist. daemon not running?'
|
||||
logger.error(str(message.format(self.pid_file)))
|
||||
logger.error(str(message.format(self.__pid_file)))
|
||||
return # not an error in a restart
|
||||
|
||||
# Try killing the daemon process
|
||||
# try killing the daemon process.
|
||||
try:
|
||||
while 1:
|
||||
os.kill(pid, signal.SIGTERM)
|
||||
|
|
@ -134,23 +128,20 @@ class Daemon:
|
|||
except OSError as err:
|
||||
e = str(err.args)
|
||||
if e.find('no such process') > 0:
|
||||
if os.path.exists(self.pid_file):
|
||||
os.remove(self.pid_file)
|
||||
if os.path.exists(self.__pid_file):
|
||||
os.remove(self.__pid_file)
|
||||
else:
|
||||
logger.error(str(err.args))
|
||||
sys.exit(1)
|
||||
|
||||
def restart(self):
|
||||
"""
|
||||
Restart the daemon.
|
||||
"""
|
||||
# restart the daemon.
|
||||
|
||||
self.stop()
|
||||
self.start()
|
||||
|
||||
def run(self):
|
||||
"""
|
||||
You should override this method when you subclass Daemon.
|
||||
|
||||
It will be called after the process has been daemonized by
|
||||
start() or restart().
|
||||
you should override this method when you subclass daemon. it will be called after the
|
||||
process has been daemonized by start() or restart().
|
||||
"""
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue