Deamonize later
This commit is contained in:
parent
302086d75b
commit
b7e12037de
@ -1,5 +1,5 @@
|
|||||||
# Nemubot is a smart and modulable IM bot.
|
# Nemubot is a smart and modulable IM bot.
|
||||||
# Copyright (C) 2012-2016 Mercier Pierre-Olivier
|
# Copyright (C) 2012-2017 Mercier Pierre-Olivier
|
||||||
#
|
#
|
||||||
# This program is free software: you can redistribute it and/or modify
|
# This program is free software: you can redistribute it and/or modify
|
||||||
# it under the terms of the GNU Affero General Public License as published by
|
# it under the terms of the GNU Affero General Public License as published by
|
||||||
@ -74,28 +74,6 @@ 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)]
|
||||||
|
|
||||||
# Check if an instance is already launched
|
|
||||||
if args.pidfile is not None and os.path.isfile(args.pidfile):
|
|
||||||
with open(args.pidfile, "r") as f:
|
|
||||||
pid = int(f.readline())
|
|
||||||
try:
|
|
||||||
os.kill(pid, 0)
|
|
||||||
except OSError:
|
|
||||||
pass
|
|
||||||
else:
|
|
||||||
from nemubot import attach
|
|
||||||
sys.exit(attach(pid, args.socketfile))
|
|
||||||
|
|
||||||
# Daemonize
|
|
||||||
if not args.debug:
|
|
||||||
from nemubot import daemonize
|
|
||||||
daemonize()
|
|
||||||
|
|
||||||
# Store PID to pidfile
|
|
||||||
if args.pidfile is not None:
|
|
||||||
with open(args.pidfile, "w+") as f:
|
|
||||||
f.write(str(os.getpid()))
|
|
||||||
|
|
||||||
# Setup logging interface
|
# Setup logging interface
|
||||||
import logging
|
import logging
|
||||||
logger = logging.getLogger("nemubot")
|
logger = logging.getLogger("nemubot")
|
||||||
@ -115,6 +93,18 @@ def main():
|
|||||||
fh.setFormatter(formatter)
|
fh.setFormatter(formatter)
|
||||||
logger.addHandler(fh)
|
logger.addHandler(fh)
|
||||||
|
|
||||||
|
# Check if an instance is already launched
|
||||||
|
if args.pidfile is not None and os.path.isfile(args.pidfile):
|
||||||
|
with open(args.pidfile, "r") as f:
|
||||||
|
pid = int(f.readline())
|
||||||
|
try:
|
||||||
|
os.kill(pid, 0)
|
||||||
|
except OSError:
|
||||||
|
pass
|
||||||
|
else:
|
||||||
|
from nemubot import attach
|
||||||
|
sys.exit(attach(pid, args.socketfile))
|
||||||
|
|
||||||
# Add modules dir paths
|
# Add modules dir paths
|
||||||
modules_paths = list()
|
modules_paths = list()
|
||||||
for path in args.modules_path:
|
for path in args.modules_path:
|
||||||
@ -149,6 +139,17 @@ def main():
|
|||||||
for module in args.module:
|
for module in args.module:
|
||||||
__import__(module)
|
__import__(module)
|
||||||
|
|
||||||
|
if args.socketfile:
|
||||||
|
from nemubot.server.socket import UnixSocketListener
|
||||||
|
context.add_server(UnixSocketListener(new_server_cb=context.add_server,
|
||||||
|
location=args.socketfile,
|
||||||
|
name="master_socket"))
|
||||||
|
|
||||||
|
# Daemonize
|
||||||
|
if not args.debug:
|
||||||
|
from nemubot import daemonize
|
||||||
|
daemonize()
|
||||||
|
|
||||||
# Signals handling
|
# Signals handling
|
||||||
def sigtermhandler(signum, frame):
|
def sigtermhandler(signum, frame):
|
||||||
"""On SIGTERM and SIGINT, quit nicely"""
|
"""On SIGTERM and SIGINT, quit nicely"""
|
||||||
@ -182,11 +183,10 @@ def main():
|
|||||||
"".join(traceback.format_stack(stack)))
|
"".join(traceback.format_stack(stack)))
|
||||||
signal.signal(signal.SIGUSR1, sigusr1handler)
|
signal.signal(signal.SIGUSR1, sigusr1handler)
|
||||||
|
|
||||||
if args.socketfile:
|
# Store PID to pidfile
|
||||||
from nemubot.server.socket import UnixSocketListener
|
if args.pidfile is not None:
|
||||||
context.add_server(UnixSocketListener(new_server_cb=context.add_server,
|
with open(args.pidfile, "w+") as f:
|
||||||
location=args.socketfile,
|
f.write(str(os.getpid()))
|
||||||
name="master_socket"))
|
|
||||||
|
|
||||||
# context can change when performing an hotswap, always join the latest context
|
# context can change when performing an hotswap, always join the latest context
|
||||||
oldcontext = None
|
oldcontext = None
|
||||||
|
Loading…
Reference in New Issue
Block a user