@@ -9,14 +9,18 @@ export class CronsService {
9
9
private schedulerRegistry : SchedulerRegistry
10
10
) { }
11
11
12
- async toggleCronJobByName ( data : { name : string ; type : WS_EVENTS_TYPE } ) {
13
- const { name, type } = data ;
12
+ async toggleCronJobByName ( data : {
13
+ name : string ;
14
+ type : WS_EVENTS_TYPE ;
15
+ address : string ;
16
+ } ) {
17
+ const { name, type, address } = data ;
14
18
15
19
switch ( name ) {
16
20
case WS_EVENTS . COLLECTION :
17
- const cronJobEntity = this . schedulerRegistry . getCronJob ( name ) ;
21
+ const cronJobEntity = this . schedulerRegistry . getCronJob ( name , address ) ;
18
22
if ( ! cronJobEntity && Number ( type ) === WS_EVENTS_TYPE . START ) {
19
- return this . getCollections ( WS_EVENTS . COLLECTION ) ;
23
+ return this . getCollections ( WS_EVENTS . COLLECTION , address ) ;
20
24
}
21
25
if ( ! cronJobEntity ) {
22
26
return ;
@@ -29,7 +33,7 @@ export class CronsService {
29
33
}
30
34
}
31
35
32
- async getCollections ( name : string ) {
36
+ async getCollections ( name : string , address : string ) {
33
37
const task = async ( ) => {
34
38
try {
35
39
const res = await this . collectionService . getAllCollections ( ) ;
@@ -42,29 +46,31 @@ export class CronsService {
42
46
return res ;
43
47
} catch ( error ) {
44
48
// When user not connect milvus, stop cron
45
- const cronJobEntity = this . schedulerRegistry . getCronJob ( name ) ;
49
+ const cronJobEntity = this . schedulerRegistry . getCronJob ( name , address ) ;
46
50
if ( cronJobEntity ) {
47
51
cronJobEntity . stop ( ) ;
48
52
}
49
53
50
54
throw new Error ( error ) ;
51
55
}
52
56
} ;
53
- this . schedulerRegistry . setCronJobEverySecond ( name , task ) ;
57
+ this . schedulerRegistry . setCronJobEverySecond ( name , task , address ) ;
54
58
}
55
59
}
56
60
57
61
export class SchedulerRegistry {
58
62
constructor ( private cronJobList : CronJob [ ] ) { }
59
63
60
- getCronJob ( name : string ) {
61
- const target = this . cronJobList . find ( ( item ) => item . name === name ) ;
64
+ getCronJob ( name : string , address : string ) {
65
+ const target = this . cronJobList . find (
66
+ item => item . name === name && item . address === address
67
+ ) ;
62
68
return target ?. entity ;
63
69
}
64
70
65
- setCronJobEverySecond ( name : string , func : ( ) => { } ) {
71
+ setCronJobEverySecond ( name : string , func : ( ) => { } , address : string ) {
66
72
// The cron job will run every second
67
- this . setCronJob ( name , '* * * * * *' , func ) ;
73
+ this . setCronJob ( name , '* * * * * *' , func , address ) ;
68
74
}
69
75
70
76
// ┌────────────── second (optional)
@@ -77,8 +83,10 @@ export class SchedulerRegistry {
77
83
// │ │ │ │ │ │
78
84
// * * * * * *
79
85
// https://www.npmjs.com/package/node-cron
80
- setCronJob ( name : string , scheduler : string , func : ( ) => { } ) {
81
- const target = this . cronJobList . find ( ( item ) => item . name === name ) ;
86
+ setCronJob ( name : string , scheduler : string , func : ( ) => { } , address : string ) {
87
+ const target = this . cronJobList . find (
88
+ item => item . name === name && item . address === address
89
+ ) ;
82
90
if ( target ) {
83
91
target ?. entity ?. stop ( ) ;
84
92
} else {
@@ -89,6 +97,7 @@ export class SchedulerRegistry {
89
97
this . cronJobList . push ( {
90
98
name,
91
99
entity : task ,
100
+ address,
92
101
} ) ;
93
102
}
94
103
}
@@ -97,4 +106,5 @@ export class SchedulerRegistry {
97
106
interface CronJob {
98
107
name : string ;
99
108
entity : ScheduledTask ;
109
+ address : string ; // milvus address
100
110
}
0 commit comments