Skip to content

Commit b32369e

Browse files
Copilothotlong
andcommitted
Extract magic numbers to named constants and improve documentation
- Add RETRY_DELAY_BASE_MS, FS_SYNC_DELAY_MS, MAX_RETRIES constants - Improve sleepSync documentation explaining why busy-wait is acceptable - Add better error messages for partial schema generation failures - Use constants consistently throughout retry logic Co-authored-by: hotlong <50353452+hotlong@users.noreply.github.com>
1 parent b50462d commit b32369e

1 file changed

Lines changed: 21 additions & 9 deletions

File tree

packages/spec/scripts/build-schemas.ts

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,19 @@ import * as Protocol from '../src/index';
66

77
const OUT_DIR = path.resolve(__dirname, '../json-schema');
88

9+
// Retry and delay configuration
10+
const RETRY_DELAY_BASE_MS = 100; // Base delay in ms, multiplied by retry attempt number
11+
const FS_SYNC_DELAY_MS = 50; // Delay after rmSync to ensure filesystem consistency
12+
const MAX_RETRIES = 3; // Maximum number of retry attempts
13+
914
/**
1015
* Synchronous sleep utility using a busy-wait loop
1116
* Only use for short delays in build scripts where blocking is acceptable
17+
*
18+
* Note: This blocks the event loop and consumes CPU. For production code,
19+
* use async/await with setTimeout. For build scripts, this simple synchronous
20+
* approach is acceptable as we need to ensure filesystem operations complete
21+
* before proceeding.
1222
*/
1323
function sleepSync(ms: number): void {
1424
const end = Date.now() + ms;
@@ -20,7 +30,7 @@ function sleepSync(ms: number): void {
2030
/**
2131
* Safely ensure directory exists with retry logic
2232
*/
23-
function ensureDir(dirPath: string, retries = 3): void {
33+
function ensureDir(dirPath: string, retries = MAX_RETRIES): void {
2434
for (let i = 0; i < retries; i++) {
2535
try {
2636
if (!fs.existsSync(dirPath)) {
@@ -34,8 +44,8 @@ function ensureDir(dirPath: string, retries = 3): void {
3444
if (i === retries - 1) {
3545
throw new Error(`Failed to create directory ${dirPath}: ${error}`);
3646
}
37-
// Wait a bit before retrying
38-
const delay = 100 * (i + 1);
47+
// Wait a bit before retrying with exponential backoff
48+
const delay = RETRY_DELAY_BASE_MS * (i + 1);
3949
sleepSync(delay);
4050
}
4151
}
@@ -44,7 +54,7 @@ function ensureDir(dirPath: string, retries = 3): void {
4454
/**
4555
* Safely write file with retry logic
4656
*/
47-
function writeFileWithRetry(filePath: string, content: string, retries = 3): void {
57+
function writeFileWithRetry(filePath: string, content: string, retries = MAX_RETRIES): void {
4858
for (let i = 0; i < retries; i++) {
4959
try {
5060
// Ensure the parent directory exists
@@ -57,8 +67,8 @@ function writeFileWithRetry(filePath: string, content: string, retries = 3): voi
5767
if (i === retries - 1) {
5868
throw new Error(`Failed to write file ${filePath}: ${error}`);
5969
}
60-
// Wait a bit before retrying
61-
const delay = 100 * (i + 1);
70+
// Wait a bit before retrying with exponential backoff
71+
const delay = RETRY_DELAY_BASE_MS * (i + 1);
6272
sleepSync(delay);
6373
}
6474
}
@@ -67,11 +77,11 @@ function writeFileWithRetry(filePath: string, content: string, retries = 3): voi
6777
// Clean output directory ensures no stale files remain
6878
if (fs.existsSync(OUT_DIR)) {
6979
console.log(`Cleaning output directory: ${OUT_DIR}`);
70-
fs.rmSync(OUT_DIR, { recursive: true, force: true, maxRetries: 3, retryDelay: 100 });
80+
fs.rmSync(OUT_DIR, { recursive: true, force: true, maxRetries: MAX_RETRIES, retryDelay: RETRY_DELAY_BASE_MS });
7181

7282
// Wait a bit to ensure file system has synced
73-
// This prevents ENOENT errors on some file systems
74-
sleepSync(50);
83+
// This prevents ENOENT errors on some file systems, particularly in CI environments
84+
sleepSync(FS_SYNC_DELAY_MS);
7585
}
7686

7787
// Ensure output directory exists
@@ -129,6 +139,8 @@ for (const [namespaceName, namespaceExports] of Object.entries(Protocol)) {
129139

130140
if (errorCount > 0) {
131141
console.error(`\n❌ Completed with ${errorCount} error(s). ${count} schemas generated successfully.`);
142+
console.error(`\nNote: Partial schema generation occurred. Some schemas may be missing.`);
143+
console.error(`This typically indicates a Zod schema definition error or file system issue.`);
132144
process.exit(1);
133145
}
134146

0 commit comments

Comments
 (0)