Skip to content

Commit 824cabc

Browse files
committed
Add short polling to fetch build results
* Build has started when the POST call with form data is successful. * Subsequently, short polling with exponential backoff is implemented to fetch build status. Exit process after three unsuccessful calls. * When build status is completed, print build details and comparions results.
1 parent 704028b commit 824cabc

File tree

1 file changed

+61
-11
lines changed

1 file changed

+61
-11
lines changed

commands/utils/dom.js

Lines changed: 61 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,38 +49,88 @@ async function sendDoM(storybookUrl, stories, storybookConfig, options) {
4949
form.append('resolution', storybookConfig.resolutions);
5050
form.append('browser', storybookConfig.browsers);
5151
form.append('projectToken', process.env.PROJECT_TOKEN);
52-
form.append('buildName', options.buildname);
52+
// form.append('buildName', options.buildname);
5353
form.append('branch', commit.branch);
5454
form.append('commitId', commit.shortHash);
5555
form.append('commitAuthor', commit.author.name);
5656
form.append('commitMessage', commit.subject);
5757

5858
// Send DOM to render API
59-
axios.post(constants[options.env].RENDER_API_URL, form, {
59+
await axios.post(constants[options.env].RENDER_API_URL, form, {
6060
headers: {
6161
...form.getHeaders()
6262
}
6363
})
64-
.then(function (response) {
65-
console.log('[smartui] Build successful')
64+
.then(async function (response) {
65+
console.log('[smartui] Build in progress...');
66+
await shortPolling(response.data.buildId, 0, 2000, 512000);
6667
})
6768
.catch(function (error) {
68-
fs.rm('doms', {recursive: true}, (err) => {
69-
if (err) {
70-
return console.error(err);
71-
}
72-
});
7369
console.log('[smartui] Build failed: Error: ', error.message);
74-
process.exit(0);
7570
});
76-
71+
7772
fs.rm('doms', {recursive: true}, (err) => {
7873
if (err) {
7974
return console.error(err);
8075
}
8176
});
8277
};
8378

79+
async function shortPolling(buildId, retries = 0, interval, maxInterval) {
80+
try {
81+
const response = await axios.get('https://stage-api.lambdatestinternal.com/storybook/status?buildId=' + buildId, {
82+
headers: {
83+
projectToken: process.env.PROJECT_TOKEN
84+
}
85+
});
86+
87+
if (response.data) {
88+
if (response.data.buildStatus === 'completed') {
89+
console.log('[smartui] Build successful\n');
90+
console.log('[smartui] Build details:\n',
91+
// 'Build URL: ', response.data.buildId, '\n',
92+
'Build Name: ', response.data.buildName, '\n',
93+
'Total Screenshots: ', response.data.screenshots.length, '\n',
94+
'Approved: ', response.data.buildResults.approved, '\n',
95+
'Changes found: ', response.data.buildResults.changesFound, '\n'
96+
);
97+
98+
response.data.screenshots.forEach(screenshot => {
99+
console.log(screenshot.storyName, ' | Mis-match: ', screenshot.mismatchPercentage);
100+
});
101+
102+
return;
103+
} else {
104+
if (response.data.screenshots.length > 0) {
105+
// TODO: show Screenshots processed 8/10
106+
console.log('[smartui] Screenshots processed: ', response.data.screenshots.length)
107+
}
108+
}
109+
}
110+
111+
// Double the interval, up to the maximum interval of 512 secs (so ~15 mins in total)
112+
interval = Math.min(interval * 2, maxInterval);
113+
if (interval == maxInterval) {
114+
console.log('[smartui] Please check the build status on LambdaTest SmartUI.');
115+
return;
116+
}
117+
118+
setTimeout(function () {
119+
shortPolling(buildId, 0, interval, maxInterval)
120+
}, interval);
121+
} catch (error) {
122+
if (retries >= 3) {
123+
console.log('[smartui] Error: Failed getting build status.', error.message);
124+
console.log('[smartui] Please check the build status on LambdaTest SmartUI.');
125+
return;
126+
}
127+
128+
setTimeout(function () {
129+
shortPolling(buildId, retries+1, interval, maxInterval);
130+
}, 2000);
131+
}
132+
};
133+
84134
function getBase64(url) {
85135
return axios.get(url, {
86136
responseType: "text",

0 commit comments

Comments
 (0)