File tree Expand file tree Collapse file tree 1 file changed +31
-0
lines changed Expand file tree Collapse file tree 1 file changed +31
-0
lines changed Original file line number Diff line number Diff line change 1
1
async function connect ( ctx ) {
2
2
if ( ctx . sse ) {
3
+ // likely not the best way to do this
4
+ // TODO: fork koa-sse and move this into the ping interval
5
+ // runs every 60s
6
+ const interval = setInterval ( ( ) => {
7
+ const connected = ctx . sse . send ( {
8
+ event : 'status' ,
9
+ data : isActive ( ctx )
10
+ } ) ;
11
+
12
+ console . log ( 'connected' ) ;
13
+
14
+ // clear the interval if the client is disconnected
15
+ if ( ! connected ) {
16
+ clearInterval ( interval ) ;
17
+ }
18
+ } , 60_000 ) ;
19
+ ctx . sse . send ( {
20
+ event : 'status' ,
21
+ data : isActive ( ctx )
22
+ } ) ;
23
+
3
24
// send bree events over sse
4
25
for ( const event of [ 'worker created' , 'worker deleted' ] ) {
5
26
ctx . bree . on ( event , ( name ) => {
@@ -9,8 +30,18 @@ async function connect(ctx) {
9
30
10
31
ctx . sse . on ( 'close' , ( ) => {
11
32
ctx . logger . error ( 'SSE closed' ) ;
33
+
34
+ clearInterval ( interval ) ;
12
35
} ) ;
13
36
}
14
37
}
15
38
39
+ function isActive ( ctx ) {
40
+ return Boolean (
41
+ ctx . bree . workers . size > 0 ||
42
+ ctx . bree . timeouts . size > 0 ||
43
+ ctx . bree . intervals . size > 0
44
+ ) ;
45
+ }
46
+
16
47
module . exports = { connect } ;
You can’t perform that action at this time.
0 commit comments