forked from hydrusnetwork/hydrus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
119 lines (68 loc) · 3.68 KB
/
server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#!/usr/bin/env python2
# 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
from include import HydrusExceptions
from include import HydrusConstants as HC
from include import HydrusData
import os
import sys
import time
from include import ServerController
import threading
from twisted.internet import reactor
from include import HydrusGlobals
from include import HydrusLogger
import traceback
action = ServerController.GetStartingAction()
if action == 'help':
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.')
else:
log_path = os.path.join( HC.DB_DIR, 'server.log' )
with HydrusLogger.HydrusLogger( log_path ) as logger:
try:
if action in ( 'stop', 'restart' ):
ServerController.ShutdownSiblingInstance()
if action in ( 'start', 'restart' ):
HydrusData.Print( 'Initialising controller...' )
threading.Thread( target = reactor.run, kwargs = { 'installSignalHandlers' : 0 } ).start()
controller = ServerController.Controller()
controller.Run()
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 )
except HydrusExceptions.PermissionException as e:
HydrusData.Print( e )
except:
import traceback
try:
dest_path = os.path.join( HC.DB_DIR, 'crash.log' )
with open( dest_path, 'wb' ) as f:
f.write( traceback.format_exc() )
print( 'Critical error occured! Details written to crash.log!' )
except NameError, IOError:
print( 'Critical error occured!' )
traceback.print_exc()