Exemple #1
0
def _handle_update_subscription(message_data, message, context):
    """
    main aggregator handler for the 'update_subscription' message
    """
    try:
        updated_items = message_data.get('updated_items', [])
        if len(updated_items) == 0:
            log.debug('Ignoring subscription update with no updated items...')
            return

        cid = message_data.get('composite_id', None)
        if cid is None:
            log.debug('Ignoring subscription update with no composite id...')
            return
        
        bid = message_data.get('bucket_id', None)
        if bid is None:
            log.debug('Ignoring subscription update to %s with no bucket id...' % cid)
            return

        composite = Composite.get(cid, context)
        if composite is None or not 'Composite' in composite.document_types:
            log.error("Ignoring subscription update for non-existent composite %s" % cid)                
            return

        # check source 
        if not bid in composite.subscriptions:
            log.warn('Ignoring subscription update to %s for non-subscribed bucket %s' % (cid, bid))
        
        log.debug("updating %s (from bucket %s)" % (cid, bid))
        updated_refs = []
        for item in updated_items:
            ref = dict([(str(k), v) for k, v in item.items()])
            updated_refs.append(NewsItemRef.from_doc(ref, context))
        count = composite.filtered_update(updated_refs)
        if count > 0:
            try:
                composite.save()
            except ResourceConflict:
                # not a big deal in this case. This basically means
                # our timestamp did not become the latest -- we 
                # have made no alterations other than adding items.
                # Our additions succeed/fail independently of this as they
                # are separate documents.
                pass
    except:
        log.error("Error updating composite subscription %s: %s" % 
                  (message_data, traceback.format_exc()))
        raise
Exemple #2
0
    def init_subscription(self, bucket_id):
        sub_info = self.subscriptions.get(bucket_id, None)
        
        if sub_info is None:
            return 0 # not subscribed.
        
        stop_date = datetime.utcnow() - timedelta(days=1)
        query = {
            'startkey': [bucket_id, {}],
            'endkey': [bucket_id, DateTimeField()._to_json(stop_date)],
            'limit': 50,
            'descending': True,
            'include_docs': True,
        }
        initial_items = [NewsItemRef.from_doc(r.doc, self._context) for r in 
                         view_entries_by_timestamp(self._context.db, **query)]

        if len(initial_items) > 0:
            return self.filtered_update(initial_items)
        else:
            return 0