Skip to content

Commit 87f0c2c

Browse files
committed
chore: improve failure handling and display
1 parent 3eb685d commit 87f0c2c

File tree

1 file changed

+39
-20
lines changed

1 file changed

+39
-20
lines changed

src/createManyConcurrentRequests.js

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,31 @@
2828
let aborted = false;
2929
let successfulRequests = 0;
3030
let failedRequests = 0;
31+
let inProgressRequests = 0;
3132

3233
// Setup Abort Handling (Ctrl+C)
3334
process.on("SIGINT", () => {
3435
console.log("\nAborting request creation...");
3536
aborted = true;
3637
});
3738

38-
// Setup Progress Bar
39-
const progressBar = new cliProgress.SingleBar(
40-
{},
39+
// Create a multi-bar container
40+
const multibar = new cliProgress.MultiBar(
41+
{
42+
clearOnComplete: false,
43+
hideCursor: true,
44+
format:
45+
" {bar} | {percentage}% | S: {successful}, F: {failed}, IP: {inProgress} | {value}/{total}",
46+
},
4147
cliProgress.Presets.shades_classic,
4248
);
4349

50+
const progressBar = multibar.create(TOTAL_REQUESTS, 0, {
51+
successful: 0,
52+
failed: 0,
53+
inProgress: 0,
54+
});
55+
4456
try {
4557
const epkSignatureProvider = new EthereumPrivateKeySignatureProvider({
4658
method: Types.Signature.METHOD.ECDSA,
@@ -66,23 +78,26 @@
6678
console.log(
6779
`Attempting to create ${TOTAL_REQUESTS} requests with concurrency ${CONCURRENCY_LIMIT}...`,
6880
);
69-
progressBar.start(TOTAL_REQUESTS, 0);
7081

7182
for (let i = 0; i < TOTAL_REQUESTS; i++) {
7283
if (aborted) {
7384
console.log(`Skipping remaining requests due to abort signal.`);
74-
break; // Stop adding new tasks if aborted
85+
break;
7586
}
7687

7788
creationPromises.push(
7889
limit(async () => {
79-
// Double-check abort flag before starting the async operation
80-
if (aborted) {
81-
return;
82-
}
90+
if (aborted) return;
8391

8492
try {
85-
// Use a unique identifier or timestamp if content needs variation
93+
// Increment in-progress counter and update progress bar
94+
inProgressRequests++;
95+
progressBar.update(successfulRequests + failedRequests, {
96+
successful: successfulRequests,
97+
failed: failedRequests,
98+
inProgress: inProgressRequests,
99+
});
100+
86101
const uniqueContent = `Request #${i + 1} - ${Date.now()}`;
87102

88103
const requestCreateParameters = {
@@ -128,31 +143,35 @@
128143
const requestData = await request.waitForConfirmation();
129144
successfulRequests++;
130145
} catch (error) {
131-
console.error(
132-
`\nFailed to create request: ${error.message || error}`,
133-
);
146+
const errorMessage = error.message || error.toString();
147+
// Simplified error output to avoid console spam
148+
console.error(`\nRequest failed: ${errorMessage.split("\n")[0]}`);
134149
failedRequests++;
135150
} finally {
136-
// Ensure progress bar updates even if aborted after starting the task
137-
if (!aborted) {
138-
progressBar.increment();
139-
}
151+
inProgressRequests--; // Decrement in-progress counter
152+
progressBar.update(successfulRequests + failedRequests, {
153+
successful: successfulRequests,
154+
failed: failedRequests,
155+
inProgress: inProgressRequests,
156+
});
140157
}
141158
}),
142159
);
143160
}
144161

145-
// Wait for all queued promises to settle
146162
await Promise.all(creationPromises);
147163
} catch (error) {
148164
console.error(`\nAn unexpected error occurred: ${error.message || error}`);
149-
aborted = true; // Stop progress bar on unexpected errors
165+
aborted = true;
150166
} finally {
151-
progressBar.stop();
167+
// Stop the progress bar
168+
multibar.stop();
169+
152170
console.log("\n--- Request Creation Summary ---");
153171
console.log(`Total attempted: ${successfulRequests + failedRequests}`);
154172
console.log(`Successful: ${successfulRequests}`);
155173
console.log(`Failed: ${failedRequests}`);
174+
156175
if (aborted && successfulRequests + failedRequests < TOTAL_REQUESTS) {
157176
console.log(
158177
`Process aborted. ${

0 commit comments

Comments
 (0)