Skip to content

Commit 1b42c93

Browse files
author
Lucas Araujo
committed
[DDW-812] Remove interval and watch for new log file lines
1 parent 8443e76 commit 1b42c93

File tree

6 files changed

+28
-49
lines changed

6 files changed

+28
-49
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,7 @@
271271
"shasum": "1.0.2",
272272
"source-map-support": "0.5.19",
273273
"spectron-fake-dialog": "0.0.1",
274+
"tail": "2.2.4",
274275
"tcp-port-used": "1.0.1",
275276
"trezor-connect": "8.2.4-extended",
276277
"unorm": "1.6.0",

source/main/config.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,6 @@ export const DISK_SPACE_RECOMMENDED_PERCENTAGE = 15; // 15% of the total disk sp
172172

173173
export const DISK_SPACE_CHECK_TIMEOUT = 9 * 1000; // Timeout for checking disks pace
174174

175-
export const BLOCK_REPLAY_PROGRESS_CHECK_INTERVAL = 1 * 1000; // 1 seconds | unit: milliseconds
176-
177175
// Used if token metadata server URL is not defined in launcher config
178176
export const FALLBACK_TOKEN_METADATA_SERVER_URL =
179177
'https://metadata.cardano-testnet.iohkdev.io';

source/main/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ const onAppReady = async () => {
248248
};
249249

250250
mainErrorHandler(onMainError);
251-
await handleCheckBlockReplayProgress(mainWindow, launcherConfig.logsPrefix);
251+
handleCheckBlockReplayProgress(mainWindow, launcherConfig.logsPrefix);
252252
await handleCheckDiskSpace();
253253

254254
if (isWatchMode) {
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import moment, { Moment } from 'moment';
22

33
export function isItFreshLog(applicationStartDate: Moment, line: string) {
4-
const [, logDate] = line.match(
5-
/\[(\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}\.\d+) UTC]/
6-
);
4+
const [, logDate] =
5+
line.match(/\[(\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}\.\d+) UTC]/) || [];
76
return applicationStartDate.isBefore(moment.utc(logDate));
87
}
Lines changed: 20 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import { BrowserWindow } from 'electron';
22
import fs from 'fs';
3-
import moment, { Moment } from 'moment';
4-
import readline from 'readline';
3+
import moment from 'moment';
54
import path from 'path';
5+
import { Tail } from 'tail';
66
import { getBlockSyncProgressChannel } from '../ipc/get-block-sync-progress';
77
import type { GetBlockSyncProgressType } from '../../common/ipc/api';
8-
import { BLOCK_REPLAY_PROGRESS_CHECK_INTERVAL } from '../config';
98
import { BlockSyncType } from '../../common/types/cardano-node.types';
109
import { isItFreshLog } from './blockSyncProgressHelpers';
1110

@@ -44,34 +43,29 @@ function getProgressType(line: string): GetBlockSyncProgressType | null {
4443

4544
const 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(/Progress:([\s\d.,]+)%/)?.[1];
74-
const progressType = getProgressType(finalProgress);
67+
const percentage = line.match(/Progress:([\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
};

yarn.lock

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16456,6 +16456,10 @@ table@^6.0.1:
1645616456
slice-ansi "^4.0.0"
1645716457
string-width "^4.2.0"
1645816458

16459+
16460+
version "2.2.4"
16461+
resolved "https://registry.yarnpkg.com/tail/-/tail-2.2.4.tgz#90dd4c5a174a3fa39dcb65a1df1950a4a0093a41"
16462+
1645916463
tapable@^0.1.8:
1646016464
version "0.1.10"
1646116465
resolved "https://registry.yarnpkg.com/tapable/-/tapable-0.1.10.tgz#29c35707c2b70e50d07482b5d202e8ed446dafd4"

0 commit comments

Comments
 (0)