mirror of
https://github.com/codex-storage/deluge.git
synced 2025-01-12 04:24:27 +00:00
Fix daemon log being clobbered by running another instance with same config dir
Also includes small fixes and code cleanup
This commit is contained in:
parent
86ac98b9f9
commit
f8fbda97cd
@ -31,100 +31,98 @@
|
||||
# this exception statement from your version. If you delete this exception
|
||||
# statement from all source files in the program, then also delete it here.
|
||||
#
|
||||
|
||||
import os
|
||||
import logging
|
||||
from twisted.internet import reactor
|
||||
|
||||
import deluge.component as component
|
||||
import deluge.configmanager
|
||||
import deluge.common
|
||||
from deluge.configmanager import get_config_dir
|
||||
from deluge.common import get_version, windows_check
|
||||
from deluge.core.rpcserver import RPCServer, export
|
||||
from deluge.error import DaemonRunningError
|
||||
from deluge.core.core import Core
|
||||
|
||||
if windows_check():
|
||||
from win32api import SetConsoleCtrlHandler
|
||||
from win32con import CTRL_CLOSE_EVENT, CTRL_SHUTDOWN_EVENT
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def check_running_daemon(pid_file):
|
||||
"""Check for another running instance of the daemon using the same pid file"""
|
||||
if os.path.isfile(pid_file):
|
||||
# Get the PID and the port of the supposedly running daemon
|
||||
with open(pid_file) as _file:
|
||||
(pid, port) = _file.readline().strip().split(";")
|
||||
try:
|
||||
pid, port = int(pid), int(port)
|
||||
except ValueError:
|
||||
pid, port = None, None
|
||||
|
||||
def process_running(pid):
|
||||
if windows_check():
|
||||
from win32process import EnumProcesses
|
||||
return pid in EnumProcesses()
|
||||
else:
|
||||
# We can just use os.kill on UNIX to test if the process is running
|
||||
try:
|
||||
os.kill(pid, 0)
|
||||
except OSError:
|
||||
return False
|
||||
else:
|
||||
return True
|
||||
|
||||
if pid is not None and process_running(pid):
|
||||
# Ensure it's a deluged process by trying to open a socket to it's port.
|
||||
import socket
|
||||
_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
try:
|
||||
_socket.connect(("127.0.0.1", port))
|
||||
except socket.error:
|
||||
# Can't connect, so it must not be a deluged process..
|
||||
pass
|
||||
else:
|
||||
# This is a deluged!
|
||||
_socket.close()
|
||||
raise DaemonRunningError("Deluge daemon already running with this config directory!")
|
||||
|
||||
|
||||
class Daemon(object):
|
||||
def __init__(self, options=None, args=None, classic=False):
|
||||
# Check for another running instance of the daemon
|
||||
pid_file = deluge.configmanager.get_config_dir("deluged.pid")
|
||||
if os.path.isfile(pid_file):
|
||||
# Get the PID and the port of the supposedly running daemon
|
||||
with open(pid_file) as _file:
|
||||
(pid, port) = _file.readline().strip().split(";")
|
||||
try:
|
||||
pid, port = int(pid), int(port)
|
||||
except ValueError:
|
||||
pid, port = None, None
|
||||
log.info("Deluge daemon %s", get_version())
|
||||
log.debug("options: %s", options)
|
||||
log.debug("args: %s", args)
|
||||
|
||||
def process_running(pid):
|
||||
if deluge.common.windows_check():
|
||||
import win32process
|
||||
return pid in win32process.EnumProcesses()
|
||||
else:
|
||||
# We can just use os.kill on UNIX to test if the process is running
|
||||
try:
|
||||
os.kill(pid, 0)
|
||||
except OSError:
|
||||
return False
|
||||
else:
|
||||
return True
|
||||
|
||||
if pid is not None and process_running(pid):
|
||||
# Ensure it's a deluged process by trying to open a socket to it's port.
|
||||
import socket
|
||||
_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
try:
|
||||
_socket.connect(("127.0.0.1", port))
|
||||
except socket.error:
|
||||
# Can't connect, so it must not be a deluged process..
|
||||
pass
|
||||
else:
|
||||
# This is a deluged!
|
||||
_socket.close()
|
||||
raise DaemonRunningError("Deluge daemon already running with this config directory!")
|
||||
pid_file = get_config_dir("deluged.pid")
|
||||
check_running_daemon(pid_file)
|
||||
|
||||
# Twisted catches signals to terminate, so just have it call the shutdown method.
|
||||
reactor.addSystemEventTrigger("before", "shutdown", self._shutdown)
|
||||
|
||||
# Catch some Windows specific signals
|
||||
if deluge.common.windows_check():
|
||||
from win32api import SetConsoleCtrlHandler
|
||||
from win32con import CTRL_CLOSE_EVENT, CTRL_SHUTDOWN_EVENT
|
||||
|
||||
if windows_check():
|
||||
def win_handler(ctrl_type):
|
||||
log.debug("ctrl_type: %s", ctrl_type)
|
||||
log.debug("windows handler ctrl_type: %s", ctrl_type)
|
||||
if ctrl_type == CTRL_CLOSE_EVENT or ctrl_type == CTRL_SHUTDOWN_EVENT:
|
||||
self._shutdown()
|
||||
return 1
|
||||
SetConsoleCtrlHandler(win_handler)
|
||||
|
||||
version = deluge.common.get_version()
|
||||
|
||||
log.info("Deluge daemon %s", version)
|
||||
log.debug("options: %s", options)
|
||||
log.debug("args: %s", args)
|
||||
# Set the config directory
|
||||
if options and options.config:
|
||||
deluge.configmanager.set_config_dir(options.config)
|
||||
|
||||
listen_interface = None
|
||||
if options and options.listen_interface:
|
||||
listen_interface = options.listen_interface
|
||||
else:
|
||||
listen_interface = ""
|
||||
|
||||
from deluge.core.core import Core
|
||||
# Start the core as a thread and join it until it's done
|
||||
self.core = Core(listen_interface=listen_interface)
|
||||
|
||||
port = self.core.config["daemon_port"]
|
||||
if options and options.port:
|
||||
port = options.port
|
||||
|
||||
interface = None
|
||||
if options and options.ui_interface:
|
||||
interface = options.ui_interface
|
||||
else:
|
||||
interface = ""
|
||||
|
||||
self.rpcserver = RPCServer(
|
||||
port=port,
|
||||
@ -141,31 +139,31 @@ class Daemon(object):
|
||||
component.start("PreferencesManager")
|
||||
|
||||
if not classic:
|
||||
log.info("Deluge daemon starting...")
|
||||
|
||||
# Create pid file to track if deluged is running, also includes the port number.
|
||||
log.debug("Creating pid file: %s", pid_file)
|
||||
open(pid_file, "wb").write("%s;%s\n" % (os.getpid(), port))
|
||||
pid = os.getpid()
|
||||
log.debug("Storing pid %s & port %s in: %s", pid, port, pid_file)
|
||||
with open(pid_file, "wb") as _file:
|
||||
_file.write("%s;%s\n" % (pid, port))
|
||||
|
||||
component.start()
|
||||
|
||||
try:
|
||||
log.info("Deluge daemon starting...")
|
||||
reactor.run()
|
||||
finally:
|
||||
try:
|
||||
log.debug("Removing pid file: %s", pid_file)
|
||||
os.remove(pid_file)
|
||||
except OSError, ex:
|
||||
log.error("Error removing pid file: %s", ex)
|
||||
log.debug("Remove pid file: %s", pid_file)
|
||||
os.remove(pid_file)
|
||||
log.info("Deluge daemon shutdown successfully")
|
||||
|
||||
@export()
|
||||
def shutdown(self, *args, **kwargs):
|
||||
log.debug("Delgue daemon shutdown requested...")
|
||||
log.debug("Deluge daemon shutdown requested...")
|
||||
reactor.callLater(0, reactor.stop)
|
||||
|
||||
def _shutdown(self, *args, **kwargs):
|
||||
log.info("Deluge daemon shutting down, waiting for components to shutdown...")
|
||||
d = component.shutdown()
|
||||
return d
|
||||
return component.shutdown()
|
||||
|
||||
@export()
|
||||
def get_method_list(self):
|
||||
@ -187,4 +185,4 @@ class Daemon(object):
|
||||
return False
|
||||
|
||||
auth_level = self.rpcserver.get_session_auth_level()
|
||||
return auth_level >= self.rpcserver.get_rpc_auth_level()
|
||||
return auth_level >= self.rpcserver.get_rpc_auth_level(rpc)
|
||||
|
@ -48,6 +48,9 @@ from errno import EEXIST
|
||||
|
||||
from deluge.log import setupLogger
|
||||
import deluge.error
|
||||
import deluge.common
|
||||
import deluge.configmanager
|
||||
|
||||
|
||||
def version_callback(option, opt_str, value, parser):
|
||||
print os.path.basename(sys.argv[0]) + ": " + deluge.common.get_version()
|
||||
@ -58,9 +61,9 @@ def version_callback(option, opt_str, value, parser):
|
||||
pass
|
||||
raise SystemExit
|
||||
|
||||
|
||||
def start_ui():
|
||||
"""Entry point for ui script"""
|
||||
import deluge.common
|
||||
deluge.common.setup_translations()
|
||||
|
||||
# Setup the argument parser
|
||||
@ -106,7 +109,7 @@ def start_ui():
|
||||
# Try to create the config folder if it doesn't exist
|
||||
try:
|
||||
os.makedirs(options.config)
|
||||
except Exception, e:
|
||||
except OSError:
|
||||
pass
|
||||
elif not os.path.isdir(options.config):
|
||||
log.error("Config option needs to be a directory!")
|
||||
@ -116,7 +119,6 @@ def start_ui():
|
||||
os.makedirs(deluge.common.get_default_config_dir())
|
||||
|
||||
if options.default_ui:
|
||||
import deluge.configmanager
|
||||
if options.config:
|
||||
deluge.configmanager.set_config_dir(options.config)
|
||||
|
||||
@ -137,9 +139,9 @@ def start_ui():
|
||||
log.info("Starting ui..")
|
||||
UI(options, args, options.args)
|
||||
|
||||
|
||||
def start_daemon():
|
||||
"""Entry point for daemon script"""
|
||||
import deluge.common
|
||||
deluge.common.setup_translations()
|
||||
|
||||
if 'dev' not in deluge.common.get_version():
|
||||
@ -185,6 +187,21 @@ this should be an IP address", metavar="IFACE",
|
||||
# Get the options and args from the OptionParser
|
||||
(options, args) = parser.parse_args()
|
||||
|
||||
if options.config:
|
||||
if not deluge.configmanager.set_config_dir(options.config):
|
||||
print "There was an error setting the config directory! Exiting..."
|
||||
sys.exit(1)
|
||||
|
||||
# Check for any daemons running with this same config
|
||||
from deluge.core.daemon import check_running_daemon
|
||||
pid_file = deluge.configmanager.get_config_dir("deluged.pid")
|
||||
try:
|
||||
check_running_daemon(pid_file)
|
||||
except deluge.error.DaemonRunningError:
|
||||
print "You cannot run multiple daemons with the same config directory set."
|
||||
print "If you believe this is an error, you can force a start by deleting: %s" % pid_file
|
||||
sys.exit(1)
|
||||
|
||||
# Setup the logger
|
||||
if options.quiet:
|
||||
options.loglevel = "none"
|
||||
@ -192,36 +209,25 @@ this should be an IP address", metavar="IFACE",
|
||||
# Try to create the logfile's directory if it doesn't exist
|
||||
try:
|
||||
os.makedirs(os.path.abspath(os.path.dirname(options.logfile)))
|
||||
except OSError, e:
|
||||
if e.errno != EEXIST:
|
||||
print "There was an error creating the log directory, exiting... (%s)" % e
|
||||
except OSError as ex:
|
||||
if ex.errno != EEXIST:
|
||||
print "There was an error creating the log directory, exiting... (%s)" % ex
|
||||
sys.exit(1)
|
||||
|
||||
logfile_mode = 'w'
|
||||
if options.rotate_logs:
|
||||
logfile_mode = 'a'
|
||||
setupLogger(level=options.loglevel, filename=options.logfile, filemode=logfile_mode)
|
||||
log = getLogger(__name__)
|
||||
|
||||
import deluge.configmanager
|
||||
if options.config:
|
||||
if not deluge.configmanager.set_config_dir(options.config):
|
||||
log.error("There was an error setting the config directory! Exiting...")
|
||||
sys.exit(1)
|
||||
|
||||
# Sets the options.logfile to point to the default location
|
||||
def open_logfile():
|
||||
if not options.logfile:
|
||||
options.logfile = deluge.configmanager.get_config_dir("deluged.log")
|
||||
file_handler = FileHandler(options.logfile)
|
||||
log.addHandler(file_handler)
|
||||
|
||||
# Writes out a pidfile if necessary
|
||||
def write_pidfile():
|
||||
if options.pidfile:
|
||||
open(options.pidfile, "wb").write("%s\n" % os.getpid())
|
||||
# If no logfile specified add logging to default location (as well as stdout)
|
||||
if not options.logfile:
|
||||
options.logfile = deluge.configmanager.get_config_dir("deluged.log")
|
||||
file_handler = FileHandler(options.logfile)
|
||||
log.addHandler(file_handler)
|
||||
|
||||
# If the donot daemonize is set, then we just skip the forking
|
||||
if not (deluge.common.windows_check() or deluge.common.osx_check() or options.donot):
|
||||
if not (options.donot or deluge.common.windows_check() or deluge.common.osx_check()):
|
||||
if os.fork():
|
||||
# We've forked and this is now the parent process, so die!
|
||||
os._exit(0)
|
||||
@ -231,7 +237,9 @@ this should be an IP address", metavar="IFACE",
|
||||
os._exit(0)
|
||||
|
||||
# Write pid file before chuid
|
||||
write_pidfile()
|
||||
if options.pidfile:
|
||||
with open(options.pidfile, "wb") as _file:
|
||||
_file.write("%s\n" % os.getpid())
|
||||
|
||||
if not deluge.common.windows_check():
|
||||
if options.user:
|
||||
@ -245,20 +253,16 @@ this should be an IP address", metavar="IFACE",
|
||||
options.group = grp.getgrnam(options.group)[2]
|
||||
os.setuid(options.group)
|
||||
|
||||
open_logfile()
|
||||
|
||||
def run_daemon(options, args):
|
||||
from deluge.core.daemon import Daemon
|
||||
try:
|
||||
from deluge.core.daemon import Daemon
|
||||
Daemon(options, args)
|
||||
except deluge.error.DaemonRunningError, e:
|
||||
log.error(e)
|
||||
log.error("You cannot run multiple daemons with the same config directory set.")
|
||||
log.error("If you believe this is an error, you can force a start by deleting %s.", deluge.configmanager.get_config_dir("deluged.pid"))
|
||||
sys.exit(1)
|
||||
except Exception, e:
|
||||
log.exception(e)
|
||||
except Exception as ex:
|
||||
log.exception(ex)
|
||||
sys.exit(1)
|
||||
finally:
|
||||
if options.pidfile:
|
||||
os.remove(options.pidfile)
|
||||
|
||||
if options.profile:
|
||||
import cProfile
|
||||
|
Loading…
x
Reference in New Issue
Block a user