def __init__( self, sgConnection, startProcessingAtId=None, updateInterval=10 ): super(SgEventWatcher, self).__init__() self.__lock = threading.RLock() self.__connection = sgConnection self.__handlers = [] self.__updateInterval = int(updateInterval) self.__search_filters = ShotgunORM.SgEntitySearchFilters( 'EventLogEntry' ) if self.__updateInterval < self.UPDATE_INTERVAL_MIN: self.__updateInterval = self.UPDATE_INTERVAL_MIN if startProcessingAtId == None: startProcessingAtId = self.LAST_EVENT elif startProcessingAtId < self.NO_EVENT: startProcessingAtId = self.NO_EVENT self.__aborted = False self.__running = False self.__lastEvent = None self.__threadData = { 'event': threading.Event(), 'start_at_id': startProcessingAtId, 'batch_size': self.batchSize() } self.__monitorThread = None
def SgEventWatcherWorker(monitor, threadData): ''' Worker thread used by SgEventWatcher to monitor Shotgun for events. ''' e = threadData['event'] connection = monitor.connection() logger = monitor.logger() lastId = threadData['start_at_id'] if lastId < 0: if lastId == monitor.FIRST_EVENT: pass else: lastEvent = connection.findOne( 'EventLogEntry', [], order=[ { 'field_name': 'id', 'direction': 'desc' } ] ) if lastEvent: if lastId == monitor.LAST_EVENT: lastId = lastEvent['id'] - 1 else: lastId = -1 else: lastId -= 1 order=[{'field_name':'id','direction':'asc'}] limit = threadData['batch_size'] while True: logger.debug( 'retrieving events starting at id %(id)s' % { 'id': lastId } ) if monitor.aborted(): monitor._workerFinished() return events = [] page = 1 e.wait(monitor.updateInterval()) while True: if monitor.aborted(): monitor._workerFinished() return search_filters = ShotgunORM.SgEntitySearchFilters( 'EventLogEntry', [ [ 'id', 'greater_than', lastId ] ] ) search_filters.appendFilter(monitor.searchFilters()) try: eventBuffer = connection.find( 'EventLogEntry', search_filters.toLogicalOp(connection).toFilter(), order=order, page=page, limit=limit ) except ( ShotgunORM.SHOTGUN_API.ProtocolError, ShotgunORM.SHOTGUN_API.ResponseError, socket.error ), e: logger.warn(str(e)) continue logger.debug( 'retrieved %(count)s events' % { 'count': len(events) } ) events.extend(eventBuffer) if len(eventBuffer) < limit: break page += 1 with monitor: for i in events: monitor.processEvent(i) if len(events) > 0: lastId = events[-1]['id']