hydrus/server.py

142 lines
4.1 KiB
Python
Raw Normal View History

2015-11-25 22:00:57 +00:00
#!/usr/bin/env python2
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
2016-10-12 21:52:50 +00:00
from include import HydrusPaths
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
2016-10-19 20:02:56 +00:00
#
2016-10-12 21:52:50 +00:00
import argparse
2015-09-02 23:16:09 +00:00
2016-10-12 21:52:50 +00:00
argparser = argparse.ArgumentParser( description = 'hydrus network server' )
2015-09-02 23:16:09 +00:00
2016-10-12 21:52:50 +00:00
argparser.add_argument( 'action', default = 'start', nargs = '?', choices = [ 'start', 'stop', 'restart' ], help = 'either start this server (default), or stop an existing server, or both' )
argparser.add_argument( '-d', '--db_dir', help = 'set an external db location' )
2016-10-19 20:02:56 +00:00
argparser.add_argument( '--no_daemons', action='store_true', help = 'run without background daemons' )
argparser.add_argument( '--no_wal', action='store_true', help = 'run without WAL db journalling' )
2016-10-12 21:52:50 +00:00
result = argparser.parse_args()
2016-10-19 20:02:56 +00:00
action = result.action
2016-10-12 21:52:50 +00:00
if result.db_dir is None:
db_dir = os.path.join( HC.BASE_DIR, 'db' )
2015-09-02 23:16:09 +00:00
else:
2016-10-12 21:52:50 +00:00
db_dir = result.db_dir
2016-10-26 20:45:34 +00:00
db_dir = HydrusPaths.ConvertPortablePathToAbsPath( db_dir, HC.BASE_DIR )
2016-10-19 20:02:56 +00:00
2016-10-12 21:52:50 +00:00
try:
HydrusPaths.MakeSureDirectoryExists( db_dir )
except:
raise Exception( 'Could not ensure db path ' + db_dir + ' exists! Check the location is correct and that you have permission to write to it!' )
2016-06-15 18:59:44 +00:00
2016-10-12 21:52:50 +00:00
2016-10-19 20:02:56 +00:00
no_daemons = result.no_daemons
no_wal = result.no_wal
#
action = ServerController.ProcessStartingAction( db_dir, action )
2016-10-12 21:52:50 +00:00
2016-11-09 23:13:22 +00:00
with HydrusLogger.HydrusLogger( db_dir, 'server' ) as logger:
2016-10-12 21:52:50 +00:00
try:
2015-09-16 18:11:00 +00:00
2016-10-12 21:52:50 +00:00
if action in ( 'stop', 'restart' ):
2015-09-02 23:16:09 +00:00
2016-10-12 21:52:50 +00:00
ServerController.ShutdownSiblingInstance( db_dir )
2015-09-02 23:16:09 +00:00
2016-10-12 21:52:50 +00:00
if action in ( 'start', 'restart' ):
2015-09-02 23:16:09 +00:00
2016-10-12 21:52:50 +00:00
HydrusData.Print( 'Initialising controller...' )
2015-09-02 23:16:09 +00:00
2016-10-12 21:52:50 +00:00
threading.Thread( target = reactor.run, kwargs = { 'installSignalHandlers' : 0 } ).start()
2015-09-02 23:16:09 +00:00
2016-10-19 20:02:56 +00:00
controller = ServerController.Controller( db_dir, no_daemons, no_wal )
2015-09-02 23:16:09 +00:00
2016-10-12 21:52:50 +00:00
controller.Run()
2015-09-02 23:16:09 +00:00
2016-10-12 21:52:50 +00:00
except HydrusExceptions.PermissionException as e:
error = str( e )
HydrusData.Print( error )
except:
error = traceback.format_exc()
HydrusData.Print( 'Hydrus server failed' )
HydrusData.Print( traceback.format_exc() )
finally:
HydrusGlobals.view_shutdown = True
HydrusGlobals.model_shutdown = True
try: controller.pubimmediate( 'wake_daemons' )
except: pass
reactor.callFromThread( reactor.stop )
2015-09-02 23:16:09 +00:00
except HydrusExceptions.PermissionException as e:
2015-11-18 22:44:07 +00:00
HydrusData.Print( e )
2015-09-02 23:16:09 +00:00
2016-10-12 21:52:50 +00:00
except Exception as e:
2015-09-02 23:16:09 +00:00
import traceback
2016-08-10 19:04:08 +00:00
import os
2015-09-02 23:16:09 +00:00
2016-10-12 21:52:50 +00:00
print( traceback.format_exc() )
if 'db_dir' in locals() and os.path.exists( db_dir ):
2016-07-20 19:57:10 +00:00
2016-10-12 21:52:50 +00:00
dest_path = os.path.join( db_dir, 'crash.log' )
2016-07-20 19:57:10 +00:00
with open( dest_path, 'wb' ) as f:
f.write( traceback.format_exc() )
print( 'Critical error occured! Details written to crash.log!' )
2016-10-12 21:52:50 +00:00