Skip to content

Commit

Permalink
Request new logs only after earlier action succeed (#139)
Browse files Browse the repository at this point in the history
* Request new logs only after earlier action succeed

* update tests

* update tests
  • Loading branch information
timqian authored Feb 23, 2022
1 parent 0c7954e commit 4438a16
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 46 deletions.
86 changes: 43 additions & 43 deletions src/commands/logs.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,53 +159,53 @@ module.exports = async (config, cli, command) => {
} else {
cli.sessionStart('监听中');

await new Promise((resolve, reject) => {
// Polling logs
let errorCount = 0;
let lastLogList = [];
const logInterval = async () => {
let newLogList = [];
// Stop polling when two errors happen in a row
let errorCount = 0;
let lastLogList = [];
const logInterval = setInterval(async () => {
let newLogList = [];
try {
newLogList = (await getLogList()) || [];
errorCount = 0;
} catch (error) {
errorCount += 1;
if (errorCount >= 2) {
clearInterval(logInterval);
reject(error);
}
}

if (newLogList.length > 0 && lastLogList.length <= 0) {
printLogMessages(newLogList, cli);
lastLogList = newLogList;
try {
newLogList = (await getLogList()) || [];
errorCount = 0;
} catch (error) {
errorCount += 1;
if (errorCount >= 2) {
throw error;
}

if (newLogList.length > 0 && lastLogList.length > 0) {
const lastLogReqId = lastLogList[lastLogList.length - 1].requestId;
const newLogReqId = newLogList[newLogList.length - 1].requestId;

const newestLogIndexInOldLogs = lastLogList.findIndex(
(item) => item.requestId === newLogReqId
);
const lastLogIndexInNewLogs = newLogList.findIndex(
(item) => item.requestId === lastLogReqId
);

// When newestLogIndexInOldLogs !== -1, it means newest log already exists in the old log list
// Note: tencent log API has a cache mechanism, sometimes newly fetched log may not conataining newst log
if (newestLogIndexInOldLogs === -1) {
if (lastLogIndexInNewLogs === -1) {
printLogMessages(newLogList, cli);
} else if (lastLogIndexInNewLogs < newLogList.length - 1) {
printLogMessages(newLogList.slice(lastLogIndexInNewLogs + 1), cli);
}
lastLogList = newLogList;
}

if (newLogList.length > 0 && lastLogList.length <= 0) {
printLogMessages(newLogList, cli);
lastLogList = newLogList;
}

if (newLogList.length > 0 && lastLogList.length > 0) {
const lastLogReqId = lastLogList[lastLogList.length - 1].requestId;
const newLogReqId = newLogList[newLogList.length - 1].requestId;

const newestLogIndexInOldLogs = lastLogList.findIndex(
(item) => item.requestId === newLogReqId
);
const lastLogIndexInNewLogs = newLogList.findIndex(
(item) => item.requestId === lastLogReqId
);

// When newestLogIndexInOldLogs !== -1, it means newest log already exists in the old log list
// Note: tencent log API has a cache mechanism, sometimes newly fetched log may not conataining newst log
if (newestLogIndexInOldLogs === -1) {
if (lastLogIndexInNewLogs === -1) {
printLogMessages(newLogList, cli);
} else if (lastLogIndexInNewLogs < newLogList.length - 1) {
printLogMessages(newLogList.slice(lastLogIndexInNewLogs + 1), cli);
}
lastLogList = newLogList;
}
return 0;
}, Number(intervalValue) || 2000);
});
}
await chinaUtils.sleep(Number(intervalValue) || 2000);
await logInterval();
};
await logInterval();
}
} catch (e) {
telemtryData.outcome = 'failure';
Expand Down
6 changes: 3 additions & 3 deletions tests/commands/logs.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ jest.mock('@serverless/platform-client-china', () => {

utils: {
getOrgId: async () => 123,
// Rejecting here to ensure sls logs -t exits
sleep: async () => new Promise((_, reject) => setTimeout(reject, 500)),
},
};
});
Expand Down Expand Up @@ -72,11 +74,9 @@ describe('Test getLogs: src/commands/getLogs', () => {

test('test logs with interval', async () => {
const tailLogs = logsCmd({ t: true }, cli);
const sleep500 = new Promise((resolve) => setTimeout(resolve, 500, 'one'));
jest.useFakeTimers();
const sleep500 = new Promise((resolve) => setTimeout(resolve, 100, 'one'));
const value = await Promise.race([tailLogs, sleep500]);
expect(value).toBe('one');
jest.runOnlyPendingTimers();
});

test('test empty response', async () => {
Expand Down

0 comments on commit 4438a16

Please sign in to comment.