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