Merge pull request #588 from Unrud/daemon
Daemon: Exit first process after server is ready
This commit is contained in:
commit
f2bfcfd406
@ -114,26 +114,35 @@ def run():
|
||||
exit(1)
|
||||
|
||||
|
||||
def serve(configuration, logger):
|
||||
"""Serve radicale from configuration."""
|
||||
# Fork if Radicale is launched as daemon
|
||||
if configuration.getboolean("server", "daemon"):
|
||||
def daemonize(configuration, logger):
|
||||
"""Fork and decouple if Radicale is configured as daemon."""
|
||||
# Check and create PID file in a race-free manner
|
||||
if configuration.get("server", "pid"):
|
||||
try:
|
||||
pid_path = os.path.abspath(os.path.expanduser(
|
||||
configuration.get("server", "pid")))
|
||||
pid_fd = os.open(
|
||||
configuration.get("server", "pid"),
|
||||
os.O_CREAT | os.O_EXCL | os.O_WRONLY)
|
||||
except:
|
||||
raise OSError(
|
||||
"PID file exists: %s" % configuration.get("server", "pid"))
|
||||
pid_path, os.O_CREAT | os.O_EXCL | os.O_WRONLY)
|
||||
except OSError as e:
|
||||
raise OSError("PID file exists: %s" %
|
||||
configuration.get("server", "pid")) from e
|
||||
pid = os.fork()
|
||||
if pid:
|
||||
sys.exit()
|
||||
# Write PID
|
||||
if configuration.get("server", "pid"):
|
||||
with os.fdopen(pid_fd, "w") as pid_file:
|
||||
pid_file.write(str(os.getpid()))
|
||||
pid_file.write(str(pid))
|
||||
sys.exit()
|
||||
if configuration.get("server", "pid"):
|
||||
os.close(pid_fd)
|
||||
|
||||
# Register exit function
|
||||
def cleanup():
|
||||
"""Remove the PID files."""
|
||||
logger.debug("Cleaning up")
|
||||
# Remove PID file
|
||||
os.unlink(pid_path)
|
||||
atexit.register(cleanup)
|
||||
# Decouple environment
|
||||
os.chdir("/")
|
||||
os.setsid()
|
||||
@ -143,16 +152,9 @@ def serve(configuration, logger):
|
||||
os.dup2(null_out.fileno(), sys.stdout.fileno())
|
||||
os.dup2(null_out.fileno(), sys.stderr.fileno())
|
||||
|
||||
# Register exit function
|
||||
def cleanup():
|
||||
"""Remove the PID files."""
|
||||
logger.debug("Cleaning up")
|
||||
# Remove PID file
|
||||
if (configuration.get("server", "pid") and
|
||||
configuration.getboolean("server", "daemon")):
|
||||
os.unlink(configuration.get("server", "pid"))
|
||||
|
||||
atexit.register(cleanup)
|
||||
def serve(configuration, logger):
|
||||
"""Serve radicale from configuration."""
|
||||
logger.info("Starting Radicale")
|
||||
|
||||
# Create collection servers
|
||||
@ -228,6 +230,8 @@ def serve(configuration, logger):
|
||||
else:
|
||||
# Fallback to busy waiting
|
||||
select_timeout = 1.0
|
||||
if configuration.getboolean("server", "daemon"):
|
||||
daemonize(configuration, logger)
|
||||
logger.debug("Radicale server ready")
|
||||
while not shutdown_program:
|
||||
try:
|
||||
|
Loading…
Reference in New Issue
Block a user