daemonize: fork client before loading context
This commit is contained in:
parent
28d4e507eb
commit
30ec912162
@ -39,10 +39,14 @@ def requires_version(min=None, max=None):
|
|||||||
"but this is nemubot v%s." % (str(max), __version__))
|
"but this is nemubot v%s." % (str(max), __version__))
|
||||||
|
|
||||||
|
|
||||||
def attach(pid, socketfile):
|
def attach(pidfile, socketfile):
|
||||||
import socket
|
import socket
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
# Read PID from pidfile
|
||||||
|
with open(pidfile, "r") as f:
|
||||||
|
pid = int(f.readline())
|
||||||
|
|
||||||
print("nemubot is launched with PID %d. Attaching to Unix socket at: %s" % (pid, socketfile))
|
print("nemubot is launched with PID %d. Attaching to Unix socket at: %s" % (pid, socketfile))
|
||||||
|
|
||||||
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
|
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
|
||||||
@ -106,28 +110,13 @@ def attach(pid, socketfile):
|
|||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
|
||||||
def daemonize(socketfile=None, autoattach=True):
|
def daemonize(socketfile=None):
|
||||||
"""Detach the running process to run as a daemon
|
"""Detach the running process to run as a daemon
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
if socketfile is not None:
|
|
||||||
try:
|
|
||||||
pid = os.fork()
|
|
||||||
if pid > 0:
|
|
||||||
if autoattach:
|
|
||||||
import time
|
|
||||||
os.waitpid(pid, 0)
|
|
||||||
time.sleep(1)
|
|
||||||
sys.exit(attach(pid, socketfile))
|
|
||||||
else:
|
|
||||||
sys.exit(0)
|
|
||||||
except OSError as err:
|
|
||||||
sys.stderr.write("Unable to fork: %s\n" % err)
|
|
||||||
sys.exit(1)
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
pid = os.fork()
|
pid = os.fork()
|
||||||
if pid > 0:
|
if pid > 0:
|
||||||
|
@ -77,6 +77,20 @@ def main():
|
|||||||
args.files = [x for x in map(os.path.abspath, args.files)]
|
args.files = [x for x in map(os.path.abspath, args.files)]
|
||||||
args.modules_path = [x for x in map(os.path.abspath, args.modules_path)]
|
args.modules_path = [x for x in map(os.path.abspath, args.modules_path)]
|
||||||
|
|
||||||
|
# Prepare the attached client, before setting other stuff
|
||||||
|
if not args.debug and not args.no_attach and args.socketfile is not None and args.pidfile is not None:
|
||||||
|
try:
|
||||||
|
pid = os.fork()
|
||||||
|
if pid > 0:
|
||||||
|
import time
|
||||||
|
os.waitpid(pid, 0)
|
||||||
|
time.sleep(1)
|
||||||
|
from nemubot import attach
|
||||||
|
sys.exit(attach(args.pidfile, args.socketfile))
|
||||||
|
except OSError as err:
|
||||||
|
sys.stderr.write("Unable to fork: %s\n" % err)
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
# Setup logging interface
|
# Setup logging interface
|
||||||
import logging
|
import logging
|
||||||
logger = logging.getLogger("nemubot")
|
logger = logging.getLogger("nemubot")
|
||||||
@ -106,7 +120,7 @@ def main():
|
|||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
from nemubot import attach
|
from nemubot import attach
|
||||||
sys.exit(attach(pid, args.socketfile))
|
sys.exit(attach(args.pidfile, args.socketfile))
|
||||||
|
|
||||||
# Add modules dir paths
|
# Add modules dir paths
|
||||||
modules_paths = list()
|
modules_paths = list()
|
||||||
@ -175,7 +189,7 @@ def main():
|
|||||||
# Daemonize
|
# Daemonize
|
||||||
if not args.debug:
|
if not args.debug:
|
||||||
from nemubot import daemonize
|
from nemubot import daemonize
|
||||||
daemonize(args.socketfile, not args.no_attach)
|
daemonize(args.socketfile)
|
||||||
|
|
||||||
# Signals handling
|
# Signals handling
|
||||||
def sigtermhandler(signum, frame):
|
def sigtermhandler(signum, frame):
|
||||||
|
Loading…
Reference in New Issue
Block a user