def __call__(self):
        event = self.event
        queue_name = self.element.queue_name

        obj = event.object

        interpolator = IStringInterpolator(obj)

        body = interpolator(self.element.body.strip())

        if body:
            #send message to the RabbitMQ service
            rabbit = RabbitMQConnector(**rabbit_config)
            rabbit.open_connection()
            try:
                rabbit.declare_queue(queue_name)
                rabbit.send_message(queue_name, body)
            except Exception, err:
                logger.error(
                    'Sending \'%s\' in \'%s\' FAILED with error: %s',
                    body,
                    queue_name,
                    err)
            else:
                logger.info(
                    'Sending \'%s\' in \'%s\' OK',
                    body,
                    queue_name)
            rabbit.close_connection()
Esempio n. 2
0
class ProxyProducer:
    """ Proxy Producer: its function is to emulate
        the EEA Portal that sends messages.
    """

    def __init__(self, queue_name):
        """ """
        self.queue_name = queue_name
        self.rabbit = RabbitMQConnector(**rabbit_config)

    def send_messages(self, howmany):
        """ Senf a message to the queue
        """
        logger.info('STARTING to send messages in \'%s\'', self.queue_name)
        self.rabbit.open_connection()
        self.rabbit.declare_queue(self.queue_name)
        for idx in range(0, howmany):
            action = choice(actions)
            dataset_identifier, dataset_url = choice(datasets.items())
            body = '%(action)s|%(dataset_url)s|%(dataset_identifier)s' % {
                'action': action,
                'dataset_url': dataset_url,
                'dataset_identifier': dataset_identifier}
            logger.info('SENDING \'%s\' in \'%s\'', body, self.queue_name)
            self.rabbit.send_message(self.queue_name, body)
        self.rabbit.close_connection()
        logger.info('DONE sending messages in \'%s\'', self.queue_name)
Esempio n. 3
0
 def bulk_update(self):
     """ Queries SDS for all datasets and injects messages in rabbitmq.
     """
     logger.info('START bulk update')
     result_json, msg = self.query_all_datasets()
     if msg:
         logger.error('BULK update: %s', msg)
     else:
         datasets_json = result_json['results']['bindings']
         logger.info('BULK update: %s datasets found', len(datasets_json))
         rabbit = RabbitMQConnector(**rabbit_config)
         rabbit.open_connection()
         rabbit.declare_queue(self.queue_name)
         counter = 1
         for item_json in datasets_json:
             dataset_identifier = item_json['id']['value']
             dataset_url = item_json['dataset']['value']
             action = 'update'
             body = '%(action)s|%(dataset_url)s|%(dataset_identifier)s' % {
                 'action': action,
                 'dataset_url': dataset_url,
                 'dataset_identifier': dataset_identifier}
             logger.info('BULK update %s: sending \'%s\' in \'%s\'', counter, body, self.queue_name)
             rabbit.send_message(self.queue_name, body)
             counter += 1
         rabbit.close_connection()
     logger.info('DONE bulk update')
Esempio n. 4
0
    def __call__(self):
        related_action = self.element.related_action
        notification_subject = self.element.notification_subject
        notification_action = self.element.notification_action

        obj = self.event.object
        path = "/".join(obj.getPhysicalPath())

        tags = get_tags(obj)

        try:
            url = obj.absolute_url()
        except Exception:
            url = "N/A"

        try:
            content_title = obj.Title()
        except Exception:
            content_title = "N/A"

        try:
            actor = ContentHistoryView(
                obj,
                self.context.REQUEST).fullHistory()[0]['actor']['username']
        except Exception:
            try:
                actor = obj.Creator()
            except Exception:
                actor = "N/A"

        rabbit_config = get_rabbit_config()
        rabbit = RabbitMQConnector(**rabbit_config)
        try:
            rabbit.open_connection()
            rabbit.declare_queue(RABBIT_QUEUE)

            json_notification = {
                'notification_subject': notification_subject,
                'notification_action': notification_action,
                'content_url': url,
                'content_title': content_title,
                'actor': actor,
                'path': path,
                'tags': tags,
                'events': related_action,
            }
            message = json.dumps(json_notification)

            rabbit.send_message(RABBIT_QUEUE, message)
        except Exception:
            LOGGER.error(
                "RabbitMQ connection problem. "
                "Check client configuration: /@@rabbitmq-client-controlpanel."
                " See example in documentation.")
Esempio n. 5
0
def get_rabbitmq_conn(queue, context=None):
    """ Context manager to connect to RabbitMQ
    """

    if context is None:
        context = getSite()

    registry = getUtility(IRegistry, context=context)
    s = registry.forInterface(IRabbitMQClientSettings)

    rb = RabbitMQConnector(s.server, s.port, s.username, s.password)
    rb.open_connection()
    rb.declare_queue(queue)

    yield rb

    rb.close_connection()
    def __call__(self):
        event = self.event
        queue_name = self.element.queue_name

        obj = event.object

        interpolator = IStringInterpolator(obj)

        body = interpolator(self.element.body.strip())

        if body:
            #send message to the RabbitMQ service
            rabbit = RabbitMQConnector(**rabbit_config)
            rabbit.open_connection()
            try:
                rabbit.declare_queue(queue_name)
                rabbit.send_message(queue_name, body)
            except Exception, err:
                logger.error('Sending \'%s\' in \'%s\' FAILED with error: %s',
                             body, queue_name, err)
            else:
                logger.info('Sending \'%s\' in \'%s\' OK', body, queue_name)
            rabbit.close_connection()
def notifications_center_operations(site):
    """ All the operations of Notifications Center happen here
        Callable by both: browser view and script
    """
    def operations(message, site):
        def get_object(site, path):
            """ Return the object at given path
                or its first existing parent
            """
            obj = get_object_having_path(path)
            if obj is None:
                try:
                    path = "/".join(path.split("/")[:-1])
                    return get_object(site, path)
                except Exception:
                    return None
            else:
                if obj == site:
                    return None
                return obj
            return None

        msg = json.loads(message)
        """ This object is the object related to notification we want to send.
            Sometimes it is missing (example: on deleted items) so a parent
            will be used for that case.

            But the content_url is not changed, basically we just make sure
            the notification is sent.
        """
        obj = get_object(site, msg['path'])

        if obj is not None:
            catalog = get_catalog()
            users = catalog.search_users_by_preferences(
                tags=msg['tags'], events=[msg.get('events', '')], mode="or")
            for user_id in users:
                msg['user_id'] = user_id
                evt = SendEEANotificationEvent(obj, json.dumps(msg))
                notify(evt)
                close(evt)  # make sure it will work for multiple notify(
        else:
            LOGGER.error("Object with path {0} not found.".format(msg['path']))

        return True

    # Consume messages from queue
    rabbit_config = get_rabbit_config()
    rabbit = RabbitMQConnector(**rabbit_config)

    ok_configuration = True
    try:
        LOGGER.info('START consuming from \'%s\'', RABBIT_QUEUE)
        rabbit.open_connection()
        rabbit.declare_queue(RABBIT_QUEUE)
    except AttributeError:
        LOGGER.error("Wrong configuration for RabbitMQ client.")
        ok_configuration = False

    if ok_configuration:
        while True:
            method, properties, body = rabbit.get_message(RABBIT_QUEUE)
            if method is None and properties is None and body is None:
                LOGGER.info('Queue is empty \'%s\'.', RABBIT_QUEUE)
                break

            operations(body, site)
            rabbit.get_channel().basic_ack(delivery_tag=method.delivery_tag)

        rabbit.close_connection()
        LOGGER.info('DONE consuming from \'%s\'', RABBIT_QUEUE)
Esempio n. 8
0
class CKANClient:
    """ CKAN Client
    """

    def __init__(self, queue_name):
        """ """
        self.queue_name = queue_name
        self.rabbit = RabbitMQConnector(**rabbit_config)
        self.odp = ODPClient()
        self.sds = SDSClient(services_config['sds'], other_config['timeout'], queue_name, self.odp)

    def process_messages(self):
        """ Process all the messages from the queue and stop after
        """
        logger.info('START to process messages in \'%s\'', self.queue_name)
        self.rabbit.open_connection()
        print 'work in progress'
        self.rabbit.close_connection()
        logger.info('DONE processing messages in \'%s\'', self.queue_name)

    def start_consuming(self):
        """ Start consuming messages from the queue.
            It may be interrupted by stopping the script (CTRL+C).
        """
        logger.info('START consuming from \'%s\'', self.queue_name)
        self.rabbit.open_connection()
        self.rabbit.start_consuming(self.queue_name, self.message_callback)
        self.rabbit.close_connection()
        logger.info('DONE consuming from \'%s\'', self.queue_name)

    def start_consuming_ex(self):
        """ It will consume all the messages from the queue and stops after.
        """
        logger.info('START consuming from \'%s\'', self.queue_name)
        self.rabbit.open_connection()
        self.rabbit.declare_queue(self.queue_name)
        processed_messages = {}
        while True:
            method, properties, body = self.rabbit.get_message(self.queue_name)
            if method is None and properties is None and body is None:
                logger.info('Queue is empty \'%s\'.', self.queue_name)
                break
            if body not in processed_messages:
                flg = self.message_callback(self.rabbit.get_channel(), method, properties, body)
                if flg:
                    processed_messages[body] = 1
            else:
                #duplicate message, acknowledge to skip
                self.rabbit.get_channel().basic_ack(delivery_tag = method.delivery_tag)
                logger.info('DUPLICATE skipping message \'%s\' in \'%s\'',
                            body, self.queue_name)
        self.rabbit.close_connection()
        logger.info('DONE consuming from \'%s\'', self.queue_name)

    def message_callback(self, ch, method, properties, body):
        """ Callback method for processing a message from the queue.
            If the message is processed ok then acknowledge,
            otherwise don't - the message will be processed again
            at the next run.
            Returns True if the messages was processed ok, otherwise False.
        """
        resp = False
        logger.info('START processing message \'%s\' in \'%s\'',
                    body, self.queue_name)
        try:
            action, dataset_url, dataset_identifier = body.split('|')
        except Exception, err:
            logger.error('INVALID message format \'%s\' in \'%s\': %s',
                         body, self.queue_name, err)
        else: