Browse Source

improve error message and ws trigger

Gitea 3 years ago
parent
commit
ad69082697

+ 1 - 1
client/src/context/WebSocket.tsx

@@ -18,7 +18,6 @@ const { Provider } = webSokcetContext;
 export const WebSocketProvider = (props: { children: React.ReactNode }) => {
   const [collections, setCollections] = useState<CollectionView[]>([]);
 
-  // test code for socket
   useEffect(() => {
     const socket = io(url);
 
@@ -52,6 +51,7 @@ export const WebSocketProvider = (props: { children: React.ReactNode }) => {
       }
     });
   }, []);
+
   return (
     <Provider
       value={{

+ 12 - 3
server/src/crons/crons.service.ts

@@ -21,8 +21,17 @@ export class CronsService {
 
   @Cron(CronExpression.EVERY_SECOND, { name: WS_EVENTS.COLLECTION })
   async getCollections() {
-    const res = await this.collectionService.getAllCollections();
-    this.eventService.server.emit(WS_EVENTS.COLLECTION, res);
-    return res;
+    try {
+      const res = await this.collectionService.getAllCollections();
+      this.eventService.server.emit(WS_EVENTS.COLLECTION, res);
+      return res;
+    } catch (error) {
+      // When user not connect milvus, stop cron
+      this.toggleCronJobByName({
+        name: WS_EVENTS.COLLECTION,
+        type: WS_EVENTS_TYPE.STOP,
+      });
+      throw new Error(error);
+    }
   }
 }

+ 10 - 0
server/src/milvus/milvus.service.ts

@@ -19,21 +19,31 @@ export class MilvusService {
   }
 
   get collectionManager() {
+    this.checkMilvus();
     return this.milvusClient.collectionManager;
   }
 
   get partitionManager() {
+    this.checkMilvus();
     return this.milvusClient.partitionManager;
   }
 
   get indexManager() {
+    this.checkMilvus();
     return this.milvusClient.indexManager;
   }
 
   get dataManager() {
+    this.checkMilvus();
     return this.milvusClient.dataManager;
   }
 
+  private checkMilvus() {
+    if (!this.milvusClient) {
+      throw new Error('Please connect milvus first');
+    }
+  }
+
   async connectMilvus(address: string) {
     // grpc only need address without http
     const milvusAddress = address.replace(/(http|https):\/\//, '');