mirror of
https://github.com/usebruno/bruno.git
synced 2024-11-07 08:34:15 +01:00
fix: Handle ENOSPC error from chokidar (#2725)
* fix: Handle ENOSPC error from chokidar Now listens to the error event to check if "ENOSPC" occurrs. The watcher will then automaticly restart in polling mode, so that the user still sees his reqeusts / collections. Fixes: https://github.com/usebruno/bruno/issues/1877 * Add more code comments, add !forcePolling to prevent endless loops and update error message
This commit is contained in:
parent
29db85a916
commit
1e0c88a291
@ -428,17 +428,16 @@ class Watcher {
|
||||
this.watchers = {};
|
||||
}
|
||||
|
||||
addWatcher(win, watchPath, collectionUid, brunoConfig) {
|
||||
addWatcher(win, watchPath, collectionUid, brunoConfig, forcePolling = false) {
|
||||
if (this.watchers[watchPath]) {
|
||||
this.watchers[watchPath].close();
|
||||
}
|
||||
|
||||
const ignores = brunoConfig?.ignore || [];
|
||||
const self = this;
|
||||
setTimeout(() => {
|
||||
const watcher = chokidar.watch(watchPath, {
|
||||
ignoreInitial: false,
|
||||
usePolling: watchPath.startsWith('\\\\') ? true : false,
|
||||
usePolling: watchPath.startsWith('\\\\') || forcePolling ? true : false,
|
||||
ignored: (filepath) => {
|
||||
const normalizedPath = filepath.replace(/\\/g, '/');
|
||||
const relativePath = path.relative(watchPath, normalizedPath);
|
||||
@ -457,14 +456,35 @@ class Watcher {
|
||||
depth: 20
|
||||
});
|
||||
|
||||
let startedNewWatcher = false;
|
||||
watcher
|
||||
.on('add', (pathname) => add(win, pathname, collectionUid, watchPath))
|
||||
.on('addDir', (pathname) => addDirectory(win, pathname, collectionUid, watchPath))
|
||||
.on('change', (pathname) => change(win, pathname, collectionUid, watchPath))
|
||||
.on('unlink', (pathname) => unlink(win, pathname, collectionUid, watchPath))
|
||||
.on('unlinkDir', (pathname) => unlinkDir(win, pathname, collectionUid, watchPath));
|
||||
.on('unlinkDir', (pathname) => unlinkDir(win, pathname, collectionUid, watchPath))
|
||||
.on('error', (error) => {
|
||||
// `ENOSPC` stands for "Error No space" but is also thrown if the file watcher limit is reached.
|
||||
// To prevent loops `!forcePolling` is checked.
|
||||
if (error.code === 'ENOSPC' && !startedNewWatcher && !forcePolling) {
|
||||
// This callback is called for every file the watcher is trying to watch. To prevent a spam of messages and
|
||||
// Multiple watcher being started `startedNewWatcher` is set to prevent this.
|
||||
startedNewWatcher = true;
|
||||
watcher.close();
|
||||
console.error(
|
||||
`\nCould not start watcher for ${watchPath}:`,
|
||||
'ENOSPC: System limit for number of file watchers reached!',
|
||||
'Trying again with polling, this will be slower!\n',
|
||||
'Update you system config to allow more concurrently watched files with:',
|
||||
'"echo fs.inotify.max_user_watches=524288 | sudo tee -a /etc/sysctl.conf && sudo sysctl -p"'
|
||||
);
|
||||
this.addWatcher(win, watchPath, collectionUid, brunoConfig, true);
|
||||
} else {
|
||||
console.error(`An error occurred in the watcher for: ${watchPath}`, error);
|
||||
}
|
||||
});
|
||||
|
||||
self.watchers[watchPath] = watcher;
|
||||
this.watchers[watchPath] = watcher;
|
||||
}, 100);
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user