Пример #1
0
 def _handle_scim_data(self, method, body):
     """
     """
     data_dict = json.loads(body)
     jti = data_dict.get('jti', 'unknown-jti')
     nbf = data_dict.get('nbf')
     if nbf is None:
         self._logger.debug(
             'Message {jti} contains no scheduling data'.format(
                 jti=jti))
         return False
     if not nbf or not str(nbf).isdigit():
         self._logger.warn(
             'Message {jti} contains invalid or empty "nbf" field'.format(
                 jti=jti))
         return False
     eta = datetime.datetime.fromtimestamp(int(nbf))
     result_ticket = schedule_message.apply_async(
         kwargs={'routing_key': method.routing_key,
                 'body': body},
         eta=eta)
     self._logger.info(
         'Scheduled {jti} for {eta} as {ticket_id}'.format(
             jti=jti,
             eta=eta,
             ticket_id=result_ticket.id))
     return True
Пример #2
0
    def publish(self, messages, durable=True):
        """
        Publish a message to the exchange after scheduling it
        in case it is marked for scheduling

        :type messages: dict, scim.Event or list of dicts and / or scim.Event
        :param messages: The message(s) to publish

        :type durable: bool
        :param durable: If this message should be durable

        :rtype: dict
        :return: A dict of jti:(celery.result.AsyncResult, eta)
        """
        from Cerebrum.modules.celery_tasks.apps.scheduler import schedule_message
        super(SchedulingAndPublishingAMQP091Client,
              self).publish(messages, durable)
        if isinstance(messages, (dict, scim.Event)):
            messages = [messages]
        elif not isinstance(messages, list):
            raise TypeError('messages must be a dict, event or a list thereof')
        result_tickets = dict()
        for message in messages:
            if not isinstance(message, (dict, scim.Event)):
                raise TypeError('messages must be a dict, '
                                'Event or a list thereof')
            try:
                if isinstance(message, dict):
                    err_msg = 'Could not extract schedule time'
                    if 'nbf' not in message:
                        continue
                    eta = datetime.datetime.fromtimestamp(int(message['nbf']))
                    jti = message.get('jti', 'invalid')
                    err_msg = 'Could not get routing-key'
                    routing_key = message.get('routing-key', 'unknown')
                    err_msg = 'Could not produce message-body'
                    body = json.dumps(
                        dict(
                            filter(lambda x: x[0] != 'routing-key',
                                   message.items())))
                else:
                    # scim.Event
                    err_msg = 'Could not extract schedule time'
                    if not isinstance(message.scheduled, datetime.datetime):
                        continue
                    eta = message.scheduled
                    jti = message.jti or 'invalid'
                    err_msg = 'Could not get routing-key'
                    routing_key = message.key
                    err_msg = 'Could not produce message-body'
                    body = json.dumps(message.get_payload())
                err_msg = 'Could not schedule / produce task'
                result_tickets[jti] = (schedule_message.apply_async(kwargs={
                    'routing_key':
                    routing_key,
                    'body':
                    body
                },
                                                                    eta=eta),
                                       eta)
            except Exception as e:
                raise ClientErrors.MessageFormatError('{0}: {1}'.format(
                    err_msg, e))
        return result_tickets
Пример #3
0
    def publish(self, messages, durable=True):
        """
        Publish a message to the exchange after scheduling it
        in case it is marked for scheduling

        :type messages: dict, scim.Event or list of dicts and / or scim.Event
        :param messages: The message(s) to publish

        :type durable: bool
        :param durable: If this message should be durable

        :rtype: dict
        :return: A dict of jti:(celery.result.AsyncResult, eta)
        """
        from Cerebrum.modules.celery_tasks.apps.scheduler import schedule_message
        super(SchedulingAndPublishingAMQP091Client, self).publish(
            messages,
            durable)
        if isinstance(messages, (dict, scim.Event)):
            messages = [messages]
        elif not isinstance(messages, list):
            raise TypeError('messages must be a dict, event or a list thereof')
        result_tickets = dict()
        for message in messages:
            if not isinstance(message, (dict, scim.Event)):
                raise TypeError('messages must be a dict, '
                                'Event or a list thereof')
            try:
                if isinstance(message, dict):
                    err_msg = 'Could not extract schedule time'
                    if 'nbf' not in message:
                        continue
                    eta = datetime.datetime.fromtimestamp(int(message['nbf']))
                    jti = message.get('jti', 'invalid')
                    err_msg = 'Could not get routing-key'
                    routing_key = message.get('routing-key', 'unknown')
                    err_msg = 'Could not produce message-body'
                    body = json.dumps(dict(
                        filter(lambda x: x[0] != 'routing-key',
                               message.items())))
                else:
                    # scim.Event
                    err_msg = 'Could not extract schedule time'
                    if not isinstance(message.scheduled, datetime.datetime):
                        continue
                    eta = message.scheduled
                    jti = message.jti or 'invalid'
                    err_msg = 'Could not get routing-key'
                    routing_key = message.key
                    err_msg = 'Could not produce message-body'
                    body = json.dumps(message.get_payload())
                err_msg = 'Could not schedule / produce task'
                result_tickets[jti] = (schedule_message.apply_async(
                    kwargs={'routing_key': routing_key,
                            'body': body},
                    eta=eta), eta)
            except Exception as e:
                raise ClientErrors.MessageFormatError('{0}: {1}'.format(
                    err_msg,
                    e))
        return result_tickets