11import { BrowserWindow } from 'electron' ;
22import fs from 'fs' ;
3- import moment , { Moment } from 'moment' ;
4- import readline from 'readline' ;
3+ import moment from 'moment' ;
54import path from 'path' ;
5+ import { Tail } from 'tail' ;
66import { getBlockSyncProgressChannel } from '../ipc/get-block-sync-progress' ;
77import type { GetBlockSyncProgressType } from '../../common/ipc/api' ;
8- import { BLOCK_REPLAY_PROGRESS_CHECK_INTERVAL } from '../config' ;
98import { BlockSyncType } from '../../common/types/cardano-node.types' ;
109import { isItFreshLog } from './blockSyncProgressHelpers' ;
1110
@@ -48,30 +47,23 @@ export const handleCheckBlockReplayProgress = (
4847 mainWindow : BrowserWindow ,
4948 logsDirectoryPath : string
5049) => {
51- const checkBlockReplayProgress = async ( ) => {
52- const filename = 'node.log' ;
53- const logFilePath = `${ logsDirectoryPath } /pub/` ;
54- const filePath = path . join ( logFilePath , filename ) ;
55- if ( ! fs . existsSync ( filePath ) ) return ;
56- const fileStream = fs . createReadStream ( filePath ) ;
57- const rl = readline . createInterface ( {
58- input : fileStream ,
59- } ) ;
60- const progress = [ ] ;
50+ const filename = 'node.log' ;
51+ const logFilePath = `${ logsDirectoryPath } /pub/` ;
52+ const filePath = path . join ( logFilePath , filename ) ;
53+ if ( ! fs . existsSync ( filePath ) ) return ;
6154
62- for await ( const line of rl ) {
63- if (
64- containProgressKeywords ( line ) &&
65- isItFreshLog ( applicationStartDate , line )
66- ) {
67- progress . push ( line ) ;
68- }
55+ const tail = new Tail ( filePath ) ;
56+
57+ tail . on ( 'line' , ( line ) => {
58+ if (
59+ ! isItFreshLog ( applicationStartDate , line ) ||
60+ ! containProgressKeywords ( line )
61+ ) {
62+ return ;
6963 }
7064
71- if ( ! progress . length ) return ;
72- const finalProgress = progress . slice ( - 1 ) . pop ( ) ;
73- const percentage = finalProgress . match ( / P r o g r e s s : ( [ \s \d . , ] + ) % / ) ?. [ 1 ] ;
74- const progressType = getProgressType ( finalProgress ) ;
65+ const percentage = line . match ( / P r o g r e s s : ( [ \s \d . , ] + ) % / ) ?. [ 1 ] ;
66+ const progressType = getProgressType ( line ) ;
7567 if ( ! percentage || ! progressType ) {
7668 return ;
7769 }
@@ -81,15 +73,5 @@ export const handleCheckBlockReplayProgress = (
8173 { progress : finalProgressPercentage , type : progressType } ,
8274 mainWindow . webContents
8375 ) ;
84- } ;
85-
86- const setBlockReplayProgressCheckingInterval = ( ) => {
87- setInterval ( async ( ) => {
88- checkBlockReplayProgress ( ) ;
89- } , BLOCK_REPLAY_PROGRESS_CHECK_INTERVAL ) ;
90- } ;
91-
92- // Start default interval
93- setBlockReplayProgressCheckingInterval ( ) ;
94- return checkBlockReplayProgress ;
76+ } ) ;
9577} ;
0 commit comments