mirror of
https://git.deluge-torrent.org/deluge
synced 2025-08-08 09:28:41 +00:00
Refactor daemon check process functions
This commit is contained in:
parent
3a8ed2e9cb
commit
58835eeb2e
3 changed files with 60 additions and 45 deletions
|
@ -1036,3 +1036,27 @@ def run_profiled(func, *args, **kwargs):
|
||||||
on_shutdown()
|
on_shutdown()
|
||||||
else:
|
else:
|
||||||
return func(*args)
|
return func(*args)
|
||||||
|
|
||||||
|
|
||||||
|
def is_process_running(pid):
|
||||||
|
"""
|
||||||
|
Verify if the supplied pid is a running process.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
pid (int): The pid to check.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
bool: True if pid is a running process, False otherwise.
|
||||||
|
|
||||||
|
"""
|
||||||
|
|
||||||
|
if windows_check():
|
||||||
|
from win32process import EnumProcesses
|
||||||
|
return pid in EnumProcesses()
|
||||||
|
else:
|
||||||
|
try:
|
||||||
|
os.kill(pid, 0)
|
||||||
|
except OSError:
|
||||||
|
return False
|
||||||
|
else:
|
||||||
|
return True
|
||||||
|
|
|
@ -11,11 +11,12 @@
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
|
import socket
|
||||||
|
|
||||||
from twisted.internet import reactor
|
from twisted.internet import reactor
|
||||||
|
|
||||||
import deluge.component as component
|
import deluge.component as component
|
||||||
from deluge.common import get_version, is_ip, windows_check
|
from deluge.common import get_version, is_ip, is_process_running, windows_check
|
||||||
from deluge.configmanager import get_config_dir
|
from deluge.configmanager import get_config_dir
|
||||||
from deluge.core.core import Core
|
from deluge.core.core import Core
|
||||||
from deluge.core.rpcserver import RPCServer, export
|
from deluge.core.rpcserver import RPCServer, export
|
||||||
|
@ -28,44 +29,36 @@ if windows_check():
|
||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
def check_running_daemon(pid_file):
|
def is_daemon_running(pid_file):
|
||||||
"""Check for another running instance of the daemon using the same pid file."""
|
"""
|
||||||
if os.path.isfile(pid_file):
|
Check for another running instance of the daemon using the same pid file.
|
||||||
# Get the PID and the port of the supposedly running daemon
|
|
||||||
|
Args:
|
||||||
|
pid_file: The location of the file with pid, port values.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
bool: True is daemon is running, False otherwise.
|
||||||
|
|
||||||
|
"""
|
||||||
|
|
||||||
|
try:
|
||||||
with open(pid_file) as _file:
|
with open(pid_file) as _file:
|
||||||
(pid, port) = _file.readline().strip().split(";")
|
pid, port = [int(x) for x in _file.readline().strip().split(";")]
|
||||||
try:
|
except EnvironmentError:
|
||||||
pid, port = int(pid), int(port)
|
|
||||||
except ValueError:
|
|
||||||
pid, port = None, None
|
|
||||||
|
|
||||||
def process_running(pid):
|
|
||||||
"""Verify if pid is a running process."""
|
|
||||||
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
|
return False
|
||||||
else:
|
|
||||||
return True
|
|
||||||
|
|
||||||
if pid is not None and process_running(pid):
|
if is_process_running(pid):
|
||||||
# Ensure it's a deluged process by trying to open a socket to it's port.
|
# 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)
|
_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||||
try:
|
try:
|
||||||
_socket.connect(("127.0.0.1", port))
|
_socket.connect(("127.0.0.1", port))
|
||||||
except socket.error:
|
except socket.error:
|
||||||
# Can't connect, so it must not be a deluged process..
|
# Can't connect, so pid is not a deluged process.
|
||||||
pass
|
return False
|
||||||
else:
|
else:
|
||||||
# This is a deluged!
|
# This is a deluged process!
|
||||||
_socket.close()
|
_socket.close()
|
||||||
raise DaemonRunningError("Deluge daemon already running with this config directory!")
|
return True
|
||||||
|
|
||||||
|
|
||||||
class Daemon(object):
|
class Daemon(object):
|
||||||
|
@ -86,7 +79,8 @@ class Daemon(object):
|
||||||
self.classic = classic
|
self.classic = classic
|
||||||
self.pid_file = get_config_dir("deluged.pid")
|
self.pid_file = get_config_dir("deluged.pid")
|
||||||
log.info("Deluge daemon %s", get_version())
|
log.info("Deluge daemon %s", get_version())
|
||||||
check_running_daemon(self.pid_file)
|
if is_daemon_running(self.pid_file):
|
||||||
|
raise DaemonRunningError("Deluge daemon already running with this config directory!")
|
||||||
|
|
||||||
# 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)
|
||||||
|
|
|
@ -15,7 +15,6 @@ from logging import FileHandler, getLogger
|
||||||
|
|
||||||
from deluge.common import run_profiled
|
from deluge.common import run_profiled
|
||||||
from deluge.configmanager import get_config_dir
|
from deluge.configmanager import get_config_dir
|
||||||
from deluge.error import DaemonRunningError
|
|
||||||
from deluge.ui.baseargparser import BaseArgParser
|
from deluge.ui.baseargparser import BaseArgParser
|
||||||
from deluge.ui.util import lang
|
from deluge.ui.util import lang
|
||||||
|
|
||||||
|
@ -53,13 +52,11 @@ def start_daemon(skip_start=False):
|
||||||
options = parser.parse_args()
|
options = parser.parse_args()
|
||||||
|
|
||||||
# Check for any daemons running with this same config
|
# Check for any daemons running with this same config
|
||||||
from deluge.core.daemon import check_running_daemon
|
from deluge.core.daemon import is_daemon_running
|
||||||
pid_file = get_config_dir("deluged.pid")
|
pid_file = get_config_dir("deluged.pid")
|
||||||
try:
|
if is_daemon_running(pid_file):
|
||||||
check_running_daemon(pid_file)
|
print("Cannot run multiple daemons using the same config directory.\n"
|
||||||
except DaemonRunningError:
|
"If you believe this is an error, you can force a start by deleting: %s" % pid_file)
|
||||||
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)
|
sys.exit(1)
|
||||||
|
|
||||||
log = getLogger(__name__)
|
log = getLogger(__name__)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue