Skip to content
Snippets Groups Projects
executionThread.py 2.41 KiB
Newer Older
#!/usr/bin/env python

import threading
import time

from dm.common.utility.loggingManager import LoggingManager
from dm.common.utility.dmSubprocess import DmSubprocess

class ExecutionThread(threading.Thread):

    THREAD_EVENT_TIMEOUT_IN_SECONDS = 10.0

    def __init__ (self, name, executionManager, executionQueue):
    
        threading.Thread.__init__(self)
        self.setName(name)
        self.exitFlag = False
        self.executionManager = executionManager
        self.executionQueue = executionQueue
        self.logger = LoggingManager.getInstance().getLogger(name)

    def processFile(self, fileInfo):

        try:
            pass
        except Exception, ex:
            self.logger.exception(ex)

    def run(self):
        self.logger.debug('Starting thread: %s' % self.getName())
        while True:
            self.executionManager.clearEvent()
            if self.exitFlag:
                self.logger.debug('Exit flag is set')
                break

            while True:
                try:
                    queuedTuple = self.executionQueue.pop()
                    if queuedTuple is None:
                        break
                    processingJob, childProcess = queuedTuple
                    self.logger.debug('Execution queue depth after pop: %s' % self.executionQueue.getLength())
                    self.logger.debug('Starting child process: %s' % childProcess)
                    command = childProcess.get('command')
                    workingDir = childProcess.get('workingDir')
                    processingJob.childProcessStarted(childProcess)
                    p = DmSubprocess.executeCommandAndIgnoreFailure(command, workingDir)
                    processInfo = p.toDict()
                    if p.getExitStatus() == 0:
                        processingJob.childProcessCompleted(childProcess, processInfo)
                    else:
                        processingJob.childProcessFailed(childProcess, processInfo)
                except Exception, ex:
                    self.logger.exception(ex)
                    processingJob.childProcessFailed(childProcess, errorMessage=str(ex))

            self.executionManager.waitOnEvent(self.THREAD_EVENT_TIMEOUT_IN_SECONDS)
        self.logger.debug('%s is done' % self.getName())

    def stop(self):
        self.exitFlag = True

####################################################################
# Testing

if __name__ == '__main__':
    pass