Skip to content

Commit 53331e8

Browse files
authored
fix: graceful BasicCrawler tidy-up on CriticalError (#2817)
Runs all the `BasicCrawler` clean-up actions even when the `AutoscaledPool.run()` throws. Closes #2807
1 parent 455f9c7 commit 53331e8

File tree

1 file changed

+41
-38
lines changed

1 file changed

+41
-38
lines changed

packages/basic-crawler/src/internals/basic-crawler.ts

+41-38
Original file line numberDiff line numberDiff line change
@@ -918,6 +918,8 @@ export class BasicCrawler<Context extends CrawlingContext = BasicCrawlingContext
918918
this.events.on(EventType.MIGRATING, boundPauseOnMigration);
919919
this.events.on(EventType.ABORTING, boundPauseOnMigration);
920920

921+
let stats = {} as FinalStatistics;
922+
921923
try {
922924
await this.autoscaledPool!.run();
923925
} finally {
@@ -927,50 +929,51 @@ export class BasicCrawler<Context extends CrawlingContext = BasicCrawlingContext
927929
process.off('SIGINT', sigintHandler);
928930
this.events.off(EventType.MIGRATING, boundPauseOnMigration);
929931
this.events.off(EventType.ABORTING, boundPauseOnMigration);
930-
}
931-
932-
const finalStats = this.stats.calculate();
933-
const stats = {
934-
requestsFinished: this.stats.state.requestsFinished,
935-
requestsFailed: this.stats.state.requestsFailed,
936-
retryHistogram: this.stats.requestRetryHistogram,
937-
...finalStats,
938-
};
939-
this.log.info('Final request statistics:', stats);
940932

941-
if (this.stats.errorTracker.total !== 0) {
942-
const prettify = ([count, info]: [number, string[]]) => `${count}x: ${info.at(-1)!.trim()} (${info[0]})`;
933+
const finalStats = this.stats.calculate();
934+
stats = {
935+
requestsFinished: this.stats.state.requestsFinished,
936+
requestsFailed: this.stats.state.requestsFailed,
937+
retryHistogram: this.stats.requestRetryHistogram,
938+
...finalStats,
939+
};
940+
this.log.info('Final request statistics:', stats);
941+
942+
if (this.stats.errorTracker.total !== 0) {
943+
const prettify = ([count, info]: [number, string[]]) =>
944+
`${count}x: ${info.at(-1)!.trim()} (${info[0]})`;
945+
946+
this.log.info(`Error analysis:`, {
947+
totalErrors: this.stats.errorTracker.total,
948+
uniqueErrors: this.stats.errorTracker.getUniqueErrorCount(),
949+
mostCommonErrors: this.stats.errorTracker.getMostPopularErrors(3).map(prettify),
950+
});
951+
}
943952

944-
this.log.info(`Error analysis:`, {
945-
totalErrors: this.stats.errorTracker.total,
946-
uniqueErrors: this.stats.errorTracker.getUniqueErrorCount(),
947-
mostCommonErrors: this.stats.errorTracker.getMostPopularErrors(3).map(prettify),
948-
});
949-
}
953+
const client = this.config.getStorageClient();
950954

951-
const client = this.config.getStorageClient();
955+
if (client.teardown) {
956+
let finished = false;
957+
setTimeout(() => {
958+
if (!finished) {
959+
this.log.info('Waiting for the storage to write its state to file system.');
960+
}
961+
}, 1000);
962+
await client.teardown();
963+
finished = true;
964+
}
952965

953-
if (client.teardown) {
954-
let finished = false;
955-
setTimeout(() => {
956-
if (!finished) {
957-
this.log.info('Waiting for the storage to write its state to file system.');
958-
}
959-
}, 1000);
960-
await client.teardown();
961-
finished = true;
966+
periodicLogger.stop();
967+
await this.setStatusMessage(
968+
`Finished! Total ${this.stats.state.requestsFinished + this.stats.state.requestsFailed} requests: ${
969+
this.stats.state.requestsFinished
970+
} succeeded, ${this.stats.state.requestsFailed} failed.`,
971+
{ isStatusMessageTerminal: true, level: 'INFO' },
972+
);
973+
this.running = false;
974+
this.hasFinishedBefore = true;
962975
}
963976

964-
periodicLogger.stop();
965-
await this.setStatusMessage(
966-
`Finished! Total ${this.stats.state.requestsFinished + this.stats.state.requestsFailed} requests: ${
967-
this.stats.state.requestsFinished
968-
} succeeded, ${this.stats.state.requestsFailed} failed.`,
969-
{ isStatusMessageTerminal: true, level: 'INFO' },
970-
);
971-
this.running = false;
972-
this.hasFinishedBefore = true;
973-
974977
return stats;
975978
}
976979

0 commit comments

Comments
 (0)