Skip to content

Commit

Permalink
fix: Mitigate issue where reading JSON can cause stars to be reset
Browse files Browse the repository at this point in the history
  • Loading branch information
mrseanryan committed Dec 31, 2018
1 parent 235ef17 commit 688eeaf
Showing 1 changed file with 32 additions and 10 deletions.
42 changes: 32 additions & 10 deletions src/bars/model/persisted/DataStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,42 @@ export namespace DataStorage {
const filepath = getDataFilePathForDirectory(imageInputDir);

return new Promise<DirectoryMetaData>((resolve, reject) => {
fs.readFile(filepath, "utf8", (error, data) => {
if (error) {
console.error(error);
const tryToReadFile = () => {
attempts++;
fs.readFile(filepath, "utf8", (error, data) => {
if (error) {
console.error(error);
retry(error);
return;
}

try {
const metaData = JSON.parse(data);
resolve(metaData);
} catch (parseError) {
console.warn("error parsing the JSON - will retry", parseError);
retry(parseError);
}
});
};

const MAX_ATTEMPTS = 3;
let attempts = 0;

const retry = (error: any) => {
if (attempts >= MAX_ATTEMPTS) {
console.error(
`tried ${MAX_ATTEMPTS} to read JSON - giving up - will default to empty stars`
);
reject(error);
return;
}

try {
resolve(JSON.parse(data));
} catch (parseError) {
console.error("error parsing the JSON", parseError);
reject(parseError);
}
});
console.log("retrying after 1 second ...");
setTimeout(() => tryToReadFile(), 1000);
};

tryToReadFile();
});
}

Expand Down

0 comments on commit 688eeaf

Please sign in to comment.