Exemplo n.º 1
0
    def check_resource_limits(self):
        if self.percentage_amount and self.amount:
            if (self.selection_count > self.amount
                    and self.selection_count > self.percentage_amount
                    and self.op == "and"):
                raise ResourceLimitExceeded((
                    "policy:%s exceeded resource-limit:{limit} and percentage-limit:%s%% "
                    "found:{selection_count} total:{population_count}") %
                                            (self.p.name, self.percent),
                                            "max-resource and max-percent",
                                            self.amount, self.selection_count,
                                            self.population_count)

        if self.amount:
            if self.selection_count > self.amount and self.op != "and":
                raise ResourceLimitExceeded(
                    ("policy:%s exceeded resource-limit:{limit} "
                     "found:{selection_count} total: {population_count}") %
                    self.p.name, "max-resource", self.amount,
                    self.selection_count, self.population_count)

        if self.percentage_amount:
            if self.selection_count > self.percentage_amount and self.op != "and":
                raise ResourceLimitExceeded(
                    ("policy:%s exceeded resource-limit:{limit}%% "
                     "found:{selection_count} total:{population_count}") %
                    self.p.name, "max-percent", self.percent,
                    self.selection_count, self.population_count)
Exemplo n.º 2
0
    def check_resource_limit(self, selection_count, population_count):
        """Check if policy's execution affects more resources then its limit.

        Ideally this would be at a higher level but we've hidden
        filtering behind the resource manager facade for default usage.
        """
        p = self.ctx.policy
        if isinstance(p.max_resources, int) and selection_count > p.max_resources:
            raise ResourceLimitExceeded(
                ("policy: %s exceeded resource limit: {limit} "
                 "found: {selection_count}") % p.name,
                "max-resources", p.max_resources, selection_count, population_count)
        elif p.max_resources_percent:
            if (population_count * (
                    p.max_resources_percent / 100.0) < selection_count):
                raise ResourceLimitExceeded(
                    ("policy: %s exceeded resource limit: {limit}%% "
                     "found: {selection_count} total: {population_count}") % p.name,
                    "max-percent", p.max_resources_percent, selection_count, population_count)
        return True