Skip to content
Snippets Groups Projects
Commit 038b8fb0 authored by sveseli's avatar sveseli
Browse files

convert file system observer to use threading utility synchronization

parent 9d639567
No related branches found
No related tags found
No related merge requests found
...@@ -9,6 +9,7 @@ from dm.common.utility.loggingManager import LoggingManager ...@@ -9,6 +9,7 @@ from dm.common.utility.loggingManager import LoggingManager
from dm.common.utility.configurationManager import ConfigurationManager from dm.common.utility.configurationManager import ConfigurationManager
from dm.common.objects.observedFile import ObservedFile from dm.common.objects.observedFile import ObservedFile
from dm.common.utility.singleton import Singleton from dm.common.utility.singleton import Singleton
from dm.common.utility.threadingUtility import ThreadingUtility
from dmFileSystemEventHandler import DmFileSystemEventHandler from dmFileSystemEventHandler import DmFileSystemEventHandler
from fileProcessingManager import FileProcessingManager from fileProcessingManager import FileProcessingManager
...@@ -54,75 +55,57 @@ class FileSystemObserver(threading.Thread,Singleton): ...@@ -54,75 +55,57 @@ class FileSystemObserver(threading.Thread,Singleton):
self.minFileProcessingDelayInSeconds = int(cm.getConfigOption(FileSystemObserver.CONFIG_SECTION_NAME, FileSystemObserver.MIN_FILE_PROCESSING_DELAY_IN_SECONDS_KEY)) self.minFileProcessingDelayInSeconds = int(cm.getConfigOption(FileSystemObserver.CONFIG_SECTION_NAME, FileSystemObserver.MIN_FILE_PROCESSING_DELAY_IN_SECONDS_KEY))
self.fileSystemTimeoutInSeconds = int(cm.getConfigOption(FileSystemObserver.CONFIG_SECTION_NAME, FileSystemObserver.FILE_SYSTEM_EVENT_TIMEOUT_IN_SECONDS_KEY)) self.fileSystemTimeoutInSeconds = int(cm.getConfigOption(FileSystemObserver.CONFIG_SECTION_NAME, FileSystemObserver.FILE_SYSTEM_EVENT_TIMEOUT_IN_SECONDS_KEY))
@ThreadingUtility.synchronize
def startObservingPath(self, daqPath, experiment): def startObservingPath(self, daqPath, experiment):
self.lock.acquire() self.logger.debug('Starting observer for %s' % daqPath)
try: eventHandler = DmFileSystemEventHandler(self, daqPath, experiment)
self.logger.debug('Starting observer for %s' % daqPath) observedWatch = self.observer.schedule(eventHandler, daqPath, recursive=True)
eventHandler = DmFileSystemEventHandler(self, daqPath, experiment) self.observedWatchDict[daqPath] = observedWatch
observedWatch = self.observer.schedule(eventHandler, daqPath, recursive=True)
self.observedWatchDict[daqPath] = observedWatch
finally:
self.lock.release()
@ThreadingUtility.synchronize
def stopObservingPath(self, daqPath, experiment): def stopObservingPath(self, daqPath, experiment):
self.lock.acquire() observedWatch = self.observedWatchDict.get(daqPath)
try: if observedWatch:
observedWatch = self.observedWatchDict.get(daqPath) self.logger.debug('Stopping observer for %s' % daqPath)
if observedWatch: self.observer.unschedule(observedWatch)
self.logger.debug('Stopping observer for %s' % daqPath) del self.observedWatchDict[daqPath]
self.observer.unschedule(observedWatch) else:
del self.observedWatchDict[daqPath] self.logger.debug('Observer for %s is not active' % daqPath)
else:
self.logger.debug('Observer for %s is not active' % daqPath)
finally:
self.lock.release()
@ThreadingUtility.synchronize
def observedFileUpdated(self, filePath, daqPath, experiment): def observedFileUpdated(self, filePath, daqPath, experiment):
self.lock.acquire() observedFile = self.observedFileMap.get(filePath, ObservedFile(filePath=filePath, daqPath=daqPath, experiment=experiment))
try: observedFile.setLastUpdatedTimestampToNow()
observedFile = self.observedFileMap.get(filePath, ObservedFile(filePath=filePath, daqPath=daqPath, experiment=experiment)) self.observedFileMap[filePath] = observedFile
observedFile.setLastUpdatedTimestampToNow() self.logger.debug('Observed file updated: %s', observedFile)
self.observedFileMap[filePath] = observedFile
self.logger.debug('Observed file updated: %s', observedFile)
finally:
self.lock.release()
@ThreadingUtility.synchronize
def checkObservedFilesForProcessing(self): def checkObservedFilesForProcessing(self):
self.lock.acquire() now = time.time()
try: filePathsForProcessing = []
now = time.time() for (filePath,observedFile) in self.observedFileMap.items():
filePathsForProcessing = [] timestamp = observedFile.get('lastUpdateTimestamp')
for (filePath,observedFile) in self.observedFileMap.items(): deltaT = now - timestamp
timestamp = observedFile.get('lastUpdateTimestamp') if deltaT > self.minFileProcessingDelayInSeconds:
deltaT = now - timestamp self.logger.debug('File %s was last modified %s seconds ago, will process it.' % (filePath, deltaT))
if deltaT > self.minFileProcessingDelayInSeconds: filePathsForProcessing.append(filePath)
self.logger.debug('File %s was last modified %s seconds ago, will process it.' % (filePath, deltaT)) return filePathsForProcessing
filePathsForProcessing.append(filePath)
return filePathsForProcessing
finally:
self.lock.release()
@ThreadingUtility.synchronize
def processObservedFile(self, filePath): def processObservedFile(self, filePath):
self.lock.acquire() self.logger.debug('Processing file %s' % filePath)
try: observedFile = self.observedFileMap.get(filePath)
self.logger.debug('Processing file %s' % filePath) if observedFile is not None:
observedFile = self.observedFileMap.get(filePath) del self.observedFileMap[filePath]
if observedFile is not None: self.fileProcessingManager.processObservedFile(observedFile)
del self.observedFileMap[filePath]
self.fileProcessingManager.processObservedFile(observedFile)
finally:
self.lock.release()
@ThreadingUtility.synchronize
def start(self): def start(self):
self.lock.acquire() self.logger.debug('Starting file observer thread')
try: threading.Thread.start(self)
self.logger.debug('Starting file observer thread')
threading.Thread.start(self)
self.logger.debug('Starting watchdog observer') self.logger.debug('Starting watchdog observer')
self.observer.start() self.observer.start()
finally:
self.lock.release()
def run(self): def run(self):
self.logger.debug('Starting thread: %s' % self.getName()) self.logger.debug('Starting thread: %s' % self.getName())
...@@ -140,36 +123,26 @@ class FileSystemObserver(threading.Thread,Singleton): ...@@ -140,36 +123,26 @@ class FileSystemObserver(threading.Thread,Singleton):
self.logger.exception(ex) self.logger.exception(ex)
self.eventFlag.wait(timeout=self.fileSystemTimeoutInSeconds) self.eventFlag.wait(timeout=self.fileSystemTimeoutInSeconds)
@ThreadingUtility.synchronize
def stop(self): def stop(self):
self.lock.acquire() self.logger.debug('Stopping watchdog observer')
try: self.observer.stop()
self.logger.debug('Stopping watchdog observer') self.observer.join()
self.observer.stop()
self.observer.join() self.logger.debug('Stopping file observer thread')
self.exitFlag = True
self.logger.debug('Stopping file observer thread') self.eventFlag.set()
self.exitFlag = True self.logger.debug('Event is set, joining thread')
self.eventFlag.set() threading.Thread.join(self)
self.logger.debug('Event is set, joining thread') self.logger.debug('Module stopped')
threading.Thread.join(self)
self.logger.debug('Module stopped') @ThreadingUtility.synchronize
finally:
self.lock.release()
def setEvent(self): def setEvent(self):
self.lock.acquire() self.eventFlag.set()
try:
self.eventFlag.set()
finally:
self.lock.release()
@ThreadingUtility.synchronize
def clearEvent(self): def clearEvent(self):
self.lock.acquire() self.eventFlag.clear()
try:
self.eventFlag.clear()
finally:
self.lock.release()
#################################################################### ####################################################################
# Testing # Testing
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment