Skip to content
Snippets Groups Projects
Unverified Commit 6e8cdb30 authored by Michael Clark's avatar Michael Clark Committed by GitHub
Browse files

:electron: Adding retries in to the file system writes (#3406)

* hardening the file system access by added retrys

* release notees

* remove unneeded console log

* promise-retry instead of my own

* tiny bit more info

* capital U
parent 3985d254
No related branches found
No related tags found
No related merge requests found
...@@ -2,6 +2,8 @@ ...@@ -2,6 +2,8 @@
import * as fs from 'fs'; import * as fs from 'fs';
import * as path from 'path'; import * as path from 'path';
import promiseRetry from 'promise-retry';
import type * as T from '.'; import type * as T from '.';
export { getDocumentDir, getBudgetDir, _setDocumentDir } from './shared'; export { getDocumentDir, getBudgetDir, _setDocumentDir } from './shared';
...@@ -110,13 +112,43 @@ export const readFile: T.ReadFile = ( ...@@ -110,13 +112,43 @@ export const readFile: T.ReadFile = (
}); });
}; };
export const writeFile: T.WriteFile = (filepath, contents) => { export const writeFile: T.WriteFile = async (filepath, contents) => {
return new Promise(function (resolve, reject) { try {
// @ts-expect-error contents type needs refining await promiseRetry(
fs.writeFile(filepath, contents, 'utf8', function (err) { (retry, attempt) => {
return err ? reject(err) : resolve(undefined); return new Promise((resolve, reject) => {
}); // @ts-expect-error contents type needs refining
}); fs.writeFile(filepath, contents, 'utf8', err => {
if (err) {
console.error(
`Failed to write to ${filepath}. Attempted ${attempt} times. Something is locking the file - potentially a virus scanner or backup software.`,
);
reject(err);
} else {
if (attempt > 1) {
console.info(
`Successfully recovered from file lock. It took ${attempt} retries`,
);
}
resolve(undefined);
}
});
}).catch(retry);
},
{
retries: 20,
minTimeout: 100,
maxTimeout: 500,
factor: 1.5,
},
);
return undefined;
} catch (err) {
console.error(`Unable to recover from file lock on file ${filepath}`);
throw err;
}
}; };
export const removeFile = filepath => { export const removeFile = filepath => {
......
---
category: Maintenance
authors: [MikesGlitch]
---
Adding retries into filesystem calls to mitigate locking issues caused by virus scanners.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment