hydrus/server.py

108 lines
3.5 KiB
Python
Raw Normal View History

2015-09-02 23:16:09 +00:00
# This program is free software. It comes without any warranty, to
# the extent permitted by applicable law. You can redistribute it
# and/or modify it under the terms of the Do What The Fuck You Want
# To Public License, Version 2, as published by Sam Hocevar. See
# http://sam.zoy.org/wtfpl/COPYING for more details.
try:
import locale
try: locale.setlocale( locale.LC_ALL, '' )
except: pass
2015-11-18 22:44:07 +00:00
from include import HydrusExceptions
2015-09-02 23:16:09 +00:00
from include import HydrusConstants as HC
2015-11-18 22:44:07 +00:00
from include import HydrusData
2015-09-02 23:16:09 +00:00
import os
import sys
import time
from include import ServerController
import threading
from twisted.internet import reactor
from include import HydrusGlobals
2015-11-11 21:20:41 +00:00
from include import HydrusLogger
2015-09-02 23:16:09 +00:00
import traceback
HydrusGlobals.instance = HC.HYDRUS_SERVER
action = ServerController.GetStartingAction()
2015-09-16 18:11:00 +00:00
if action == 'help':
2015-09-02 23:16:09 +00:00
2015-11-18 22:44:07 +00:00
HydrusData.Print( 'This is the hydrus server. It accepts these commands:' )
HydrusData.Print( '' )
HydrusData.Print( 'server start - runs the server' )
HydrusData.Print( 'server stop - stops an existing instance of this server' )
HydrusData.Print( 'server restart - stops an existing instance of this server, then runs itself' )
HydrusData.Print( '' )
HydrusData.Print( 'You can also run \'server\' without arguments. Depending on what is going on, it will try to start or it will ask you if you want to stop or restart.' )
HydrusData.Print( 'You can also stop the running server just by hitting Ctrl+C.')
2015-09-02 23:16:09 +00:00
else:
2015-11-18 22:44:07 +00:00
with HydrusLogger.HydrusLogger( 'server.log' ) as logger:
2015-09-16 18:11:00 +00:00
2015-11-11 21:20:41 +00:00
error_occured = False
2015-09-02 23:16:09 +00:00
try:
2015-09-16 18:11:00 +00:00
if action in ( 'stop', 'restart' ):
ServerController.ShutdownSiblingInstance()
if action in ( 'start', 'restart' ):
2015-11-18 22:44:07 +00:00
HydrusData.Print( 'Initialising controller...' )
2015-09-16 18:11:00 +00:00
threading.Thread( target = reactor.run, kwargs = { 'installSignalHandlers' : 0 } ).start()
controller = ServerController.Controller()
controller.Run()
2015-09-02 23:16:09 +00:00
except HydrusExceptions.PermissionException as e:
error_occured = True
error = str( e )
2015-11-18 22:44:07 +00:00
HydrusData.Print( error )
2015-09-02 23:16:09 +00:00
except:
error_occured = True
error = traceback.format_exc()
2015-11-18 22:44:07 +00:00
HydrusData.Print( 'Hydrus server failed' )
2015-09-02 23:16:09 +00:00
2015-11-18 22:44:07 +00:00
HydrusData.Print( traceback.format_exc() )
2015-09-02 23:16:09 +00:00
finally:
HydrusGlobals.view_shutdown = True
HydrusGlobals.model_shutdown = True
try: controller.pubimmediate( 'wake_daemons' )
except: pass
reactor.callFromThread( reactor.stop )
except HydrusExceptions.PermissionException as e:
2015-11-18 22:44:07 +00:00
HydrusData.Print( e )
2015-09-02 23:16:09 +00:00
except:
import traceback
2015-11-18 22:44:07 +00:00
HydrusData.Print( 'Critical error occured! Details written to crash.log!' )
2015-09-02 23:16:09 +00:00
with open( 'crash.log', 'wb' ) as f: f.write( traceback.format_exc() )