@@ -87,6 +87,7 @@ export class SyncEngine {
8787 private pollingTimer : NodeJS . Timeout | null = null ;
8888 private wsClients : Set < { send : ( data : string ) => void } > = new Set ( ) ;
8989 private ignoreMatcherCache = new Map < string , picomatch . Matcher > ( ) ;
90+ private lastLogCleanup = 0 ;
9091
9192 constructor ( db : Database . Database ) {
9293 this . db = db ;
@@ -191,6 +192,7 @@ export class SyncEngine {
191192 await this . scanAllServicesForNewFiles ( ) ;
192193 await this . syncAllRepos ( ) ;
193194 await this . syncAllServices ( ) ;
195+ this . pruneOldSyncLogs ( ) ;
194196 } catch ( err ) {
195197 console . error ( 'Polling error:' , err ) ;
196198 }
@@ -200,12 +202,27 @@ export class SyncEngine {
200202 } , interval ) ;
201203 }
202204
205+ private pruneOldSyncLogs ( ) : void {
206+ const now = Date . now ( ) ;
207+ // Run cleanup at most once per hour
208+ if ( now - this . lastLogCleanup < 3_600_000 ) return ;
209+ this . lastLogCleanup = now ;
210+ try {
211+ this . db . prepare ( "DELETE FROM sync_log WHERE created_at < datetime('now', '-30 days')" ) . run ( ) ;
212+ } catch ( err ) {
213+ console . error ( 'Failed to prune sync_log:' , err ) ;
214+ }
215+ }
216+
203217 async stop ( ) : Promise < void > {
204218 if ( this . pollingTimer ) {
205219 clearTimeout ( this . pollingTimer ) ;
206220 this . pollingTimer = null ;
207221 }
222+ this . watcher . removeAllListeners ( ) ;
208223 await this . watcher . stopAll ( ) ;
224+ this . wsClients . clear ( ) ;
225+ this . ignoreMatcherCache . clear ( ) ;
209226 console . log ( 'Sync engine stopped' ) ;
210227 }
211228
0 commit comments