Browse Source

Merge branch 'test' of http://192.168.136.99:3000/servers/ASJServer into peng

peng 2 years ago
parent
commit
76c4998d2f

+ 2 - 6
Ansjer/cn_config/test_settings.py

@@ -86,22 +86,18 @@ DATABASES_PASS2 = 'p7XolzlQoOJ2YqvBqogr'
 
 DATABASES = {
     'default': {
-        'ENGINE': 'dj_db_conn_pool.backends.mysql',
+        'ENGINE': 'django.db.backends.mysql',
         'NAME': DATABASE_DATA,
         'USER': DATABASES_USER,
         'PASSWORD': DATABASES_PASS,
         'HOST': SERVER_HOST,
         'PORT': '3306',
         'AUTOCOMMIT': True,
+        'CONN_MAX_AGE': 60,
         'OPTIONS': {
             'charset': 'utf8mb4',
             'use_unicode': True,
             'init_command': "SET sql_mode='STRICT_TRANS_TABLES'"
-        },
-        'POOL_OPTIONS': {
-            'POOL_SIZE': 20,
-            'MAX_OVERFLOW': 50,
-            'RECYCLE': 60
         }
     },
     'mysql02': {

+ 37 - 5
Controller/UnicomCombo/UnicomComboTaskController.py

@@ -57,6 +57,8 @@ class UnicomComboTaskView(View):
             return response.json(0)
         elif operation == 'queryFlowUsedHistory':
             return self.query_flow_used_history(response)
+        elif operation == 'queryFlowCache':
+            return self.query_flow_cache(response)
 
     @classmethod
     def check_activate_combo(cls, request_dict, response):
@@ -117,10 +119,23 @@ class UnicomComboTaskView(View):
         """
         logger.info('--->进入监控流量使用情况')
         try:
-            unicom_api = UnicomObjeect()
             combo_order_qs = UnicomComboOrderInfo.objects.filter(status=1, is_del=False, combo__is_unlimited=0).values()
             if not combo_order_qs.exists():
                 return response.json(0)
+            asy = threading.Thread(target=UnicomComboTaskView.async_monitoring_flow, args=(combo_order_qs,))
+            asy.start()
+            return response.json(0)
+        except Exception as e:
+            logger.info('出错了~检测流量用量详情异常,errLine:{}, errMsg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
+            return response.json(177, repr(e))
+
+    @classmethod
+    def async_monitoring_flow(cls, combo_order_qs):
+        """
+        异步检测流量使用详情
+        """
+        try:
+            unicom_api = UnicomObjeect()
             today = datetime.datetime.today()
             year = today.year
             month = today.month
@@ -162,11 +177,8 @@ class UnicomComboTaskView(View):
                     if not activate_status:
                         # 停用
                         unicom_api.change_device_to_disable(iccid)
-
-            return response.json(0)
         except Exception as e:
-            logger.info('出错了~检测流量用量详情异常,errLine:{}, errMsg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
-            return response.json(177, repr(e))
+            logger.info('异步~检测流量用量详情异常,errLine:{}, errMsg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
 
     @staticmethod
     def flow_warning_push(app_user_id, serial_no, combo_order_id, flow_total, flow_usage):
@@ -390,3 +402,23 @@ class UnicomComboTaskView(View):
             # 批量创建IotCardUsageHistory对象
             if iot_card_list:
                 IotCardUsageHistory.objects.bulk_create(iot_card_list)
+
+    @classmethod
+    def query_flow_cache(cls, response):
+        """
+        查询流量缓存永久的将设置过期时间为10分钟
+        """
+        redis = RedisObject()
+        try:
+            res = redis.get_keys('ASJ:UNICOM:FLOW:*')
+            keys = [key.decode() for key in res]
+            # 进行进一步的处理或打印
+            for key in keys:
+                ttl = redis.get_ttl(key)
+                if ttl == -1:
+                    logger.info('iccidFlow:{}'.format(key))
+                    redis.CONN.expire(key, 60 * 10)
+            return response.json(0)
+        except Exception as e:
+            logger.info('出错了~次月激活套餐异常,errLine:{}, errMsg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
+            return response.json(177, repr(e))

+ 5 - 3
Object/UnicomObject.py

@@ -117,7 +117,8 @@ class UnicomObjeect:
         response_data = json.loads(response_data.text)
         token = response_data['access_token']
         expires_in = response_data['expires_in']
-        redis.set_data(UNICOM_KEY, token, int(expires_in))
+        redis.CONN.setnx(UNICOM_KEY, token)
+        redis.CONN.expire(UNICOM_KEY, int(expires_in))
         return token
 
     def refresh_token(self, refresh_token):
@@ -250,7 +251,7 @@ class UnicomObjeect:
         return None
 
     @staticmethod
-    def get_flow_total_usage(key, expire=0, **usage_data):
+    def get_flow_total_usage(key, expire=600, **usage_data):
         """
         设备当前队列用量详情(实现缓存)
         @param key: 缓存key
@@ -294,7 +295,8 @@ class UnicomObjeect:
                 sim_flow_used_total = sim_vo.sim_used_flow + cycle_total
             else:
                 sim_flow_used_total = sim_vo.sim_used_flow + sim_vo.sim_cycle_used_flow
-            redis.set_data(key=key, val=str(sim_flow_used_total), expire=expire)
+            redis.CONN.setnx(key, str(sim_flow_used_total))
+            redis.CONN.expire(key, expire)
         return sim_flow_used_total
 
     @staticmethod