Exemplo n.º 1
0
    def process(self, channel, basic_deliver, properties, body, output):
        """
        Sends notifications to IRC.

        `Params Required`:
            * target: List of persons/channels who will receive the message.
            * msg: The message to send.
        """
        if not getattr(self, "_irc_comm", None):
            self.reject(basic_deliver, requeue=True)
            self.app_logger("Not connected to IRC yet. Putting message back on the bus.")
            return

        # Ack the original message
        self.ack(basic_deliver)
        corr_id = str(properties.correlation_id)
        # Notify we are starting
        self.send(properties.reply_to, corr_id, {"status": "started"}, exchange="")

        try:
            # Transform step format to notification format if needed
            if "parameters" in body.keys():
                self.app_logger.info("Received step message format." " Translating to notification format.")
                body = step_to_notification_format(body)

            required_keys = ("slug", "message", "phase", "target")
            try:
                # Remove target from this check
                for key in required_keys[0:3]:
                    if key not in body.keys():
                        raise KeyError()
                    if not type(body[key]) in types.StringTypes:
                        raise ValueError()
                # Check target on it's own since it's a list of strs
                if "target" not in body.keys():
                    raise KeyError()
                if type(body["target"]) is not list:
                    raise ValueError()
                for key in body["target"]:
                    if not type(key) in types.StringTypes:
                        raise ValueError()
            except KeyError:
                raise IRCNotifyWorkerError("Missing a required param. Requires: %s" % str(required_keys))
            except ValueError:
                raise IRCNotifyWorkerError("All inputs must be str.")

            output.info("Sending notification to %s on IRC" % ", ".join(body["target"]))
            for target in body["target"]:
                self._irc_comm.put((target, body["message"]))
            output.info("IRC notification sent!")
            self.app_logger.info("Finished IRC notification with no errors.")

        except IRCNotifyWorkerError, fwe:
            # If a IRCNotifyWorkerError happens send a failure, notify and log
            # the info for review.
            self.app_logger.error("Failure: %s" % fwe)

            self.send(properties.reply_to, corr_id, {"status": "failed"}, exchange="")
            output.error(str(fwe))
Exemplo n.º 2
0
    def test_step_to_notification_format(self):
        """
        Verify step format translates to notification format.
        """
        result = utils.step_to_notification_format({
            'parameters': {
                'slug': 'slug',
                'message': 'message',
                'phase': 'phase',
                'target': ['target'],
            }
        })

        assert result == {
            'slug': 'slug',
            'message': 'message',
            'phase': 'phase',
            'target': ['target'],
        }
Exemplo n.º 3
0
    def process(self, channel, basic_deliver, properties, body, output):
        """
        Sends notifications via email.

        `Params Required`:
            * target: List of persons/channels who will receive the message.
            * msg: The message to send.
        """
        # Ack the original message
        self.ack(basic_deliver)
        corr_id = str(properties.correlation_id)
        # Notify we are starting
        self.send(
            properties.reply_to, corr_id, {'status': 'started'}, exchange='')

        try:
            # Transform step format to notification format if needed
            if 'parameters' in body.keys():
                self.app_logger.info(
                    'Received step message format.'
                    ' Translating to notification format.')
                body = step_to_notification_format(body)

            required_keys = ('slug', 'message', 'phase', 'target')
            try:
                # Remove target from this check
                for key in required_keys[0:3]:
                    if key not in body.keys():
                        raise KeyError()
                    if not type(body[key]) in types.StringTypes:
                        raise ValueError()
                # Check target on it's own since it's a list of strs
                if 'target' not in body.keys():
                    raise KeyError()
                if type(body['target']) is not list:
                    raise ValueError()
                for key in body['target']:
                    # TODO: better verification that it's an email address
                    if not type(key) in types.StringTypes or '@' not in key:
                        raise ValueError()
            except KeyError:
                raise EmailNotifyWorkerError(
                    'Missing a required param. Requires: %s' % str(
                        required_keys))
            except ValueError:
                raise EmailNotifyWorkerError(
                    'All inputs must be str and valid addresses.')

            output.info('Sending notification to %s via email' % ", ".join(
                body['target']))

            for target in body['target']:
                self._send_msg(target, body['slug'], body['message'])

            output.info('Email notification sent!')
            self.app_logger.info('Finished Email notification with no errors.')

            self.send(
                properties.reply_to,
                corr_id,
                {'status': 'completed'},
                exchange=''
            )

        except EmailNotifyWorkerError, fwe:
            # If a EmailNotifyWorkerError happens send a failure,
            # notify and log  the info for review.
            self.app_logger.error('Failure: %s' % fwe)

            self.send(
                properties.reply_to,
                corr_id,
                {'status': 'failed'},
                exchange=''
            )
            output.error(str(fwe))