示例#1
0
    def enforce(self):
        rule_spec = {'ref': self.rule.ref, 'id': str(self.rule.id), 'uid': self.rule.uid}
        enforcement_db = RuleEnforcementDB(trigger_instance_id=str(self.trigger_instance.id),
                                           rule=rule_spec)
        extra = {
            'trigger_instance_db': self.trigger_instance,
            'rule_db': self.rule
        }
        execution_db = None
        try:
            execution_db = self._do_enforce()
            # pylint: disable=no-member
            enforcement_db.execution_id = str(execution_db.id)
            enforcement_db.status = RULE_ENFORCEMENT_STATUS_SUCCEEDED
            extra['execution_db'] = execution_db
        except Exception as e:
            # Record the failure reason in the RuleEnforcement.
            enforcement_db.status = RULE_ENFORCEMENT_STATUS_FAILED
            enforcement_db.failure_reason = six.text_type(e)
            LOG.exception('Failed kicking off execution for rule %s.', self.rule, extra=extra)
        finally:
            self._update_enforcement(enforcement_db)

        # pylint: disable=no-member
        if not execution_db or execution_db.status not in EXEC_KICKED_OFF_STATES:
            LOG.audit('Rule enforcement failed. Execution of Action %s failed. '
                      'TriggerInstance: %s and Rule: %s',
                      self.rule.action.ref, self.trigger_instance, self.rule,
                      extra=extra)
        else:
            LOG.audit('Rule enforced. Execution %s, TriggerInstance %s and Rule %s.',
                      execution_db, self.trigger_instance, self.rule, extra=extra)

        return execution_db
示例#2
0
    def enforce(self):
        rule_spec = {'ref': self.rule.ref, 'id': str(self.rule.id), 'uid': self.rule.uid}
        enforcement_db = RuleEnforcementDB(trigger_instance_id=str(self.trigger_instance.id),
                                           rule=rule_spec)
        extra = {
            'trigger_instance_db': self.trigger_instance,
            'rule_db': self.rule
        }
        execution_db = None
        try:
            execution_db = self._do_enforce()
            # pylint: disable=no-member
            enforcement_db.execution_id = str(execution_db.id)
            enforcement_db.status = RULE_ENFORCEMENT_STATUS_SUCCEEDED
            extra['execution_db'] = execution_db
        except Exception as e:
            # Record the failure reason in the RuleEnforcement.
            enforcement_db.status = RULE_ENFORCEMENT_STATUS_FAILED
            enforcement_db.failure_reason = six.text_type(e)
            LOG.exception('Failed kicking off execution for rule %s.', self.rule, extra=extra)
        finally:
            self._update_enforcement(enforcement_db)

        # pylint: disable=no-member
        if not execution_db or execution_db.status not in EXEC_KICKED_OFF_STATES:
            LOG.audit('Rule enforcement failed. Execution of Action %s failed. '
                      'TriggerInstance: %s and Rule: %s',
                      self.rule.action.ref, self.trigger_instance, self.rule,
                      extra=extra)
        else:
            LOG.audit('Rule enforced. Execution %s, TriggerInstance %s and Rule %s.',
                      execution_db, self.trigger_instance, self.rule, extra=extra)

        return execution_db