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
@@ -44,34 +43,29 @@ function getProgressType(line: string): GetBlockSyncProgressType | null {
4443
4544const applicationStartDate = moment . utc ( ) ;
4645
47- export const handleCheckBlockReplayProgress = (
46+ export const handleCheckBlockReplayProgress = async (
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+ ! (
60+ isItFreshLog ( applicationStartDate , line ) &&
61+ containProgressKeywords ( line )
62+ )
63+ ) {
64+ return ;
6965 }
7066
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 ) ;
67+ const percentage = line . match ( / P r o g r e s s : ( [ \s \d . , ] + ) % / ) ?. [ 1 ] ;
68+ const progressType = getProgressType ( line ) ;
7569 if ( ! percentage || ! progressType ) {
7670 return ;
7771 }
@@ -81,22 +75,5 @@ export const handleCheckBlockReplayProgress = (
8175 { progress : finalProgressPercentage , type : progressType } ,
8276 mainWindow . webContents
8377 ) ;
84-
85- if (
86- finalProgressPercentage === 100 &&
87- progressType === BlockSyncType . pushingLedger
88- ) {
89- clearInterval ( checkBlockReplayProgressInterval ) ;
90- }
91- } ;
92-
93- const setBlockReplayProgressCheckingInterval = ( ) => {
94- return setInterval ( async ( ) => {
95- checkBlockReplayProgress ( ) ;
96- } , BLOCK_REPLAY_PROGRESS_CHECK_INTERVAL ) ;
97- } ;
98-
99- // Start default interval
100- const checkBlockReplayProgressInterval = setBlockReplayProgressCheckingInterval ( ) ;
101- return checkBlockReplayProgress ;
78+ } ) ;
10279} ;
0 commit comments