Exemple #1
0
    def get_current_user(self):
        username = self.request.headers.get(settings.user_auth_header)
        if not username:
            return

        # Users must be fully qualified
        if not re.match("^{}$".format(USERNAME_VALIDATION), username):
            raise InvalidUser()

        try:
            user, created = User.get_or_create(self.session, username=username)
            if created:
                logging.info("Created new user %s", username)
                self.session.commit()
                # Because the graph doesn't initialize until the updates table
                # is populated, we need to refresh the graph here in case this
                # is the first update.
                self.graph.update_from_db(self.session)
        except sqlalchemy.exc.OperationalError:
            # Failed to connect to database or create user, try to reconfigure the db. This invokes
            # the fetcher to try to see if our URL string has changed.
            Session.configure(bind=get_db_engine(get_database_url(settings)))
            raise DatabaseFailure()

        return user
Exemple #2
0
def session(request, tmpdir):
    db_path = tmpdir.join("grouper.sqlite")
    db_engine = get_db_engine("sqlite:///%s" % db_path)

    models.Model.metadata.create_all(db_engine)
    Session.configure(bind=db_engine)
    session = Session()

    def fin():
        session.close()
        # Useful if testing against MySQL
        # models.Model.metadata.drop_all(db_engine)
    request.addfinalizer(fin)

    return session
Exemple #3
0
def session(request, tmpdir):
    db_path = tmpdir.join("grouper.sqlite")
    db_engine = get_db_engine("sqlite:///%s" % db_path)

    models.Model.metadata.create_all(db_engine)
    Session.configure(bind=db_engine)
    session = Session()

    def fin():
        session.close()
        # Useful if testing against MySQL
        #models.Model.metadata.drop_all(db_engine)
    request.addfinalizer(fin)

    return session
Exemple #4
0
    def run(self):
        while True:
            sleep(self.refresh_interval)

            logging.debug("Updating Graph from Database.")
            try:
                session = Session()
                self.graph.update_from_db(session)
                session.close()
                stats.set_gauge("successful-db-update", 1)
            except OperationalError:
                Session.configure(bind=get_db_engine(get_database_url(self.settings)))
                logging.critical("Failed to connect to database.")
                stats.set_gauge("successful-db-update", 0)
                self.capture_exception()
            except:
                stats.set_gauge("successful-db-update", 0)
                self.capture_exception()
                raise
Exemple #5
0
 def run(self):
     while True:
         logging.debug("Sending emails...")
         try:
             session = Session()
             self.send_emails(session, datetime.utcnow())
             session.commit()
             session.close()
         except OperationalError:
             Session.configure(
                 bind=get_db_engine(get_database_url(self.settings)))
             logging.critical("Failed to connect to database.")
         sleep(60)
Exemple #6
0
 def run(self):
     while True:
         try:
             session = Session()
             logging.debug("Expiring edges....")
             self.expire_edges(session)
             logging.debug("Sending emails...")
             process_async_emails(self.settings, session, datetime.utcnow())
             logging.debug("Pruning old traces....")
             prune_old_traces(session)
             session.commit()
             session.close()
             stats.set_gauge("successful-background-run", 1)
         except OperationalError:
             Session.configure(bind=get_db_engine(get_database_url(self.settings)))
             logging.critical("Failed to connect to database.")
             stats.set_gauge("successful-background-run", 0)
             self.capture_exception()
         except:
             stats.set_gauge("successful-background-run", 0)
             self.capture_exception()
             raise
         sleep(60)
Exemple #7
0
def make_session():
    db_engine = get_db_engine(get_database_url(settings))
    Session.configure(bind=db_engine)
    return Session()