Skip to content

Commit

Permalink
Stop polling logs when two errors happen in a row (#1037)
Browse files Browse the repository at this point in the history
* Stop polling when two errors happen in a row

* update version

* prettify

* remove unecessary code

* update changelog

* prettify
  • Loading branch information
timqian committed Jan 19, 2022
1 parent 24d9123 commit 6bc2eeb
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 40 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.

## [3.18.2](https://github.com/serverless/components/compare/v3.18.1....v3.18.2) (2022-01-19)

- Stop polling logs when two errors happens in a row

## [3.18.1](https://github.com/serverless/components/compare/v3.18.0....v3.18.1) (2021-11-17)

- Improvement for YML genertation on Node proejct with HTTP component
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@serverless/components",
"version": "3.18.1",
"version": "3.18.2",
"description": "The Serverless Framework's new infrastructure provisioning technology — Build, compose, & deploy serverless apps in seconds...",
"main": "./src/index.js",
"bin": {
Expand Down
82 changes: 43 additions & 39 deletions src/cli/commands-cn/logs.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,51 +150,55 @@ module.exports = async (config, cli, command) => {
}
cli.sessionStop('success', '获取日志成功');
} else {
let lastLogList = await getLogList();

if (lastLogList.length > 0) {
printLogMessages(lastLogList, cli);
}

cli.sessionStart('监听中');
setInterval(async () => {
let newLogList;
// ignore timeout issue when polling new logs
try {
newLogList = await getLogList();
} catch (error) {
return 0;
}

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

const newestLogIndexInOldLogs = lastLogList.findIndex(
(item) => item.requestId === newLogReqId
);
const lastLogIndexInNewLogs = newLogList.findIndex(
(item) => item.requestId === lastLogReqId
);
if (newLogList.length > 0 && lastLogList.length <= 0) {
printLogMessages(newLogList, cli);
lastLogList = newLogList;
}

// 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);
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;
}
lastLogList = newLogList;
}
}
return 0;
}, Number(intervalValue) || 2000);
return 0;
}, Number(intervalValue) || 2000);
});
}
} catch (e) {
telemtryData.outcome = 'failure';
Expand Down

0 comments on commit 6bc2eeb

Please sign in to comment.