@@ -356,13 +356,15 @@ class ErrsoleSequelize extends EventEmitter {
356356 *
357357 * @async
358358 * @function ensureLogsTTL
359- * @returns {Promise<{}> } - Resolves with an empty object once the TTL configuration is confirmed or updated.
359+ * @returns {Promise<{}> } - A promise that resolves with an empty object once the TTL configuration is confirmed or updated.
360360 */
361361 async ensureLogsTTL ( ) {
362362 const DEFAULT_TTL = 2592000000 ; // 30 days in milliseconds
363363 try {
364- const config = await this . errsoleConfig . findOne ( { where : { key : 'logsTTL' } , raw : true } ) ;
365- if ( ! config ) await this . errsoleConfig . create ( { key : 'logsTTL' , value : DEFAULT_TTL . toString ( ) } ) ;
364+ const configResult = await this . getConfig ( 'logsTTL' ) ;
365+ if ( ! configResult . item ) {
366+ await this . setConfig ( 'logsTTL' , DEFAULT_TTL . toString ( ) ) ;
367+ }
366368 this . deleteExpiredLogs ( ) ;
367369 } catch ( err ) {
368370 console . error ( err ) ;
@@ -378,26 +380,26 @@ class ErrsoleSequelize extends EventEmitter {
378380 */
379381 async deleteExpiredLogs ( ) {
380382 try {
381- const config = await this . errsoleConfig . findOne ( { where : { key : 'logsTTL' } , raw : true } ) ;
382- if ( ! config ) {
383+ const configResult = await this . getConfig ( 'logsTTL' ) ;
384+ if ( ! configResult . item ) {
383385 throw new Error ( 'Could not find the TTL configuration for logs.' ) ;
384386 }
385387
386- const logsTTL = parseInt ( config . value , 10 ) ;
388+ const logsTTL = parseInt ( configResult . item . value , 10 ) ;
387389 const expirationTime = new Date ( new Date ( ) . getTime ( ) - logsTTL ) ;
388- const deletedRowCount = await this . errsoleLogs . destroy ( {
389- where : {
390- timestamp : {
391- [ Sequelize . Op . lt ] : expirationTime
392- }
393- } ,
394- limit : 1000
395- } ) ;
396390
397- // Recurse based on whether logs were deleted
398- const delayTime = deletedRowCount ? 1000 : 3600000 ; // 3600000 ms equals 1 hour
399- await this . delay ( delayTime ) ;
400- this . deleteExpiredLogs ( ) ;
391+ while ( true ) {
392+ const deletedRowCount = await this . errsoleLogs . destroy ( {
393+ where : {
394+ timestamp : {
395+ [ Sequelize . Op . lt ] : expirationTime
396+ }
397+ } ,
398+ limit : 1000
399+ } ) ;
400+ const delayTime = deletedRowCount ? 1000 : 3600000 ; // 1 second if rows were deleted, otherwise 1 hour
401+ await this . delay ( delayTime ) ;
402+ }
401403 } catch ( err ) {
402404 console . error ( err ) ;
403405 }
0 commit comments