예제 #1
0
    def __maintenance(self):
        with dbutils.Database() as db:
            cursor = db.cursor()

            # Update the UTC offsets of all timezones.
            #
            # The PostgreSQL database server has accurate (DST-adjusted) values,
            # but is very slow to query, so we cache the UTC offsets in our
            # 'timezones' table.  This call updates that cache every night.
            # (This is obviously a no-op most nights, but we don't want to have
            # to care about which nights it isn't.)
            self.debug("updating timezones")
            dbutils.updateTimezones(db)

            if self.terminated:
                return

            # Run a garbage collect in all Git repositories, to keep them neat
            # and tidy.
            cursor.execute("SELECT name FROM repositories")
            for (repository_name,) in cursor:
                self.debug("repository GC: %s" % repository_name)
                try:
                    repository = gitutils.Repository.fromName(db, repository_name)
                    repository.run("gc", "--prune=1 day", "--quiet")
                    repository.stopBatch()
                except Exception:
                    self.exception("repository GC failed: %s" % repository_name)

                if self.terminated:
                    return
예제 #2
0
    def __maintenance(self):
        with dbutils.Database() as db:
            cursor = db.cursor()

            # Update the UTC offsets of all timezones.
            #
            # The PostgreSQL database server has accurate (DST-adjusted) values,
            # but is very slow to query, so we cache the UTC offsets in our
            # 'timezones' table.  This call updates that cache every night.
            # (This is obviously a no-op most nights, but we don't want to have
            # to care about which nights it isn't.)
            self.debug("updating timezones")
            dbutils.updateTimezones(db)

            if self.terminated:
                return

            # Run a garbage collect in all Git repositories, to keep them neat
            # and tidy.  Also pack keepalive refs.
            cursor.execute("SELECT name FROM repositories")
            for (repository_name,) in cursor:
                self.debug("repository GC: %s" % repository_name)
                try:
                    repository = gitutils.Repository.fromName(db, repository_name)
                    repository.packKeepaliveRefs()
                    repository.run("gc", "--prune=1 day", "--quiet")
                    repository.stopBatch()
                except Exception:
                    self.exception("repository GC failed: %s" % repository_name)

                if self.terminated:
                    return

            if configuration.extensions.ENABLED:
                now = time.time()
                max_age = 7 * 24 * 60 * 60

                base_path = os.path.join(configuration.paths.DATA_DIR,
                                         "temporary", "EXTENSIONS")

                for user_name in os.listdir(base_path):
                    user_dir = os.path.join(base_path, user_name)

                    for extension_id in os.listdir(user_dir):
                        extension_dir = os.path.join(user_dir, extension_id)

                        for repository_name in os.listdir(extension_dir):
                            repository_dir = os.path.join(extension_dir,
                                                          repository_name)
                            age = now - os.stat(repository_dir).st_mtime

                            if age > max_age:
                                self.info("Removing repository work copy: %s"
                                          % repository_dir)
                                shutil.rmtree(repository_dir)
예제 #3
0
파일: maintenance.py 프로젝트: jensl/critic
    def __maintenance(self):
        with dbutils.Database.forSystem() as db:
            cursor = db.cursor()

            # Update the UTC offsets of all timezones.
            #
            # The PostgreSQL database server has accurate (DST-adjusted) values,
            # but is very slow to query, so we cache the UTC offsets in our
            # 'timezones' table.  This call updates that cache every night.
            # (This is obviously a no-op most nights, but we don't want to have
            # to care about which nights it isn't.)
            self.debug("updating timezones")
            dbutils.updateTimezones(db)

            if self.terminated:
                return

            # Execute scheduled review branch archivals.
            if configuration.base.ARCHIVE_REVIEW_BRANCHES:
                repository = None

                cursor.execute("""SELECT branches.repository, branches.id, branches.name
                                    FROM scheduledreviewbrancharchivals
                                    JOIN reviews ON (reviews.id=scheduledreviewbrancharchivals.review)
                                    JOIN branches ON (branches.id=reviews.branch)
                                   WHERE scheduledreviewbrancharchivals.deadline <= NOW()
                                     AND reviews.state IN ('closed', 'dropped')
                                     AND NOT branches.archived
                                ORDER BY branches.repository""",
                               for_update=True)

                for repository_id, branch_id, branch_name in cursor:
                    if not repository or repository.id != repository_id:
                        if repository:
                            repository.stopBatch()
                        repository = gitutils.Repository.fromId(db, repository_id)
                        self.info("archiving branches in: " + repository.name)

                    self.info("  " + branch_name)

                    branch = dbutils.Branch.fromId(db, branch_id, repository=repository)

                    try:
                        branch.archive(db)
                    except Exception:
                        self.exception(as_warning=True)

                # Since NOW() returns the same value each time within a single
                # transaction, this is guaranteed to delete only the set of
                # archivals we selected above.
                cursor.execute("""DELETE
                                    FROM scheduledreviewbrancharchivals
                                   WHERE deadline <= NOW()""")

                db.commit()

            # Run a garbage collect in all Git repositories, to keep them neat
            # and tidy.  Also pack keepalive refs.
            cursor.execute("SELECT name FROM repositories")
            for (repository_name,) in cursor:
                self.debug("repository GC: %s" % repository_name)
                try:
                    repository = gitutils.Repository.fromName(db, repository_name)
                    repository.packKeepaliveRefs()
                    repository.run("gc", "--prune=1 day", "--quiet")
                    repository.stopBatch()
                except Exception:
                    self.exception("repository GC failed: %s" % repository_name)

                if self.terminated:
                    return

            if configuration.extensions.ENABLED:
                now = time.time()
                max_age = 7 * 24 * 60 * 60

                base_path = os.path.join(configuration.paths.DATA_DIR,
                                         "temporary", "EXTENSIONS")

                for user_name in os.listdir(base_path):
                    user_dir = os.path.join(base_path, user_name)

                    for extension_id in os.listdir(user_dir):
                        extension_dir = os.path.join(user_dir, extension_id)

                        for repository_name in os.listdir(extension_dir):
                            repository_dir = os.path.join(extension_dir,
                                                          repository_name)
                            age = now - os.stat(repository_dir).st_mtime

                            if age > max_age:
                                self.info("Removing repository work copy: %s"
                                          % repository_dir)
                                shutil.rmtree(repository_dir)
예제 #4
0
    def __maintenance(self):
        with dbutils.Database() as db:
            cursor = db.cursor()

            # Update the UTC offsets of all timezones.
            #
            # The PostgreSQL database server has accurate (DST-adjusted) values,
            # but is very slow to query, so we cache the UTC offsets in our
            # 'timezones' table.  This call updates that cache every night.
            # (This is obviously a no-op most nights, but we don't want to have
            # to care about which nights it isn't.)
            self.debug("updating timezones")
            dbutils.updateTimezones(db)

            if self.terminated:
                return

            # Execute scheduled review branch archivals.
            if configuration.base.ARCHIVE_REVIEW_BRANCHES:
                repository = None

                cursor.execute(
                    """SELECT branches.repository, branches.id, branches.name
                                    FROM scheduledreviewbrancharchivals
                                    JOIN reviews ON (reviews.id=scheduledreviewbrancharchivals.review)
                                    JOIN branches ON (branches.id=reviews.branch)
                                   WHERE scheduledreviewbrancharchivals.deadline <= NOW()
                                     AND reviews.state IN ('closed', 'dropped')
                                     AND NOT branches.archived
                                ORDER BY branches.repository""",
                    for_update=True)

                for repository_id, branch_id, branch_name in cursor:
                    if not repository or repository.id != repository_id:
                        if repository:
                            repository.stopBatch()
                        repository = gitutils.Repository.fromId(
                            db, repository_id)
                        self.info("archiving branches in: " + repository.name)

                    self.info("  " + branch_name)

                    branch = dbutils.Branch.fromId(db,
                                                   branch_id,
                                                   repository=repository)

                    try:
                        branch.archive(db)
                    except Exception:
                        self.exception(as_warning=True)

                # Since NOW() returns the same value each time within a single
                # transaction, this is guaranteed to delete only the set of
                # archivals we selected above.
                cursor.execute("""DELETE
                                    FROM scheduledreviewbrancharchivals
                                   WHERE deadline <= NOW()""")

                db.commit()

            # Run a garbage collect in all Git repositories, to keep them neat
            # and tidy.  Also pack keepalive refs.
            cursor.execute("SELECT name FROM repositories")
            for (repository_name, ) in cursor:
                self.debug("repository GC: %s" % repository_name)
                try:
                    repository = gitutils.Repository.fromName(
                        db, repository_name)
                    repository.packKeepaliveRefs()
                    repository.run("gc", "--prune=1 day", "--quiet")
                    repository.stopBatch()
                except Exception:
                    self.exception("repository GC failed: %s" %
                                   repository_name)

                if self.terminated:
                    return

            if configuration.extensions.ENABLED:
                now = time.time()
                max_age = 7 * 24 * 60 * 60

                base_path = os.path.join(configuration.paths.DATA_DIR,
                                         "temporary", "EXTENSIONS")

                for user_name in os.listdir(base_path):
                    user_dir = os.path.join(base_path, user_name)

                    for extension_id in os.listdir(user_dir):
                        extension_dir = os.path.join(user_dir, extension_id)

                        for repository_name in os.listdir(extension_dir):
                            repository_dir = os.path.join(
                                extension_dir, repository_name)
                            age = now - os.stat(repository_dir).st_mtime

                            if age > max_age:
                                self.info("Removing repository work copy: %s" %
                                          repository_dir)
                                shutil.rmtree(repository_dir)