Skip to content

Commit 325cf3d

Browse files
committed
Remove housekeeping code
1 parent 21c2c5c commit 325cf3d

File tree

2 files changed

+1
-194
lines changed

2 files changed

+1
-194
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"node": ">=14"
1616
},
1717
"scripts": {
18-
"start": "node dist/server.js",
18+
"start": "node src/server.js",
1919
"dev": "nodemon -r dotenv/config src/server.js",
2020
"test": "run-s test:lint",
2121
"test:lint": "eslint ."

src/notepadhandler.js

Lines changed: 0 additions & 193 deletions
Original file line numberDiff line numberDiff line change
@@ -849,122 +849,6 @@ export class NoteScreenConnection {
849849
}
850850
}
851851

852-
async houseKeeping() {
853-
let lock
854-
try {
855-
lock = await this.redlock.lock('housekeeping', 2000)
856-
console.log('Do saveChangedLectures')
857-
await this.saveChangedLectures()
858-
console.log('tryLectureRedisPurge')
859-
await this.tryLectureRedisPurge()
860-
console.log('House keeping done!')
861-
lock.unlock()
862-
} catch (error) {
863-
console.log('Busy or Error in Housekeeping', error)
864-
}
865-
}
866-
867-
async saveChangedLectures() {
868-
const client = this.redis
869-
const scan = promisify(this.redis.scan).bind(client)
870-
const hmget = promisify(this.redis.hmget).bind(client)
871-
try {
872-
let cursor = 0
873-
do {
874-
const scanret = await scan(
875-
cursor,
876-
'MATCH',
877-
'lecture:????????-????-????-????-????????????',
878-
'COUNT',
879-
20
880-
)
881-
// console.log("scanret", scanret);
882-
// got the lectures now figure out, which we need to save
883-
const saveproms = Promise.all(
884-
scanret[1].map(async (el) => {
885-
const info = await hmget(el, 'lastwrite', 'lastDBsave')
886-
// console.log("our info",info);
887-
if (info[0] > info[1] + 3 * 60 * 1000) {
888-
// do not save more often than every 3 minutes
889-
const lectureuuid = el.substr(8)
890-
return this.saveLectureToDB(lectureuuid)
891-
} else return null
892-
})
893-
)
894-
await saveproms // wait before next iteration, do not use up to much mem
895-
896-
cursor = scanret[0]
897-
} while (cursor !== '0')
898-
} catch (error) {
899-
console.log('Error saveChangedLecture', error)
900-
}
901-
}
902-
903-
async saveLectureToDB(lectureuuid) {
904-
const client = this.redis
905-
const smembers = promisify(this.redis.smembers).bind(client)
906-
const get = promisify(this.redis.get).bind(client)
907-
const hset = promisify(this.redis.hset).bind(client)
908-
const hget = promisify(this.redis.hget).bind(client)
909-
const time = Date.now()
910-
console.log('Try saveLectureToDB for lecture', lectureuuid)
911-
try {
912-
const lecturescol = this.mongo.collection('lectures')
913-
const boardscol = this.mongo.collection('lectureboards')
914-
// we got now through all boards and save them to the db
915-
916-
const boardprefix = 'lecture:' + lectureuuid + ':board'
917-
let update = []
918-
const backgroundp = hget('lecture:' + lectureuuid, 'backgroundbw')
919-
920-
const members = await smembers(boardprefix + 's')
921-
const copyprom = Promise.all(
922-
members.map(async (el) => {
923-
const boardname = el
924-
// if (boardname=="s") return null; // "boards excluded"
925-
// console.log("one board", el);
926-
// console.log("boardname", boardname);
927-
const boarddata = await get(Buffer.from(boardprefix + el))
928-
if (boarddata) {
929-
// got it now store it
930-
update = boardscol.updateOne(
931-
{ uuid: lectureuuid, board: boardname },
932-
{
933-
$set: {
934-
savetime: time,
935-
boarddata: boarddata
936-
}
937-
},
938-
{ upsert: true }
939-
)
940-
return Promise.all([boardname, update])
941-
} else return null
942-
})
943-
)
944-
update = update.concat(await copyprom) // reduces memory footprint
945-
946-
const allboards = update
947-
.filter((el) => !!el)
948-
.map((el) => (el ? el[0] : null))
949-
// console.log("allbaords", allboards);
950-
const backgroundbw = await backgroundp
951-
lecturescol.updateOne(
952-
{ uuid: lectureuuid },
953-
{
954-
$set: {
955-
boards: allboards,
956-
boardsavetime: time,
957-
backgroundbw: backgroundbw
958-
}
959-
}
960-
)
961-
await hset('lecture:' + lectureuuid, 'lastDBsave', Date.now())
962-
console.log('saveLectureToDB successful for lecture', lectureuuid)
963-
} catch (err) {
964-
console.log('saveLectToDBErr', err, lectureuuid)
965-
}
966-
}
967-
968852
async loadLectFromDB(lectureuuid) {
969853
const client = this.redis
970854
const hget = promisify(this.redis.hget).bind(client)
@@ -1053,83 +937,6 @@ export class NoteScreenConnection {
1053937
}
1054938
}
1055939

1056-
async tryLectureRedisPurge() {
1057-
const client = this.redis
1058-
// ok we got through all lectures and collect last access times
1059-
const scan = promisify(this.redis.scan).bind(client)
1060-
const hmget = promisify(this.redis.hmget).bind(client)
1061-
const unlink = promisify(this.redis.unlink).bind(client)
1062-
1063-
try {
1064-
let cursor = 0
1065-
const allprom = []
1066-
1067-
do {
1068-
const scanret = await scan(
1069-
cursor,
1070-
'MATCH',
1071-
'lecture:????????-????-????-????-????????????',
1072-
'COUNT',
1073-
40
1074-
)
1075-
// ok we figure out one by one if we should delete
1076-
// console.log("purge scanret", scanret);
1077-
const myprom = Promise.all(
1078-
scanret[1].map(async (el) => {
1079-
const lastaccessesp = []
1080-
1081-
lastaccessesp.push(hmget(el, 'lastwrite', 'lastaccess'))
1082-
1083-
// ok but also the notescreens are of interest
1084-
1085-
let cursor2 = 0
1086-
do {
1087-
const scanret2 = await scan(
1088-
cursor2,
1089-
'MATCH',
1090-
el + ':notescreen:????????-????-????-????-????????????'
1091-
)
1092-
// console.log("purge scanret2", scanret2);
1093-
const myprom2 = scanret2[1].map((el2) => {
1094-
return hmget(el2, 'lastaccess')
1095-
})
1096-
lastaccessesp.push(...myprom2)
1097-
1098-
cursor2 = scanret2[0]
1099-
} while (cursor2 !== '0')
1100-
1101-
let laarr = await Promise.all(lastaccessesp)
1102-
laarr = laarr.flat()
1103-
// console.log("laar",laarr);
1104-
const la = Math.max(...laarr)
1105-
// console.log("lastaccess",la,Date.now()-la );
1106-
const retprom = []
1107-
// console.log("before purge");
1108-
if (Date.now() - la > 30 * 60 * 1000) {
1109-
console.log('Starting to purge lecture ', el)
1110-
// purge allowed
1111-
retprom.push(unlink(el))
1112-
let pcursor = 0
1113-
do {
1114-
const pscanret = await scan(pcursor, 'MATCH', el + ':*')
1115-
console.log('purge element', pscanret)
1116-
pcursor = pscanret[0]
1117-
retprom.push(...pscanret[1].map((el2) => unlink(el2)))
1118-
} while (pcursor !== '0')
1119-
}
1120-
return Promise.all(retprom)
1121-
})
1122-
)
1123-
allprom.push(myprom)
1124-
cursor = scanret[0]
1125-
} while (cursor !== '0')
1126-
await Promise.all(allprom) // we are finished giving orders, wait for return
1127-
return
1128-
} catch (err) {
1129-
console.log('tryLectureRedisPurge error', err)
1130-
}
1131-
}
1132-
1133940
async sendBoardsToSocket(lectureuuid, socket) {
1134941
// we have to send first information about pictures
1135942

0 commit comments

Comments
 (0)