From eacaebd84b89c0b83d1f75fb61c283055c251a7f Mon Sep 17 00:00:00 2001 From: janis Date: Tue, 22 Aug 2023 19:21:40 +0200 Subject: [PATCH] add gc for db --- src/server/backend/db/db.js | 25 +++++++++++++++++++++++++ src/server/config/settings.config.json | 16 +++++++++++++++- 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/src/server/backend/db/db.js b/src/server/backend/db/db.js index 4e7bb52..9fa191e 100644 --- a/src/server/backend/db/db.js +++ b/src/server/backend/db/db.js @@ -185,5 +185,30 @@ module.exports.deleteJSONDataSimple = ( db, identifier ) => { } ); }; +const gc = () => { + // this function acts as the database garbage collector. TicketTimeout can be changed from the GUI. TODO: Add + this.getData( 'temp' ).then( tempData => { + let data = tempData; + console.info( 'Garbage Collecting' ); + for ( let item in data ) { + if ( new Date( data[ item ][ 'timestamp' ] ).getTime() + ( parseInt( settings.ticketTimeout ) * 1000 ) > new Date().getTime() ) { + delete data[ item ]; + console.debug( 'Garbage collected a session' ); + } + } + } ); +}; + +/* + Here, GC-Duty is scheduled to run every so often (defined in settings.config.json file, no GUI setting available. + If you are a developer and are thinking about adding a GUI setting for this, please consider that this is a + advanced setting and many people might not understand what it changes. The config file is mentioned in the + "advanced settings" section of the admin panel documentation where all the available settings in the config file + are explained in some detail.) +*/ +setInterval( () => { + gc(); +}, parseInt( settings.gcInterval ) ); + // TODO: Build garbage collector for DB (parse DB every so often (get from settings) // and delete all items where timestamp is older than a certain amount of time (get from settings)) \ No newline at end of file diff --git a/src/server/config/settings.config.json b/src/server/config/settings.config.json index e1540ed..0abe6f5 100644 --- a/src/server/config/settings.config.json +++ b/src/server/config/settings.config.json @@ -1 +1,15 @@ -{"init":true,"twoFA":"enforce","setupKey":"hello world","twoFAMode":"enhanced","db":"json","payments":"stripe","name":"libreevent","yourDomain":"http://localhost:8080","mailSender":"libreevent ","maxTickets":10,"currency":"CHF"} \ No newline at end of file +{ + "init":true, + "twoFA":"enforce", + "setupKey":"hello world", + "twoFAMode":"enhanced", + "db":"json", + "payments":"stripe", + "name":"libreevent", + "yourDomain":"http://localhost:8080", + "mailSender":"libreevent ", + "maxTickets":10, + "currency":"CHF", + "gcInterval": "600", + "ticketTimeout": "900" +} \ No newline at end of file