1
1
/* eslint-disable no-console */
2
- /* eslint-disable n/no-process-exit */
3
2
4
3
import fetch from 'node-fetch'
5
4
import dotenv from 'dotenv'
@@ -9,16 +8,15 @@ dotenv.config()
9
8
const {
10
9
NEXTCLOUD_URL = 'http://nextcloud.local' ,
11
10
ADMIN_USER = 'admin' ,
12
- ADMIN_PASS = 'admin' ,
11
+ ADMIN_PASS = 'admin'
13
12
} = process . env
14
- const FORCE_CLOSE_TIMEOUT = 60 * 1000
15
13
16
- export let roomDataStore = { }
14
+ export const roomDataStore = { }
17
15
18
16
const fetchOptions = ( method , token , body = null ) => {
19
17
const headers = {
20
18
'Content-Type' : 'application/json' ,
21
- Authorization : `Bearer ${ token } ` ,
19
+ Authorization : `Bearer ${ token } `
22
20
}
23
21
24
22
if ( method === 'PUT' ) {
@@ -28,7 +26,7 @@ const fetchOptions = (method, token, body = null) => {
28
26
return {
29
27
method,
30
28
headers,
31
- ...( body && { body : JSON . stringify ( body ) } ) ,
29
+ ...( body && { body : JSON . stringify ( body ) } )
32
30
}
33
31
}
34
32
@@ -58,6 +56,7 @@ export const getRoomDataFromFile = async (roomID, socket) => {
58
56
return result ? result . data . elements : null
59
57
}
60
58
59
+ // Called when there's nobody in the room (No one keeping the latest data), BE to BE communication
61
60
export const saveRoomDataToFile = async ( roomID , data ) => {
62
61
console . log ( `Saving room data to file: ${ roomID } ` )
63
62
const url = `${ NEXTCLOUD_URL } /index.php/apps/whiteboard/${ roomID } `
@@ -67,27 +66,16 @@ export const saveRoomDataToFile = async (roomID, data) => {
67
66
await fetchData ( url , options )
68
67
}
69
68
69
+ // TODO: Should be called when the server is shutting down and a should be a BE to BE (or OS) communication
70
+ // in batch operation, run in background and check if it's necessary to save for each room.
71
+ // Should be called periodically and saved somewhere else for preventing data loss (memory loss, server crash, electricity cut, etc.)
70
72
export const saveAllRoomsData = async ( ) => {
73
+ }
74
+
75
+ export const removeAllRoomData = async ( ) => {
71
76
for ( const roomID in roomDataStore ) {
72
- if ( Object . prototype . hasOwnProperty . call ( roomDataStore , roomID ) && roomDataStore [ roomID ] ) {
73
- await saveRoomDataToFile ( roomID , roomDataStore [ roomID ] )
77
+ if ( Object . prototype . hasOwnProperty . call ( roomDataStore , roomID ) ) {
78
+ delete roomDataStore [ roomID ]
74
79
}
75
80
}
76
81
}
77
-
78
- export const gracefulShutdown = async ( server ) => {
79
- console . log ( 'Received shutdown signal, saving all data...' )
80
- await saveAllRoomsData ( )
81
- console . log ( 'All data saved, shutting down server...' )
82
- roomDataStore = { }
83
-
84
- server . close ( ( ) => {
85
- console . log ( 'HTTP server closed.' )
86
- process . exit ( 0 )
87
- } )
88
-
89
- setTimeout ( ( ) => {
90
- console . error ( 'Force closing server after 1 minute.' )
91
- process . exit ( 1 )
92
- } , FORCE_CLOSE_TIMEOUT )
93
- }
0 commit comments