Example #1
0
async def init_cache_if_not_exists():
    key = SNAPSHOT_KEY_REDIS_KEY.format(today=today())
    if not await is_key_exists(key):
        lock_name = 'robot_snapshot'
        lock_value = await acquire_lock(lock_name, acquire_timeout=3)
        if lock_value and not await is_key_exists(key):
            await _robot_snapshot()
            await release_lock(lock_name, lock_value)
        else:  # 目前3秒未同步到缓存的用户返回404
            logger.info('acquire lock timeout, user need try againt')
            pass
Example #2
0
async def update_cache_after_bind_group(robot_id):
    key = ACTIVATED_KEY_REDIS_KEY.format(today=today())
    key_exists = await redis.conn.exists(key)
    if not key_exists:
        activate_count = await redis.conn.zadd(key, **{robot_id: 1})
        await redis.conn.expire(key, REDIS_EXPIRE_TIME)
    else:
        activate_count = await redis.conn.zincrby(key, robot_id, 1)

    max_time_key = SNAPSHOT_KEY_REDIS_KEY.format(today=today())
    max_time = await redis.conn.zscore(max_time_key, robot_id)

    distribute_key = DISTRIBUTE_KEY_REDIS_KEY.format(today=today())
    count = 0
    if max_time:
        count = int(max_time / DISPLAY_RATE - activate_count)
    if count <= 0:
        await remove_robot_distribute(robot_id)
    else:
        if await redis.conn.zscore(distribute_key, robot_id):
            await redis.conn.zadd(distribute_key, **{robot_id: count})
Example #3
0
async def _random_robot_distribution(count, channel=None):
    '''随机分配一个机器人'''
    distribute_key = DISTRIBUTE_KEY_REDIS_KEY.format(today=today())
    max_score = await _get_max_score(distribute_key, count)
    if not max_score:
        return None
    robot_id = await _choose_robot_id(distribute_key,
                                      max_score,
                                      channel=channel)
    if channel is not None and not robot_id:
        robot_id = await _choose_robot_id(distribute_key,
                                          max_score,
                                          channel=None)
    return robot_id
Example #4
0
async def update_join_and_retreat_redis_record(user_id, group_code,
                                               record_type):
    """更新群成员入群或退群redis记录"""
    key = STATISTICS_USER_GROUPS_REDIS_KEY.format(today=today())
    user_group_member_statistic = await redis.conn.hget(key, user_id)
    if user_group_member_statistic is None:
        value = {group_code: {record_type: 1}}
    else:
        val = user_group_member_statistic
        value = ujson.loads(val)
        if group_code in value:
            if record_type not in value[group_code]:
                value[group_code][record_type] = 1
            else:
                value[group_code][
                    record_type] = value[group_code][record_type] + 1
        else:
            value[group_code] = {record_type: 1}
    await redis.conn.hset(key, user_id, ujson.dumps(value))
    await redis.conn.expire(key, REDIS_EXPIRE_TIME)
Example #5
0
async def _specific_robot_distribution(robot_id=None, count=1):
    '''指定机器人额度查询与分配'''
    distribute_key = DISTRIBUTE_KEY_REDIS_KEY.format(today=today())
    robot_overage = await redis.conn.zscore(distribute_key, robot_id)
    if robot_overage and int(robot_overage) >= count:
        return robot_id
Example #6
0
async def remove_robot_distribute(robot_id):
    '''将机器人置为不可分配'''
    distribute_key = DISTRIBUTE_KEY_REDIS_KEY.format(today=today())
    await redis.conn.zrem(distribute_key, robot_id)
    logger.info(f'remove_robot_distribute --> [{robot_id}]')