fix handling close on windows

This commit is contained in:
Damien Churchill 2009-04-27 18:46:30 +00:00
commit 725198fc4d

View file

@ -27,6 +27,7 @@ import time
import locale import locale
import shutil import shutil
import signal import signal
import signal
import urllib import urllib
import gettext import gettext
import hashlib import hashlib
@ -349,6 +350,19 @@ class DelugeWeb(component.Component):
# use of it. # use of it.
reactor.addSystemEventTrigger("after", "shutdown", self.shutdown) reactor.addSystemEventTrigger("after", "shutdown", self.shutdown)
# Twisted doesn't handle windows specific signals so we still
# need to attach to those to handle the close correctly.
if common.windows_check():
from win32api import SetConsoleCtrlHandler
from win32con import CTRL_CLOSE_EVENT, CTRL_SHUTDOWN_EVENT
def win_handler(ctrl_type):
log.debug("ctrl type: %s", ctrl_type)
if ctrl_type == CTRL_CLOSE_EVENT or \
ctrl_type == CTRL_SHUTDOWN_EVENT:
self.shutdown()
return 1
SetConsoleCtrlHandler(win_handler)
# Initalize the plugins # Initalize the plugins
self.plugins = PluginManager() self.plugins = PluginManager()
@ -359,10 +373,14 @@ class DelugeWeb(component.Component):
self.port, self.port) self.port, self.port)
reactor.run() reactor.run()
def shutdown(self): def shutdown(self, *args):
log.info("Shutting down webserver") log.info("Shutting down webserver")
log.debug("Saving configuration file") log.debug("Saving configuration file")
self.config.save() self.config.save()
try:
reactor.stop()
except error.ReactorNotRunning:
log.debug("Reactor not running")
if __name__ == "__builtin__": if __name__ == "__builtin__":
deluge_web = DelugeWeb() deluge_web = DelugeWeb()