11import { SatelliteTokenService } from './satelliteTokenService' ;
2+ import type { FastifyBaseLogger } from 'fastify' ;
23
34/**
45 * Background service for cleaning up expired satellite registration tokens
@@ -7,33 +8,49 @@ import { SatelliteTokenService } from './satelliteTokenService';
78export class TokenCleanupService {
89 private static intervalId : NodeJS . Timeout | null = null ;
910 private static readonly DEFAULT_CLEANUP_INTERVAL_MS = 60 * 60 * 1000 ; // 1 hour
11+ private static logger : FastifyBaseLogger | null = null ;
1012
1113 /**
1214 * Start periodic cleanup of expired tokens
1315 * Runs every hour by default
1416 */
15- static start ( intervalMs : number = this . DEFAULT_CLEANUP_INTERVAL_MS ) {
17+ static start ( logger : FastifyBaseLogger , intervalMs : number = this . DEFAULT_CLEANUP_INTERVAL_MS ) {
1618 if ( this . intervalId ) {
17- console . log ( '🧹 Token cleanup service already running' ) ;
19+ logger . warn ( {
20+ operation : 'token_cleanup_start' ,
21+ status : 'already_running'
22+ } , '🧹 Token cleanup service already running' ) ;
1823 return ;
1924 }
2025
26+ this . logger = logger ;
27+
2128 // Run cleanup immediately on start
2229 this . runCleanup ( ) . catch ( error => {
23- console . error ( 'Initial token cleanup failed:' , error ) ;
30+ logger . error ( {
31+ operation : 'token_cleanup_initial' ,
32+ error
33+ } , 'Initial token cleanup failed' ) ;
2434 } ) ;
2535
2636 // Schedule periodic cleanup
2737 this . intervalId = setInterval ( async ( ) => {
2838 try {
2939 await this . runCleanup ( ) ;
3040 } catch ( error ) {
31- console . error ( 'Token cleanup failed:' , error ) ;
41+ logger . error ( {
42+ operation : 'token_cleanup_periodic' ,
43+ error
44+ } , 'Token cleanup failed' ) ;
3245 }
3346 } , intervalMs ) ;
3447
3548 const intervalHours = Math . round ( intervalMs / ( 60 * 60 * 1000 ) ) ;
36- console . log ( `🕒 Token cleanup service started (runs every ${ intervalHours } hour${ intervalHours !== 1 ? 's' : '' } )` ) ;
49+ logger . info ( {
50+ operation : 'token_cleanup_start' ,
51+ intervalMs,
52+ intervalHours
53+ } , `🕒 Token cleanup service started (runs every ${ intervalHours } hour${ intervalHours !== 1 ? 's' : '' } )` ) ;
3754 }
3855
3956 /**
@@ -43,7 +60,12 @@ export class TokenCleanupService {
4360 if ( this . intervalId ) {
4461 clearInterval ( this . intervalId ) ;
4562 this . intervalId = null ;
46- console . log ( '🛑 Token cleanup service stopped' ) ;
63+
64+ if ( this . logger ) {
65+ this . logger . info ( {
66+ operation : 'token_cleanup_stop'
67+ } , '🛑 Token cleanup service stopped' ) ;
68+ }
4769 }
4870 }
4971
@@ -53,12 +75,20 @@ export class TokenCleanupService {
5375 static async runCleanup ( ) : Promise < number > {
5476 try {
5577 const deletedCount = await SatelliteTokenService . cleanupExpiredTokens ( ) ;
56- if ( deletedCount > 0 ) {
57- console . log ( `🧹 Cleaned up ${ deletedCount } expired satellite registration token${ deletedCount !== 1 ? 's' : '' } ` ) ;
78+ if ( deletedCount > 0 && this . logger ) {
79+ this . logger . info ( {
80+ operation : 'token_cleanup_run' ,
81+ deletedCount
82+ } , `🧹 Cleaned up ${ deletedCount } expired satellite registration token${ deletedCount !== 1 ? 's' : '' } ` ) ;
5883 }
5984 return deletedCount ;
6085 } catch ( error ) {
61- console . error ( 'Token cleanup failed:' , error ) ;
86+ if ( this . logger ) {
87+ this . logger . error ( {
88+ operation : 'token_cleanup_run' ,
89+ error
90+ } , 'Token cleanup failed' ) ;
91+ }
6292 throw error ;
6393 }
6494 }
@@ -76,8 +106,8 @@ export class TokenCleanupService {
76106 /**
77107 * Restart the cleanup service with new interval
78108 */
79- static restart ( intervalMs ?: number ) {
109+ static restart ( logger : FastifyBaseLogger , intervalMs ?: number ) {
80110 this . stop ( ) ;
81- this . start ( intervalMs ) ;
111+ this . start ( logger , intervalMs ) ;
82112 }
83113}
0 commit comments