def update_site_box_stock(): from monservice.models import SiteBoxStock from rentservice.utils import logger from django.conf import settings from rentservice.utils.redistools import RedisTool import json log = logger.get_logger(__name__) redis_pool = RedisTool() log.info("update_site_box_stock: compute begin") REDIS_KEY_SITE_BOX_STOCK = 'site_box_stock_update' try: conn = redis_pool.get_connection() len_queue = conn.llen(REDIS_KEY_SITE_BOX_STOCK) log.info("update_site_box_stock: len_queue = %s" % len_queue) site_type_ava_num_hash = {} site_type_reserv_num_hash = {} if len_queue > 0: # log.debug("update_list = %s" % update_list) for index in xrange(0, len_queue): try: item = conn.lpop(REDIS_KEY_SITE_BOX_STOCK) item_dict = json.loads(item) tmp_key = str(item_dict['site']) + '#' + str( item_dict['box_type']) if tmp_key in site_type_ava_num_hash.keys(): try: site_type_ava_num_hash[tmp_key] += int( item_dict['ava_num']) except Exception, e: site_type_ava_num_hash[tmp_key] -= int( item_dict['ava_num']) log.error(repr(e)) log.error( "update_site_box_stock: restore initial value") else: site_type_ava_num_hash[tmp_key] = int( item_dict['ava_num']) if tmp_key in site_type_reserv_num_hash.keys(): try: site_type_reserv_num_hash[tmp_key] += int( item_dict['reserve_num']) except Exception, e: site_type_reserv_num_hash[tmp_key] -= int( item_dict['reserve_num']) log.error(repr(e)) log.error( "update_site_box_stock: restore initial value") else: site_type_reserv_num_hash[tmp_key] = int( item_dict['reserve_num']) except Exception, e: log.error(repr(e)) log.error( "update_site_box_stock: redis queue consume failure, push message into queue again" ) conn.rpush(REDIS_KEY_SITE_BOX_STOCK, item)
def update_smarttms_redis_auth_info(): from smarttms.models import AccessGroup, AccessUrlGroup, AuthUserGroup from smarttms.serializers import AccessGroupSerializer, AuthUserGroupSerializer, AccessUrlGroupSerializer from smarttms.utils import logger from smarttms.utils.redistools import RedisTool PERMISSION_GROUP_HASH = 'smarttms_permissions_group_hash' PERMISSION_URL_HASH = 'smarttms_permissions_url_hash' log = logger.get_logger(__name__) log.info("smarttms_update_redis_auth_info begin") try: redis_pool = RedisTool() conn = redis_pool.get_connection() access_group_ret = AccessGroup.objects.all() access_group_list = AccessGroupSerializer(access_group_ret, many=True).data groupid_group_dic = {} for item in access_group_list: groupid_group_dic[item['access_group_id']] = item['group'] ret = AuthUserGroup.objects.all() auth_user_group = AuthUserGroupSerializer(ret, many=True).data for item in auth_user_group: conn.hset(PERMISSION_GROUP_HASH, item['user_token'], groupid_group_dic[item['group']]) for item_access_group in access_group_list: ret = AccessUrlGroup.objects.filter( access_group__group=item_access_group['group']) access_url_list = [] access_url_group = AccessUrlGroupSerializer(ret, many=True).data for item in access_url_group: access_url_list.append(item['access_url_set']) if access_url_list: final_hash_value = ','.join(access_url_list) else: final_hash_value = '' if final_hash_value: conn.hset(PERMISSION_URL_HASH, item_access_group['group'], final_hash_value) except Exception, e: log.error(repr(e))
def get_connection_from_pool(): redis_pool = RedisTool() return redis_pool.get_connection()