Skip to content

Commit

Permalink
Daemon configuration management refactored to SQLite usage.
Browse files Browse the repository at this point in the history
  • Loading branch information
grigoryvp committed Mar 1, 2012
1 parent 0fee43f commit 6a09aa8
Showing 1 changed file with 13 additions and 29 deletions.
42 changes: 13 additions & 29 deletions parabridge_daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,43 +17,24 @@

import pyparadox

class Config( object ) :

m_oTimeReloadLast = None
m_mItems = { 'tasks' : [] }

@classmethod
def reload( self ) :
sPath = os.path.expanduser( "~/.parabridge" )
if os.path.exists( sPath ) :
self.m_mItems.update( json.load( open( sPath ) ) )
self.m_oTimeReloadLast = time.localtime()
Worker.instance().cfgChanged()

@classmethod
def get( self ) :
return self.m_mItems

@classmethod
def timeReloadLast( self ) :
return self.m_oTimeReloadLast

class Worker( threading.Thread ) :

m_oInstance = None

def __init__( self ) :
super( Worker, self ).__init__()
self.m_fShutdown = False
self.m_fCfgChanged = True
self.m_mResults = {}
self.m_oTimeReloadLast = None

def run( self ) :
mCfg = Config().get()
while not self.m_fShutdown :
if self.m_fCfgChanged :
mCfg = Config().get()
lTasks = Settings.taskList()
self.m_fCfgChanged = False
for mTask in mCfg[ 'tasks' ] :
self.m_oTimeReloadLast = time.localtime()
for mTask in lTasks :
sSrc = os.path.expanduser( mTask[ 'src' ] )
sDst = os.path.expanduser( mTask[ 'dst' ] )
self.processTask( mTask[ 'name' ], sSrc, sDst )
Expand All @@ -77,12 +58,13 @@ def setRes( i_sTxt ) :
if 0 == len( lSrcFiles ) :
return setRes( "No .db files in \"{0}\".".format( i_sSrc ) )
lProcessed = []
for sSrcFile in lSrcFiles :
nTotal = len( lSrcFiles )
for i, sSrcFile in enumerate( lSrcFiles ) :
setRes( "Processing {0}/{1}".format( i + 1, nTotal ) )
if self.processParadoxFile( sSrcFile, i_sDst ) :
lProcessed.append( True )
sTime = time.strftime( '%Y.%m.%d %H:%M:%S' )
nProcessed = len( lProcessed )
nTotal = len( lSrcFiles )
setRes( "Processed {0}/{1} at {2}.".format( nProcessed, nTotal, sTime ) )

##x Process individual Paradox |.db| file and synchronize specified
Expand All @@ -104,6 +86,8 @@ def cfgChanged( self ) : self.m_fCfgChanged = True

def results( self ) : return self.m_mResults

def timeReloadLast( self ) : return self.m_oTimeReloadLast

class Server( SimpleXMLRPCServer, object ) :

def __init__( self, i_nPort ) :
Expand All @@ -123,24 +107,24 @@ def stop( self ) :
return True

def status( self ) :
oTimeReloadLast = Worker.instance().timeReloadLast()
sMsg = """Daemon is running.
\tConfiguration reloaded: {0}""".format(
time.strftime( '%Y.%m.%d %H:%M:%S', Config().timeReloadLast() ) )
time.strftime( '%Y.%m.%d %H:%M:%S', oTimeReloadLast ) )
mResults = Worker.instance().results()
for sKey in sorted( mResults.keys() ) :
sMsg += "\n{0}:\n\t {1}".format( sKey, mResults[ sKey ] )
return re.sub( '\t', ' ', re.sub( ' +', ' ', sMsg ) )

def cfg_changed( self ) :
Config().reload()
Worker.instance().cfgChanged()
return True

Settings.init()
oParser = argparse.ArgumentParser( description = "Parabridge daemon" )
oParser.add_argument( 'port', type = int, help = "Port to listen on" )
oArgs = oParser.parse_args()

Config.reload()
Worker.instance().start()
try :
Server( oArgs.port ).serve_forever()
Expand Down

0 comments on commit 6a09aa8

Please sign in to comment.