def get(self):
     """Responds with the list of Jenkins masters in the blacklist.
     """
     response = dict()
     blacklist = list(redis.smembers(MASTER_BLACKLIST_KEY))
     response['blacklist'] = blacklist
     return self.respond(response, serialize=True)
    def post(self):
        """Adds or removes a master from the blacklist.

        The post params should include the `master_url` to be added or removed.
        By default, the master will be added to the blacklist. Set `remove` to
        be true to remove the master.

        Responds with the current blacklist and a warning message if it was a noop.
        """
        args = self.parser.parse_args()

        master = args.master_url
        remove = args.remove

        warning = ''
        if remove:
            if 0 == redis.srem(MASTER_BLACKLIST_KEY, master):
                warning = 'The master was already not on the blacklist'
        else:
            if 0 == redis.sadd(MASTER_BLACKLIST_KEY, master):
                warning = 'The master was already on the blacklist'

        response = dict()
        blacklist = list(redis.smembers(MASTER_BLACKLIST_KEY))
        response['blacklist'] = blacklist

        if warning:
            response['warning'] = warning
        return self.respond(response, serialize=True)
예제 #3
0
    def _pick_master(self, job_name, is_diff=False):
        """
        Identify a master to run the given job on.

        The master with the lowest queue for the given job is chosen. By random
        sorting the first empty queue will be prioritized.
        """
        candidate_urls = self.master_urls
        if is_diff and self.diff_urls:
            candidate_urls = self.diff_urls

        blacklist = redis.smembers(MASTER_BLACKLIST_KEY)
        master_urls = [c for c in candidate_urls if c not in blacklist]

        if len(master_urls) == 0:
            raise ValueError("No masters to pick from.")

        if len(master_urls) == 1:
            return master_urls[0]

        random.shuffle(master_urls)

        best_match = (sys.maxint, None)
        for url in master_urls:
            try:
                queued_jobs = self._count_queued_jobs(url, job_name)
            except:
                self.logger.exception(
                    "Couldn't count queued jobs on master %s", url)
                continue

            if queued_jobs == 0:
                return url

            if best_match[0] > queued_jobs:
                best_match = (queued_jobs, url)

        best = best_match[1]
        if not best:
            raise Exception(
                "Unable to successfully pick a master from {}.".format(
                    master_urls))
        return best
예제 #4
0
파일: builder.py 프로젝트: jhance/changes
    def _pick_master(self, job_name, is_diff=False):
        """
        Identify a master to run the given job on.

        The master with the lowest queue for the given job is chosen. By random
        sorting the first empty queue will be prioritized.
        """
        candidate_urls = self.master_urls
        if is_diff and self.diff_urls:
            candidate_urls = self.diff_urls

        blacklist = redis.smembers(MASTER_BLACKLIST_KEY)
        master_urls = [c for c in candidate_urls if c not in blacklist]

        if len(master_urls) == 0:
            raise ValueError("No masters to pick from.")

        if len(master_urls) == 1:
            return master_urls[0]

        random.shuffle(master_urls)

        best_match = (sys.maxint, None)
        for url in master_urls:
            try:
                queued_jobs = self._count_queued_jobs(url, job_name)
            except:
                self.logger.exception("Couldn't count queued jobs on master %s", url)
                continue

            if queued_jobs == 0:
                return url

            if best_match[0] > queued_jobs:
                best_match = (queued_jobs, url)

        best = best_match[1]
        if not best:
            raise Exception("Unable to successfully pick a master from {}.".format(master_urls))
        return best