Skip to content

Commit 5ebc0d4

Browse files
committed
Fixing a bug where failing to fetch a feed could make Bubo hang
1 parent 04696e9 commit 5ebc0d4

File tree

1 file changed

+19
-16
lines changed

1 file changed

+19
-16
lines changed

src/index.ts

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,11 @@ let completed = 0;
6666
* and we want to build the static output.
6767
*/
6868
const finishBuild: () => void = async () => {
69-
console.log("\nDone fetching everything!");
69+
completed++;
70+
// if this isn't the last feed, just return early
71+
if (completed !== feedListLength) return;
72+
73+
process.stdout.write("\nDone fetching everything!\n");
7074

7175
// generate the static HTML output from our template renderer
7276
const output = render({
@@ -77,10 +81,10 @@ const finishBuild: () => void = async () => {
7781

7882
// write the output to public/index.html
7983
await writeFile("./public/index.html", output);
80-
console.log(
84+
process.stdout.write(
8185
`\nFinished writing to output:\n- ${feedListLength} feeds in ${benchmark(
8286
initTime
83-
)}\n- ${errors.length} errors`
87+
)}\n- ${errors.length} errors\n`
8488
);
8589
};
8690

@@ -103,8 +107,7 @@ const processFeed =
103107
}) =>
104108
async (response: Response): Promise<void> => {
105109
const body = await parseFeed(response);
106-
completed++;
107-
// skip to the next one if this didn't work out
110+
//skip to the next one if this didn't work out
108111
if (!body) return;
109112

110113
try {
@@ -124,20 +127,19 @@ const processFeed =
124127
});
125128

126129
contentFromAllFeeds[group].push(contents as object);
127-
console.log(
128-
`${success("Successfully fetched:")} ${feed} - ${benchmark(startTime)}`
130+
process.stdout.write(
131+
`${success("Successfully fetched:")} ${feed} - ${benchmark(startTime)}\n`
129132
);
130133
} catch (err) {
131-
console.log(
134+
process.stdout.write(
132135
`${error("Error processing:")} ${feed} - ${benchmark(
133136
startTime
134-
)}\n${err}`
137+
)}\n${err}\n`
135138
);
136139
errors.push(`Error processing: ${feed}\n\t${err}`);
137140
}
138141

139-
// if this is the last feed, go ahead and build the output
140-
completed === feedListLength && finishBuild();
142+
finishBuild();
141143
};
142144

143145
// go through each group of feeds and process
@@ -150,20 +152,21 @@ const processFeeds = () => {
150152
for (const feed of feeds) {
151153
const startTime = Date.now();
152154
setTimeout(() => {
153-
console.log(`Fetching: ${feed}...`);
155+
process.stdout.write(`Fetching: ${feed}...\n`);
154156

155157
fetch(feed)
156158
.then(processFeed({ group, feed, startTime }))
157159
.catch(err => {
158-
console.log(
159-
error(`Error fetching ${feed} ${benchmark(startTime)}`)
160+
process.stdout.write(
161+
error(`Error fetching ${feed} ${benchmark(startTime)}\n`)
160162
);
161-
errors.push(`Error fetching ${feed} ${err.toString()}`);
163+
errors.push(`Error fetching ${feed} ${err.toString()}\n`);
164+
finishBuild();
162165
});
163166
}, (idx % (feedListLength / MAX_CONNECTIONS)) * DELAY_MS);
164167
idx++;
165168
}
166169
}
167170
};
168171

169-
processFeeds();
172+
processFeeds();

0 commit comments

Comments
 (0)