Skip to content

Commit

Permalink
don't call an erroring data loader too often (#133)
Browse files Browse the repository at this point in the history
this pauses 1 sec after a data loader has failed before calling it again (unless it has just been edited)

closes #112
  • Loading branch information
Fil authored Dec 11, 2023
1 parent 822c2ad commit 56551bb
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/dataloader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,14 +152,21 @@ export abstract class Loader {
else effects.output.write(faint("[stale] "));
} else return effects.output.write(faint("[fresh] ")), outputPath;
const tempPath = join(this.sourceRoot, ".observablehq", "cache", `${this.targetPath}.${process.pid}`);
const errorPath = tempPath + ".err";
const errorStat = await maybeStat(errorPath);
if (errorStat) {
if (errorStat.mtimeMs > loaderStat!.mtimeMs && errorStat.mtimeMs > -1000 + Date.now())
throw new Error("loader skipped due to recent error");
else await unlink(errorPath).catch(() => {});
}
await prepareOutput(tempPath);
const tempFd = await open(tempPath, "w");
try {
await this.exec(tempFd.createWriteStream({highWaterMark: 1024 * 1024}), effects);
await mkdir(dirname(cachePath), {recursive: true});
await rename(tempPath, cachePath);
} catch (error) {
await unlink(tempPath);
await rename(tempPath, errorPath);
throw error;
} finally {
await tempFd.close();
Expand Down Expand Up @@ -223,7 +230,7 @@ class CommandLoader extends Loader {
subprocess.on("close", resolve);
});
if (code !== 0) {
throw new Error(`exited with code ${code}`);
throw new Error(`loader exited with code ${code}`);
}
}
}
Expand Down

0 comments on commit 56551bb

Please sign in to comment.