Save cache to disk on SIGTERM/SIGINT
This commit is contained in:
		
							parent
							
								
									cec8445223
								
							
						
					
					
						commit
						796566e7ae
					
				| @ -16,14 +16,26 @@ class DiskCache { | |||||||
|   private static CHUNK_FILES = 25; |   private static CHUNK_FILES = 25; | ||||||
|   private isWritingCache = false; |   private isWritingCache = false; | ||||||
| 
 | 
 | ||||||
|   constructor() { } |   constructor() { | ||||||
|  |     if (!cluster.isMaster) { | ||||||
|  |       return; | ||||||
|  |     } | ||||||
|  |     process.on('SIGINT', (e) => { | ||||||
|  |       this.saveCacheToDiskSync(); | ||||||
|  |       process.exit(2); | ||||||
|  |     }); | ||||||
|  |     process.on('SIGTERM', (e) => { | ||||||
|  |       this.saveCacheToDiskSync(); | ||||||
|  |       process.exit(2); | ||||||
|  |     }); | ||||||
|  |   } | ||||||
| 
 | 
 | ||||||
|   async $saveCacheToDisk(): Promise<void> { |   async $saveCacheToDisk(): Promise<void> { | ||||||
|     if (!cluster.isPrimary) { |     if (!cluster.isPrimary) { | ||||||
|       return; |       return; | ||||||
|     } |     } | ||||||
|     if (this.isWritingCache) { |     if (this.isWritingCache) { | ||||||
|       logger.debug('Saving cache already in progress. Skipping.') |       logger.debug('Saving cache already in progress. Skipping.'); | ||||||
|       return; |       return; | ||||||
|     } |     } | ||||||
|     try { |     try { | ||||||
| @ -61,7 +73,50 @@ class DiskCache { | |||||||
|     } |     } | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   wipeCache() { |   saveCacheToDiskSync(): void { | ||||||
|  |     if (!cluster.isPrimary) { | ||||||
|  |       return; | ||||||
|  |     } | ||||||
|  |     if (this.isWritingCache) { | ||||||
|  |       logger.debug('Saving cache already in progress. Skipping.'); | ||||||
|  |       return; | ||||||
|  |     } | ||||||
|  |     try { | ||||||
|  |       logger.debug('Writing mempool and blocks data to disk cache (sync)...'); | ||||||
|  |       this.isWritingCache = true; | ||||||
|  | 
 | ||||||
|  |       const mempool = memPool.getMempool(); | ||||||
|  |       const mempoolArray: TransactionExtended[] = []; | ||||||
|  |       for (const tx in mempool) { | ||||||
|  |         mempoolArray.push(mempool[tx]); | ||||||
|  |       } | ||||||
|  | 
 | ||||||
|  |       Common.shuffleArray(mempoolArray); | ||||||
|  | 
 | ||||||
|  |       const chunkSize = Math.floor(mempoolArray.length / DiskCache.CHUNK_FILES); | ||||||
|  | 
 | ||||||
|  |       fs.writeFileSync(DiskCache.FILE_NAME, JSON.stringify({ | ||||||
|  |         cacheSchemaVersion: this.cacheSchemaVersion, | ||||||
|  |         blocks: blocks.getBlocks(), | ||||||
|  |         blockSummaries: blocks.getBlockSummaries(), | ||||||
|  |         mempool: {}, | ||||||
|  |         mempoolArray: mempoolArray.splice(0, chunkSize), | ||||||
|  |       }), { flag: 'w' }); | ||||||
|  |       for (let i = 1; i < DiskCache.CHUNK_FILES; i++) { | ||||||
|  |         fs.writeFileSync(DiskCache.FILE_NAMES.replace('{number}', i.toString()), JSON.stringify({ | ||||||
|  |           mempool: {}, | ||||||
|  |           mempoolArray: mempoolArray.splice(0, chunkSize), | ||||||
|  |         }), { flag: 'w' }); | ||||||
|  |       } | ||||||
|  |       logger.debug('Mempool and blocks data saved to disk cache'); | ||||||
|  |       this.isWritingCache = false; | ||||||
|  |     } catch (e) { | ||||||
|  |       logger.warn('Error writing to cache file: ' + (e instanceof Error ? e.message : e)); | ||||||
|  |       this.isWritingCache = false; | ||||||
|  |     } | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   wipeCache(): void { | ||||||
|     logger.notice(`Wiping nodejs backend cache/cache*.json files`); |     logger.notice(`Wiping nodejs backend cache/cache*.json files`); | ||||||
|     try { |     try { | ||||||
|       fs.unlinkSync(DiskCache.FILE_NAME); |       fs.unlinkSync(DiskCache.FILE_NAME); | ||||||
| @ -83,7 +138,7 @@ class DiskCache { | |||||||
|     } |     } | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   loadMempoolCache() { |   loadMempoolCache(): void { | ||||||
|     if (!fs.existsSync(DiskCache.FILE_NAME)) { |     if (!fs.existsSync(DiskCache.FILE_NAME)) { | ||||||
|       return; |       return; | ||||||
|     } |     } | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user