Procházet zdrojové kódy

LDS server 增加异常处理

ChenHaolong před 1 rokem
rodič
revize
49220b1de6
3 změnil soubory, kde provedl 53 přidání a 28 odebrání
  1. 1 1
      proto
  2. 47 22
      src/main.py
  3. 5 5
      测试.ipynb

+ 1 - 1
proto

@@ -1 +1 @@
-Subproject commit 2209526c85110743b49c506bce2e4b636b3dd46e
+Subproject commit 07aa4680f617cff9ccca2ef7f5f303ab744d1c45

+ 47 - 22
src/main.py

@@ -101,9 +101,12 @@ def lds_config_from_grpc(request):
 def lds_uuids_to_grpc(res):
     uuids = []
     results = res.get("results")
-    for result in results:
-        uuids.append(result.get("uuid"))
-    return uuids
+    if results is None:
+        return ListLDSResponse(uuids=uuids)
+    else:
+        for result in results:
+            uuids.append(result.get("uuid"))
+        return ListLDSResponse(uuids=uuids)
 
 
 def check_error(res, context):
@@ -115,11 +118,22 @@ def check_error(res, context):
                 context.abort(grpc.StatusCode.UNKNOWN, json.dumps(res))
 
 
-def check_lds_result(res):
-    if res.get('isEnabled', True):
-        return True
+def check_lds_create_result(res):
+    if res.get('isEnabled') is None:
+        return CreateLDSResponse(isSuccess=False)
     else:
-        return False
+        if res.get('isEnabled') is True:
+            return CreateLDSResponse(isSuccess=True)
+        else:
+            return CreateLDSResponse(isSuccess=False)
+
+
+def check_lds_delete_result(res_code):
+    print("响应码:", res_code.status_code)
+    if res_code.status_code == 200:
+        return DeleteLDSResponse(isSuccess=True)
+    else:
+        return DeleteLDSResponse(isSuccess=False)
 
 
 class DeliveryServiceServicer(llnw_pb2_grpc.DeliveryServiceServicer):
@@ -163,26 +177,37 @@ class DeliveryServiceServicer(llnw_pb2_grpc.DeliveryServiceServicer):
 class LogDeliveryServiceServicer(llnwLDS_pb2_grpc.LogDeliveryServiceServicer):
 
     def CreateLDS(self, request, context):
-        print("请求:{}", request)
-        lds_config = lds_config_from_grpc(request)
-        res = get_cl(request.base).create_lds(request.shortname, lds_config).json()
-        print("响应:{}", res)
-        return check_lds_result(res)
+        print("CreateLDS")
+        print("请求:", request)
+        try:
+            lds_config = lds_config_from_grpc(request)
+            res = get_cl(request.base).create_lds(shortname=request.shortName, config={"body": lds_config}).json()
+            print("响应:", res)
+        except:
+            return CreateLDSResponse(isSuccess=False)
+        else:
+            return check_lds_create_result(res)
 
     def DeleteLDS(self, request, context):
-        print("请求:{}", request)
-        res = get_cl(request.base).delete_lds(request.shortname, request.uuid).json()
-        print("响应:{}", res)
-        if res.get('success', True):
-            return True
+        print("DeleteLDS")
+        print("请求:", request)
+        try:
+            res_code = get_cl(request.base).delete_lds(shortname=request.shortName, uuid=request.uuid)
+        except:
+            return DeleteLDSResponse(isSuccess=False)
         else:
-            return False
+            return check_lds_delete_result(res_code)
 
     def ListLDS(self, request, context):
-        print("请求:{}", request)
-        res = get_cl(request.base).list_lds(request.shortname).json()
-        print("响应:{}", res)
-        return lds_uuids_to_grpc(res)
+        print("ListLDS")
+        print("请求:", request)
+        try:
+            res = get_cl(request.base).list_lds(shortname=request.shortName).json()
+            print("响应:", res)
+        except:
+            return ListLDSResponse(uuids=[])
+        else:
+            return lds_uuids_to_grpc(res)
 
 
 if __name__ == '__main__':

+ 5 - 5
测试.ipynb

@@ -403,13 +403,13 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 34,
+   "execution_count": 37,
    "outputs": [
     {
      "name": "stdout",
      "output_type": "stream",
      "text": [
-      "{'results': [], 'size': 'all', 'offset': 0, 'page': 1, 'total': 0, 'sort': '_id', 'sort.dir': 'ascending', '$prev': '', '$next': ''}\n"
+      "<Response [200]>\n"
      ]
     }
    ],
@@ -438,11 +438,11 @@
     "    \"storage\": storage\n",
     "}\n",
     "\n",
-    "uuid = \"97a3db7d-86b2-4302-92cb-3fc7bfa81a60\"\n",
+    "uuid = \"1af891bf-cf7e-4294-ad66-f63721d33db9\"\n",
     "\n",
-    "# print(cl.create_lds(shortname=short_name, config={\"body\":config}).json())\n",
+    "# print(cl.create_lds(shortname=short_name, config={\"body\": config}).json())\n",
     "# print(cl.list_lds(shortname=short_name).json())\n",
-    "# print(cl.delete_lds(shortname=short_name, uuid=uuid).json())"
+    "print(cl.delete_lds(shortname=short_name, uuid=uuid))"
    ],
    "metadata": {
     "collapsed": false