Clean-up signal handling since twisted.reactor handles it now

This commit is contained in:
Andrew Resch 2009-04-27 18:50:41 +00:00
commit 2ac545dec6
3 changed files with 37 additions and 40 deletions

View file

@ -22,8 +22,6 @@
# Boston, MA 02110-1301, USA. # Boston, MA 02110-1301, USA.
# #
import signal
import gettext import gettext
import locale import locale
import pkg_resources import pkg_resources
@ -51,12 +49,12 @@ class Daemon(object):
except Exception, e: except Exception, e:
log.error("Unable to initialize gettext/locale: %s", e) log.error("Unable to initialize gettext/locale: %s", e)
# Setup signals # Twisted catches signals to terminate, so just have it call the shutdown
signal.signal(signal.SIGINT, self.shutdown) # method.
signal.signal(signal.SIGTERM, self.shutdown) reactor.addSystemEventTrigger("after", "shutdown", self.shutdown)
if not deluge.common.windows_check():
signal.signal(signal.SIGHUP, self.shutdown) # Catch some Windows specific signals
else: if deluge.common.windows_check():
from win32api import SetConsoleCtrlHandler from win32api import SetConsoleCtrlHandler
from win32con import CTRL_CLOSE_EVENT from win32con import CTRL_CLOSE_EVENT
from win32con import CTRL_SHUTDOWN_EVENT from win32con import CTRL_SHUTDOWN_EVENT

View file

@ -33,7 +33,6 @@ import gobject
import gettext import gettext
import locale import locale
import pkg_resources import pkg_resources
import signal
import gtk, gtk.glade import gtk, gtk.glade
import deluge.component as component import deluge.component as component
@ -132,8 +131,10 @@ class GtkUI:
self.gnome_client.connect("die", self.shutdown) self.gnome_client.connect("die", self.shutdown)
except: except:
pass pass
signal.signal(signal.SIGINT, self.shutdown)
signal.signal(signal.SIGTERM, self.shutdown) # Twisted catches signals to terminate, so just have it call the shutdown
# method.
reactor.addSystemEventTrigger("after", "shutdown", self.shutdown)
if deluge.common.windows_check(): if deluge.common.windows_check():
from win32api import SetConsoleCtrlHandler from win32api import SetConsoleCtrlHandler

View file

@ -26,8 +26,6 @@ import os
import time import time
import locale import locale
import shutil import shutil
import signal
import signal
import urllib import urllib
import gettext import gettext
import hashlib import hashlib
@ -92,7 +90,7 @@ class Config(resource.Resource):
Writes out a javascript file that contains the WebUI configuration Writes out a javascript file that contains the WebUI configuration
available as Deluge.Config. available as Deluge.Config.
""" """
def render(self, request): def render(self, request):
return """Deluge = { return """Deluge = {
author: 'Damien Churchill <damoxc@gmail.com>', author: 'Damien Churchill <damoxc@gmail.com>',
@ -110,22 +108,22 @@ class Upload(resource.Resource):
""" """
Twisted Web resource to handle file uploads Twisted Web resource to handle file uploads
""" """
def render(self, request): def render(self, request):
""" """
Saves all uploaded files to the disk and returns a list of filenames, Saves all uploaded files to the disk and returns a list of filenames,
each on a new line. each on a new line.
""" """
# Block all other HTTP methods. # Block all other HTTP methods.
if request.method != "POST": if request.method != "POST":
request.setResponseCode(http.NOT_ALLOWED) request.setResponseCode(http.NOT_ALLOWED)
return "" return ""
if "file" not in request.args: if "file" not in request.args:
request.setResponseCode(http.OK) request.setResponseCode(http.OK)
return "" return ""
tempdir = os.path.join(tempfile.gettempdir(), "delugeweb") tempdir = os.path.join(tempfile.gettempdir(), "delugeweb")
if not os.path.isdir(tempdir): if not os.path.isdir(tempdir):
os.mkdir(tempdir) os.mkdir(tempdir)
@ -145,7 +143,7 @@ class Render(resource.Resource):
def getChild(self, path, request): def getChild(self, path, request):
request.render_file = path request.render_file = path
return self return self
def render(self, request): def render(self, request):
if not hasattr(request, "render_file"): if not hasattr(request, "render_file"):
request.setResponseCode(http.INTERNAL_SERVER_ERROR) request.setResponseCode(http.INTERNAL_SERVER_ERROR)
@ -159,11 +157,11 @@ class Render(resource.Resource):
class Tracker(resource.Resource): class Tracker(resource.Resource):
tracker_icons = TrackerIcons() tracker_icons = TrackerIcons()
def getChild(self, path, request): def getChild(self, path, request):
request.tracker_name = path request.tracker_name = path
return self return self
def render(self, request): def render(self, request):
headers = {} headers = {}
filename = self.tracker_icons.get(request.tracker_name) filename = self.tracker_icons.get(request.tracker_name)
@ -185,7 +183,7 @@ class Flag(resource.Resource):
def getChild(self, path, request): def getChild(self, path, request):
request.country = path request.country = path
return self return self
def render(self, request): def render(self, request):
headers = {} headers = {}
path = ("data", "pixmaps", "flags", request.country.lower() + ".png") path = ("data", "pixmaps", "flags", request.country.lower() + ".png")
@ -203,20 +201,20 @@ class Flag(resource.Resource):
return "" return ""
class LookupResource(resource.Resource, component.Component): class LookupResource(resource.Resource, component.Component):
def __init__(self, name, *directories): def __init__(self, name, *directories):
resource.Resource.__init__(self) resource.Resource.__init__(self)
component.Component.__init__(self, name) component.Component.__init__(self, name)
self.__directories = directories self.__directories = directories
@property @property
def directories(self): def directories(self):
return self.__directories return self.__directories
def getChild(self, path, request): def getChild(self, path, request):
request.path = path request.path = path
return self return self
def render(self, request): def render(self, request):
log.debug("Requested path: '%s'", request.path) log.debug("Requested path: '%s'", request.path)
for lookup in self.directories: for lookup in self.directories:
@ -231,13 +229,13 @@ class LookupResource(resource.Resource, component.Component):
class TopLevel(resource.Resource): class TopLevel(resource.Resource):
addSlash = True addSlash = True
__stylesheets = [ __stylesheets = [
"/css/ext-all.css", "/css/ext-all.css",
"/css/ext-extensions.css", "/css/ext-extensions.css",
"/css/deluge.css" "/css/deluge.css"
] ]
__scripts = [ __scripts = [
"/js/ext-base.js", "/js/ext-base.js",
"/js/ext-all.js", "/js/ext-all.js",
@ -246,7 +244,7 @@ class TopLevel(resource.Resource):
"/gettext.js", "/gettext.js",
"/js/deluge-yc.js" "/js/deluge-yc.js"
] ]
__debug_scripts = [ __debug_scripts = [
"/js/ext-base.js", "/js/ext-base.js",
"/js/ext-all-debug.js", "/js/ext-all-debug.js",
@ -288,7 +286,7 @@ class TopLevel(resource.Resource):
"/js/Deluge.Torrents.js", "/js/Deluge.Torrents.js",
"/js/Deluge.UI.js" "/js/Deluge.UI.js"
] ]
def __init__(self): def __init__(self):
resource.Resource.__init__(self) resource.Resource.__init__(self)
self.putChild("config.js", Config()) self.putChild("config.js", Config())
@ -303,22 +301,22 @@ class TopLevel(resource.Resource):
self.putChild("render", Render()) self.putChild("render", Render())
self.putChild("themes", static.File(rpath("themes"))) self.putChild("themes", static.File(rpath("themes")))
self.putChild("tracker", Tracker()) self.putChild("tracker", Tracker())
theme = component.get("DelugeWeb").config["theme"] theme = component.get("DelugeWeb").config["theme"]
self.__stylesheets.append("/css/xtheme-%s.css" % theme) self.__stylesheets.append("/css/xtheme-%s.css" % theme)
@property @property
def scripts(self): def scripts(self):
return self.__scripts return self.__scripts
@property @property
def debug_scripts(self): def debug_scripts(self):
return self.__debug_scripts return self.__debug_scripts
@property @property
def stylesheets(self): def stylesheets(self):
return self.__stylesheets return self.__stylesheets
def getChild(self, path, request): def getChild(self, path, request):
if path == "": if path == "":
return self return self
@ -330,22 +328,22 @@ class TopLevel(resource.Resource):
scripts = self.debug_scripts[:] scripts = self.debug_scripts[:]
else: else:
scripts = self.scripts[:] scripts = self.scripts[:]
template = Template(filename=rpath("index.html")) template = Template(filename=rpath("index.html"))
request.setHeader("content-type", "text/html; charset=utf-8") request.setHeader("content-type", "text/html; charset=utf-8")
return template.render(scripts=scripts, stylesheets=self.stylesheets) return template.render(scripts=scripts, stylesheets=self.stylesheets)
class DelugeWeb(component.Component): class DelugeWeb(component.Component):
def __init__(self): def __init__(self):
super(DelugeWeb, self).__init__("DelugeWeb") super(DelugeWeb, self).__init__("DelugeWeb")
self.config = ConfigManager("web.conf", CONFIG_DEFAULTS) self.config = ConfigManager("web.conf", CONFIG_DEFAULTS)
self.top_level = TopLevel() self.top_level = TopLevel()
self.site = server.Site(self.top_level) self.site = server.Site(self.top_level)
self.port = self.config["port"] self.port = self.config["port"]
self.web_api = WebApi() self.web_api = WebApi()
# Since twisted assigns itself all the signals may as well make # Since twisted assigns itself all the signals may as well make
# use of it. # use of it.
reactor.addSystemEventTrigger("after", "shutdown", self.shutdown) reactor.addSystemEventTrigger("after", "shutdown", self.shutdown)
@ -362,7 +360,7 @@ class DelugeWeb(component.Component):
self.shutdown() self.shutdown()
return 1 return 1
SetConsoleCtrlHandler(win_handler) SetConsoleCtrlHandler(win_handler)
# Initalize the plugins # Initalize the plugins
self.plugins = PluginManager() self.plugins = PluginManager()