示例#1
0
 def test_set_and_del(self):
     """
     Test redis connection by writing, reading and deleting a value
     """
     r = redis.get_connection()
     r.set('foo', 'bar')
     self.assertEqual(r.get('foo'), b'bar')
     r.delete('foo')
     self.assertEqual(r.get('foo'), None)
示例#2
0
def extract_from_lead(lead_id):
    """
    A task to auto extract text and images from a lead.

    * Lead should have a valid attachment or url.
    * It needs to be checked whether this task from the same lead
      is already going on.
    * On extraction complete, subscribed users through websocket
      need to be notified.
    """

    # Use redis store to keep track of leads currently being extracted
    # and try to prevent useless parallel extraction of same lead that
    # that might happen.
    r = redis.get_connection()
    key = 'lead_extraction_{}'.format(lead_id)
    lock = 'lock_{}'.format(key)

    # Check if key exists and if so return, otherwise set the key ourself
    # Also use lock while doing this.
    with redis.get_lock(lock):
        if r.exists(key):
            logger.error('Lead Redis Locked')
            return False
        r.set(key, '1')

    try:
        # Actual extraction process
        return_value = _extract_from_lead_core(lead_id)

        # Send signal to all pending websocket clients
        # that the lead extraction has completed.

        code = SubscriptionConsumer.encode({
            'channel': 'leads',
            'event': 'onPreviewExtracted',
            'leadId': lead_id,
        })

        # TODO: Discuss and decide the notification response format
        # Also TODO: Should a handler be added during subscription
        # to immediately reply with already extracted lead?

        Group(code).send(
            json.loads(JSONRenderer().render({
                'code': code,
                'timestamp': timezone.now(),
                'type': 'notification',
                'status': return_value,
            }).decode('utf-8')))
    except Exception as e:
        logger.error(traceback.format_exc())
        return_value = False

    # Once done, we delete the key to say that this task is done.
    r.delete(key)
    return return_value
示例#3
0
def extract_from_file(file_preview_id):
    r = redis.get_connection()
    key = 'file_extraction_{}'.format(file_preview_id)
    lock = 'lock_{}'.format(key)

    with redis.get_lock(lock):
        if r.exists(key):
            return False
        r.set(key, '1')

    try:
        return_value = _extract_from_file_core(file_preview_id)
    except Exception as e:
        logger.error(traceback.format_exc())
        return_value = False

    r.delete(key)
    return return_value
示例#4
0
def load_geo_areas(region_id):
    r = redis.get_connection()
    key = 'load_geo_areas_{}'.format(region_id)
    lock = 'lock_{}'.format(key)

    with redis.get_lock(lock):
        if r.exists(key):
            logger.error('Geo Area Redis Locked')
            return False
        r.set(key, '1')

    try:
        return_value = _load_geo_areas(region_id)
    except Exception as e:
        logger.error(traceback.format_exc())
        return_value = False

    r.delete(key)
    return return_value
示例#5
0
文件: common.py 项目: the-deep/server
def get_redis_lock_ttl(lock):
    try:
        return timedelta(seconds=redis.get_connection().ttl(lock.name))
    except Exception:
        pass