Implement PID file support in nntp.tiny.daemon
This commit is contained in:
parent
c53e45ca49
commit
07689f87cb
3 changed files with 56 additions and 8 deletions
|
@ -6,6 +6,7 @@ import argparse
|
||||||
from nntp.tiny.config import Config
|
from nntp.tiny.config import Config
|
||||||
from nntp.tiny.db import Database
|
from nntp.tiny.db import Database
|
||||||
from nntp.tiny.server import Server
|
from nntp.tiny.server import Server
|
||||||
|
from nntp.tiny.daemon import Daemon
|
||||||
|
|
||||||
parser = argparse.ArgumentParser(description='Tiny NNTP server')
|
parser = argparse.ArgumentParser(description='Tiny NNTP server')
|
||||||
parser.add_argument('--daemon', '-d', action='store_true', help='Run NNTP server as daemon in background')
|
parser.add_argument('--daemon', '-d', action='store_true', help='Run NNTP server as daemon in background')
|
||||||
|
@ -16,9 +17,6 @@ config = Config.load()
|
||||||
server = Server(config)
|
server = Server(config)
|
||||||
|
|
||||||
if args.daemon:
|
if args.daemon:
|
||||||
pid = os.fork()
|
Daemon.init(config)
|
||||||
|
|
||||||
if pid > 0:
|
|
||||||
exit(0)
|
|
||||||
|
|
||||||
server.run()
|
server.run()
|
||||||
|
|
|
@ -4,11 +4,35 @@ import configparser
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
|
|
||||||
class ConfigException(Exception):
|
class ConfigException(Exception):
|
||||||
def __init__(self, path: str):
|
pass
|
||||||
self.path = path
|
|
||||||
|
class ConfigSectionException(ConfigException):
|
||||||
|
def __init__(self, section: str):
|
||||||
|
self.section = section
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return "Unable to locate file '" + self.path + "'"
|
return "Missing configuration section '%s'" % (
|
||||||
|
self.section
|
||||||
|
)
|
||||||
|
|
||||||
|
class ConfigValueException(ConfigException):
|
||||||
|
def __init__(self, section: str, value: str):
|
||||||
|
self.section = section
|
||||||
|
self.value = value
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return "Missing configuration value '%s' in section '%s'" % (
|
||||||
|
self.value, self.section
|
||||||
|
)
|
||||||
|
|
||||||
|
class ConfigFileException(ConfigException):
|
||||||
|
def __init__(self, paths: list):
|
||||||
|
self.paths = paths
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return "Unable to locate configuration file in: %s" % (
|
||||||
|
", ".join(self.paths)
|
||||||
|
)
|
||||||
|
|
||||||
class Config():
|
class Config():
|
||||||
SEARCH_PATHS = [
|
SEARCH_PATHS = [
|
||||||
|
@ -29,7 +53,7 @@ class Config():
|
||||||
path = Config.find()
|
path = Config.find()
|
||||||
|
|
||||||
if path is None:
|
if path is None:
|
||||||
raise ConfigException(path)
|
raise ConfigFileException(Config.SEARCH_PATHS)
|
||||||
|
|
||||||
parser = configparser.ConfigParser()
|
parser = configparser.ConfigParser()
|
||||||
parser.read(path)
|
parser.read(path)
|
||||||
|
|
26
lib/nntp/tiny/daemon.py
Normal file
26
lib/nntp/tiny/daemon.py
Normal file
|
@ -0,0 +1,26 @@
|
||||||
|
import os
|
||||||
|
import signal
|
||||||
|
import configparser
|
||||||
|
|
||||||
|
from nntp.tiny.config import (ConfigSectionException,
|
||||||
|
ConfigValueException)
|
||||||
|
|
||||||
|
class Daemon():
|
||||||
|
def init(config: configparser.ConfigParser):
|
||||||
|
if not config.has_section('daemon'):
|
||||||
|
raise ConfigSectionException('daemon')
|
||||||
|
|
||||||
|
if not config.has_option('daemon', 'pidfile'):
|
||||||
|
raise ConfigValueException('daemon', 'pidfile')
|
||||||
|
|
||||||
|
pidfile = config.get('daemon', 'pidfile')
|
||||||
|
|
||||||
|
pid = os.fork()
|
||||||
|
|
||||||
|
if pid > 0:
|
||||||
|
exit(0)
|
||||||
|
|
||||||
|
with open(pidfile, 'w') as fh:
|
||||||
|
print(str(os.getpid()), file=fh)
|
||||||
|
|
||||||
|
signal.signal(signal.SIGTERM, lambda s, f: os.unlink(pidfile))
|
Loading…
Add table
Reference in a new issue