hydrus/include/ServerController.py

227 lines
7.6 KiB
Python
Raw Normal View History

2013-02-19 00:11:43 +00:00
import httplib
import HydrusConstants as HC
2013-10-02 22:06:06 +00:00
import HydrusServer
2013-03-15 02:38:12 +00:00
import HydrusSessions
2013-10-02 22:06:06 +00:00
import ServerConstants as SC
2013-02-19 00:11:43 +00:00
import ServerDB
import os
import random
import threading
import time
import traceback
import wx
import yaml
2013-10-02 22:06:06 +00:00
from twisted.internet import reactor
2013-10-23 21:36:47 +00:00
from twisted.internet import defer
2013-02-19 00:11:43 +00:00
class Controller( wx.App ):
def _AlreadyRunning( self, port ):
2013-09-25 20:20:10 +00:00
connection = httplib.HTTPConnection( '127.0.0.1', port, timeout = 20 )
2013-02-19 00:11:43 +00:00
try:
connection.connect()
connection.close()
return True
except: return False
2013-10-09 18:13:42 +00:00
def _Read( self, action, priority, *args, **kwargs ): return self._db.Read( action, priority, *args, **kwargs )
2013-03-15 02:38:12 +00:00
2013-10-02 22:06:06 +00:00
def _Write( self, action, priority, *args, **kwargs ): return self._db.Write( action, priority, *args, **kwargs )
2013-11-06 18:22:07 +00:00
def GetSessionManager( self ): return self._session_manager
2013-03-15 02:38:12 +00:00
2013-02-19 00:11:43 +00:00
def EventExit( self, event ): self._tbicon.Destroy()
def EventPubSub( self, event ):
pubsubs_queue = HC.pubsub.GetQueue()
( callable, args, kwargs ) = pubsubs_queue.get()
try: callable( *args, **kwargs )
except TypeError: pass
2013-08-14 20:21:49 +00:00
except Exception as e: HC.ShowException( e )
2013-02-19 00:11:43 +00:00
pubsubs_queue.task_done()
def OnInit( self ):
2013-07-10 20:25:57 +00:00
HC.app = self
2013-10-02 22:06:06 +00:00
try:
self._db = ServerDB.DB()
self.Bind( wx.EVT_MENU, self.EventExit, id=wx.ID_EXIT )
self.Bind( HC.EVT_PUBSUB, self.EventPubSub )
self._session_manager = HydrusSessions.HydrusSessionManagerServer()
HC.pubsub.sub( self, 'RestartService', 'restart_service' )
self._services = {}
services_info = self.Read( 'services' )
2013-10-23 21:36:47 +00:00
for ( service_identifier, options ) in services_info:
if service_identifier.GetType() == HC.SERVER_ADMIN:
port = options[ 'port' ]
connection = httplib.HTTPConnection( '127.0.0.1', port, timeout = 10 )
try:
connection.connect()
connection.close()
message = 'Something was already bound to port ' + HC.u( port )
wx.MessageBox( message )
return False
except: pass
2013-10-02 22:06:06 +00:00
for ( service_identifier, options ) in services_info: self.RestartService( service_identifier, options )
2013-10-09 18:13:42 +00:00
self.StartDaemons()
2013-10-23 21:36:47 +00:00
self._tbicon = TaskBarIcon()
2013-10-02 22:06:06 +00:00
return True
2013-02-19 00:11:43 +00:00
except Exception as e:
2013-10-02 22:06:06 +00:00
print( traceback.format_exc() )
2013-02-19 00:11:43 +00:00
return False
2013-10-02 22:06:06 +00:00
def Read( self, action, *args, **kwargs ):
2013-03-15 02:38:12 +00:00
2013-10-09 18:13:42 +00:00
return self._Read( action, HC.HIGH_PRIORITY, *args, **kwargs )
def ReadDaemon( self, action, *args, **kwargs ):
return self._Read( action, HC.LOW_PRIORITY, *args, **kwargs )
2013-02-19 00:11:43 +00:00
2013-10-02 22:06:06 +00:00
def RestartService( self, service_identifier, options ):
2013-02-19 00:11:43 +00:00
2013-10-02 22:06:06 +00:00
def TWISTEDRestartService():
2013-10-23 21:36:47 +00:00
def StartService( *args, **kwargs ):
2013-10-02 22:06:06 +00:00
try:
2013-10-09 18:13:42 +00:00
if 'port' not in options: return
2013-10-02 22:06:06 +00:00
port = options[ 'port' ]
connection = httplib.HTTPConnection( '127.0.0.1', port, timeout = 10 )
try:
connection.connect()
connection.close()
raise Exception( 'Something was already bound to port ' + HC.u( port ) )
except:
message = options[ 'message' ]
service_type = service_identifier.GetType()
if service_type == HC.SERVER_ADMIN: service_object = HydrusServer.HydrusServiceAdmin( service_identifier, message )
elif service_type == HC.FILE_REPOSITORY: service_object = HydrusServer.HydrusServiceRepositoryFile( service_identifier, message )
elif service_type == HC.TAG_REPOSITORY: service_object = HydrusServer.HydrusServiceRepositoryTag( service_identifier, message )
elif service_type == HC.MESSAGE_DEPOT: return
self._services[ service_identifier ] = reactor.listenTCP( port, service_object )
connection = httplib.HTTPConnection( '127.0.0.1', port, timeout = 10 )
try:
connection.connect()
connection.close()
except:
raise Exception( 'Tried to bind port ' + HC.u( port ) + ' but it failed.' )
except Exception as e:
print( traceback.format_exc() )
2013-10-09 18:13:42 +00:00
2013-10-02 22:06:06 +00:00
if service_identifier not in self._services: StartService()
else:
2013-10-23 21:36:47 +00:00
deferred = defer.maybeDeferred( self._services[ service_identifier ].stopListening )
2013-10-02 22:06:06 +00:00
2013-10-23 21:36:47 +00:00
deferred.addCallback( StartService )
2013-10-02 22:06:06 +00:00
2013-10-09 18:13:42 +00:00
2013-10-02 22:06:06 +00:00
reactor.callFromThread( TWISTEDRestartService )
2013-10-09 18:13:42 +00:00
def StartDaemons( self ):
HC.DAEMONQueue( 'FlushRequestsMade', ServerDB.DAEMONFlushRequestsMade, 'request_made', period = 60 )
HC.DAEMONWorker( 'CheckMonthlyData', ServerDB.DAEMONCheckMonthlyData, period = 3600 )
HC.DAEMONWorker( 'ClearBans', ServerDB.DAEMONClearBans, period = 3600 )
HC.DAEMONWorker( 'DeleteOrphans', ServerDB.DAEMONDeleteOrphans, period = 86400 )
HC.DAEMONWorker( 'GenerateUpdates', ServerDB.DAEMONGenerateUpdates, period = 1200 )
HC.DAEMONWorker( 'CheckDataUsage', ServerDB.DAEMONCheckDataUsage, period = 86400 )
2013-11-06 18:22:07 +00:00
HC.DAEMONWorker( 'UPnP', ServerDB.DAEMONUPnP, ( 'notify_new_options', ), period = 43200 )
2013-10-09 18:13:42 +00:00
2013-10-02 22:06:06 +00:00
def Write( self, action, *args, **kwargs ):
2013-02-19 00:11:43 +00:00
2013-10-02 22:06:06 +00:00
return self._Write( action, HC.HIGH_PRIORITY, *args, **kwargs )
2013-02-19 00:11:43 +00:00
2013-10-09 18:13:42 +00:00
def WriteDaemon( self, action, *args, **kwargs ):
return self._Write( action, HC.LOW_PRIORITY, *args, **kwargs )
2013-02-19 00:11:43 +00:00
class TaskBarIcon( wx.TaskBarIcon ):
def __init__( self ):
wx.TaskBarIcon.__init__( self )
icon = wx.Icon( HC.STATIC_DIR + os.path.sep + 'hydrus.ico', wx.BITMAP_TYPE_ICO )
self.SetIcon( icon, 'hydrus server' )
self._tbmenu = wx.Menu()
self._tbmenu.Append( wx.ID_EXIT, 'exit' )
2013-09-04 16:48:44 +00:00
self.Bind( wx.EVT_TASKBAR_RIGHT_DOWN, lambda event: self.PopupMenu( self._tbmenu ) )
2013-02-19 00:11:43 +00:00