示例#1
0
    def __init__(self, dbhost, dbuser, dbpass, dbname):
        # reset event
        if EVENT_MCX_UPDATES_AVAILABLE.isSet():
            EVENT_MCX_UPDATES_AVAILABLE.clear()

        self.__cursor = None
        """The cursor of MySQLdb library retrieved from the mcxPersistent object."""

        self.__updates = {}
        """All updates, that are available since last time. Defaults to an empty dict."""

        self.__getUpdateMethods()

        # initiate base class
        mcxDatabaseThread.__init__(self, dbhost, dbuser, dbpass, dbname)
示例#2
0
    def __checkForUpdates(self):
        """The base method that's actually checking for new updates by running all marked methods."""

        if len(self.__updateMethods):
            updatesAvailable = False
            for method in self.__updateMethods:
                # get new cursor
                self.__cursor = self.persistent.getCursor()

                # execute check update method
                updatesAvailable = updatesAvailable or getattr(self, method)()

                # close and reset cursor
                self.__cursor.close()
                self.__cursor = None

            if updatesAvailable:
                # needs to be cleared by another thread
                EVENT_MCX_UPDATES_AVAILABLE.set()
示例#3
0
    def run(self):
        """
            @requires: self.isDaemon() == True

            @summary: This thread serves forever.
                As long as EVENT_MCX_UPDATES_AVAILABLE is not set this thread will look every CONFIG_CHECKUPDATE_INTERVAL seconds for new updates until found.

                If updates were found, it will fire EVENT_MCX_UPDATES_AVAILABLE and wait until this event becomes cleared.
        """

        # serve forever
        while self.isDaemon():
            # and event flag not cleared by now
            if not EVENT_MCX_UPDATES_AVAILABLE.isSet():
                # ensure we do not operate on an invalid object
                self.__cursor = None
                self.__clearUpdates()

                try:
                    # make sure connection is established
                    self.persistent.connect()

                    # check for updates only, if connection is established
                    if self.persistent.connected():
                        self.__cursor = self.persistent.getCursor()
                        # check for updates
                        self.__checkForUpdates()
                        # disconnect properly
                        self.persistent.disconnect()
                    else:
                        raise ConnectionException(DATABASE_SERVER_NOT_AVAILABLE)

                # no database available
                # let's handle the recover thread and wait for it
                except ConnectionException, (error):
                    print "%s fired EVENT_MCX_DATABASE_LOST caused by: %s" % (self.getName(), error)
                    # handle events
                    EVENT_MCX_DATABASE_RECOVERED.clear()
                    EVENT_MCX_DATABASE_LOST.set()
                    EVENT_MCX_DATABASE_RECOVERED.wait()

                # repeat this in a given interval and sleep in the meantime :)
                sleep(CONFIG_CHECKUPDATE_INTERVAL)