示例#1
0
    def run(self):
        """
            @summary: This thread is waiting for EVENT_MCX_DATABASE_LOST and if fired, retrying to connect to the databse.
                If connection could be established, EVENT_MCX_DATABASE_RECOVERED will be fired.

            @note: EVENT_MCX_DATABASE_LOST xor EVENT_MCX_DATABASE_RECOVERED
        """

        print "%s waiting for database lose .." % self.getName()

        # wait for the database lost event
        EVENT_MCX_DATABASE_LOST.wait()

        print "%s executing recovery .." % self.getName()

        if self.persistent.connected():
            self.persistent.disconnect()

        # event was fired
        # make sure everyone knows the database has not been recovered yet
        EVENT_MCX_DATABASE_RECOVERED.clear()

        # loop until database is available
        while not EVENT_MCX_DATABASE_RECOVERED.isSet():
            try:
                # connect
                self.persistent.connect()
                # ping the persistent
                if self.persistent.ping():
                    # on pong database is available

                    # database is not lost anymore
                    EVENT_MCX_DATABASE_LOST.clear()

                    # database connection finally re-established
                    # fire event, database has been recovered
                    EVENT_MCX_DATABASE_RECOVERED.set()

                    print "%s recovered database connection .." % self.getName()

                self.persistent.disconnect()
            except:
                # try until working
                pass

        # at the end.. "restart" thread
        self.run()
示例#2
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)