@@ -11,55 +11,63 @@ const connectionsGauge = new MetricsGauge({
1111 help : 'Number of open ddp connections' ,
1212} )
1313
14- Meteor . onConnection ( ( conn : Meteor . Connection ) => {
14+ /**
15+ * Track a newly-opened DDP connection (from either Meteor's built-in server or the standalone DDP
16+ * server). Updates the open-connection metrics.
17+ */
18+ export function trackConnectionOpen ( connectionId : string , clientAddress : string ) : void {
1519 // This is called whenever a new ddp-connection is opened (ie a web-client or a peripheral-device)
20+ connections . add ( connectionId )
21+ logger . debug ( `Client connected: "${ connectionId } ", "${ clientAddress } "` )
22+ traceConnections ( )
23+ }
1624
17- const connectionId : string = conn . id
18- // var clientAddress = conn.clientAddress; // ip-adress
19-
20- connections . add ( conn . id )
21- logger . debug ( `Client connected: "${ conn . id } ", "${ conn . clientAddress } "` )
25+ /**
26+ * Track a closed DDP connection. Updates metrics, and marks any PeripheralDevices that were bound to this
27+ * connection (and their children) as offline.
28+ */
29+ export function trackConnectionClose ( connectionId : string , clientAddress : string ) : void {
30+ connections . delete ( connectionId )
31+ logger . debug ( `Client disconnected: "${ connectionId } ", "${ clientAddress } "` )
2232 traceConnections ( )
2333
24- conn . onClose ( ( ) => {
25- // called when a connection is closed
26- connections . delete ( conn . id )
27- logger . debug ( `Client disconnected: "${ conn . id } ", "${ conn . clientAddress } "` )
28- traceConnections ( )
34+ if ( ! connectionId ) return
2935
30- if ( connectionId ) {
31- deferAsync ( async ( ) => {
32- const devices = await PeripheralDevices . findFetchAsync ( {
33- connectionId : connectionId ,
34- } )
36+ deferAsync ( async ( ) => {
37+ const devices = await PeripheralDevices . findFetchAsync ( {
38+ connectionId : connectionId ,
39+ } )
3540
36- for ( const device of devices ) {
37- // set the status of the machine to offline:
41+ for ( const device of devices ) {
42+ // set the status of the machine to offline:
3843
39- await PeripheralDevices . updateAsync ( device . _id , {
40- $set : {
41- lastSeen : getCurrentTime ( ) ,
42- connected : false ,
43- // connectionId: ''
44- } ,
45- } )
46- await PeripheralDevices . updateAsync (
47- {
48- parentDeviceId : device . _id ,
49- } ,
50- {
51- $set : {
52- lastSeen : getCurrentTime ( ) ,
53- connected : false ,
54- // connectionId: ''
55- } ,
56- } ,
57- { multi : true }
58- )
59- }
44+ await PeripheralDevices . updateAsync ( device . _id , {
45+ $set : {
46+ lastSeen : getCurrentTime ( ) ,
47+ connected : false ,
48+ // connectionId: ''
49+ } ,
6050 } )
51+ await PeripheralDevices . updateAsync (
52+ {
53+ parentDeviceId : device . _id ,
54+ } ,
55+ {
56+ $set : {
57+ lastSeen : getCurrentTime ( ) ,
58+ connected : false ,
59+ // connectionId: ''
60+ } ,
61+ } ,
62+ { multi : true }
63+ )
6164 }
6265 } )
66+ }
67+
68+ Meteor . onConnection ( ( conn : Meteor . Connection ) => {
69+ trackConnectionOpen ( conn . id , conn . clientAddress )
70+ conn . onClose ( ( ) => trackConnectionClose ( conn . id , conn . clientAddress ) )
6371} )
6472
6573let logTimeout : number | undefined = undefined
0 commit comments