Skip to content

Commit f37a79a

Browse files
author
Marcin Mazurek
committed
[DDW-1088] Debounce entire fs.watchFile callback to reduce the resource usage
1 parent 9b48a33 commit f37a79a

File tree

1 file changed

+25
-32
lines changed

1 file changed

+25
-32
lines changed

source/main/utils/handleCheckBlockReplayProgress.ts

Lines changed: 25 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,10 @@ import path from 'path';
55
import { Tail } from 'tail';
66
import debounce from 'lodash/debounce';
77
import { getBlockSyncProgressChannel } from '../ipc/get-block-sync-progress';
8-
import type {
9-
GetBlockSyncProgressMainResponse,
10-
GetBlockSyncProgressRendererRequest,
11-
GetBlockSyncProgressType,
12-
} from '../../common/ipc/api';
8+
import type { GetBlockSyncProgressType } from '../../common/ipc/api';
139
import { BlockSyncType } from '../../common/types/cardano-node.types';
1410
import { isItFreshLog } from './blockSyncProgressHelpers';
1511
import { environment } from '../environment';
16-
import { MainIpcChannel } from '../ipc/lib/MainIpcChannel';
1712

1813
const blockKeyword = 'Replayed block';
1914
const validatingChunkKeyword = 'Validating chunk';
@@ -50,30 +45,8 @@ function getProgressType(line: string): GetBlockSyncProgressType | null {
5045

5146
const applicationStartDate = moment.utc();
5247

53-
const debouncedSyncProgress = debounce<
54-
MainIpcChannel<
55-
GetBlockSyncProgressRendererRequest,
56-
GetBlockSyncProgressMainResponse
57-
>['send']
58-
>((...args) => getBlockSyncProgressChannel.send(...args), 1000);
59-
60-
export const handleCheckBlockReplayProgress = (
61-
mainWindow: BrowserWindow,
62-
logsDirectoryPath: string
63-
) => {
64-
const filename = 'node.log';
65-
const logFilePath = `${logsDirectoryPath}/pub/`;
66-
const filePath = path.join(logFilePath, filename);
67-
if (!fs.existsSync(filePath)) return;
68-
69-
const tail = new Tail(filePath, {
70-
// using fs.watchFile instead of fs.watch on Windows because of Node API inconsistency:
71-
// https://nodejs.org/dist/latest-v14.x/docs/api/fs.html#fs_caveats
72-
// https://github.com/lucagrulla/node-tail/issues/137
73-
useWatchFile: true,
74-
});
75-
76-
tail.on('line', (line) => {
48+
const createHandleNewLine = (mainWindow: BrowserWindow) =>
49+
debounce((line: string) => {
7750
if (
7851
!isItFreshLog(applicationStartDate, line) ||
7952
!containProgressKeywords(line)
@@ -87,10 +60,30 @@ export const handleCheckBlockReplayProgress = (
8760
return;
8861
}
8962
const finalProgressPercentage = parseFloat(percentage);
90-
// Send result to renderer process (NetworkStatusStore)
91-
debouncedSyncProgress(
63+
64+
getBlockSyncProgressChannel.send(
9265
{ progress: finalProgressPercentage, type: progressType },
9366
mainWindow.webContents
9467
);
68+
}, 1000);
69+
70+
export const handleCheckBlockReplayProgress = (
71+
mainWindow: BrowserWindow,
72+
logsDirectoryPath: string
73+
) => {
74+
const filename = 'node.log';
75+
const logFilePath = `${logsDirectoryPath}/pub/`;
76+
const filePath = path.join(logFilePath, filename);
77+
if (!fs.existsSync(filePath)) return;
78+
79+
const tail = new Tail(filePath, {
80+
// using fs.watchFile instead of fs.watch on Windows because of Node API issues:
81+
// https://github.com/nodejs/node/issues/36888
82+
// https://github.com/lucagrulla/node-tail/issues/137
83+
// https://nodejs.org/dist/latest-v14.x/docs/api/fs.html#fs_caveats
84+
useWatchFile: environment.isWindows,
9585
});
86+
87+
const handleNewLine = createHandleNewLine(mainWindow);
88+
tail.on('line', handleNewLine);
9689
};

0 commit comments

Comments
 (0)