Пример #1
0
    def expire_servers(self, timeout):
        """set servers whose last_updated is before current - timeout as dead

        """
        from sqlalchemy.sql.expression import func, text
        Server = tables.Server
        OnAir = tables.OnAir
        now = tables.now_func()
        if isinstance(now, datetime.datetime):
            deadline = now - datetime.timedelta(seconds=timeout)
        else:
            deadline = func.timestampadd(text('second'), -timeout, now)
        query = self.session \
            .query(Server.id) \
            .filter(Server.alive) \
            .filter(Server.last_updated < deadline)
        # delete on airs of broadcast server
        broadcasts = query \
            .filter_by(type='broadcast') \
            .subquery()
        self.session \
            .query(OnAir) \
            .filter(OnAir.server_id.in_(broadcasts)) \
            .delete('fetch')
        # update server state to dead
        count = query.count()
        query.update(dict(alive=False), False)
        self.session.flush()
        self.logger.info('Expire %s servers', count)
Пример #2
0
    def expire_hosts(self, timeout):
        """set hosts whose last_updated is before current - timeout as dead

        """
        from sqlalchemy.sql.expression import func, text
        Host = tables.Host
        # delete out-dated hosts
        # func.timestampadd()
        now = tables.now_func()
        if isinstance(now, datetime.datetime):
            deadline = now - datetime.timedelta(seconds=timeout)
        else:
            deadline = func.timestampadd(text('second'), -timeout, now)
        query = self.session \
            .query(Host) \
            .filter(Host.alive) \
            .filter(Host.last_updated < deadline)
        count = query.count()
        query.update(dict(alive=False), False)
        self.session.flush()
        self.logger.info('Expire %s hosts', count)