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)
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: