fix(watchers): re-arm a stopped TeamWatcher watcher from the poll

chokidar 3 reports a one-off ENOENT race (a new file gone between its
stat and its watch) through the same TypeError as a lasting EMFILE, so a
watcher that stayed closed ended live team and task updates until a
restart. Each poll tick now re-creates a stopped watcher, as
WorkflowRunWatcher already does, and an outage warns once rather than
once per attempt. Also imports getErrorMessage from the types barrel.
This commit is contained in:
JD committed 2026-11-19 02:52:40 -05:00
1 parent 10e443a309
commit c7b6e1cb4e
4 files changed
+290 -16

No files matched your search

+29 -7
View File
@@ -15,8 +15,7 @@ import { join } from 'node:path';
import { watch as chokidarWatch, type FSWatcher as ChokidarWatcher } from 'chokidar';
import type { TeamConfig, TeamTask, InboxMessage } from './types.js';
import { getErrorMessage } from './types/api.js';
import { getErrorMessage, type TeamConfig, type TeamTask, type InboxMessage } from './types.js';
import { closeWatcherOnError } from './utils/close-watcher-on-error.js';
import { LRUMap } from './utils/lru-map.js';
import { TEAM_POLL_INTERVAL_MS, MAX_CACHED_TEAMS, MAX_CACHED_TASKS } from './config/team-config.js';
@@ -38,6 +37,8 @@ export class TeamWatcher extends EventEmitter {
private sessionToTeam: Map<string, string> = new Map();
private teamsWatcher: ChokidarWatcher | null = null;
private tasksWatcher: ChokidarWatcher | null = null;
// Watchers stopped by an error and not yet back for a whole poll: one warning per outage.
private watchersDown = new Set<'teams' | 'tasks'>();
constructor(teamsDir?: string, tasksDir?: string) {
super();
@@ -49,11 +50,28 @@ export class TeamWatcher extends EventEmitter {
start(): void {
if (this.pollTimer) return;
this.poll();
this.pollTimer = setInterval(() => this.poll(), TEAM_POLL_INTERVAL_MS);
this.pollTimer = setInterval(() => {
this.setupFsWatchers(); // re-creates a watcher an error stopped
this.poll();
}, TEAM_POLL_INTERVAL_MS);
this.setupFsWatchers();
}
private watcherStopped(name: 'teams' | 'tasks', err: unknown): void {
if (this.watchersDown.has(name)) return;
this.watchersDown.add(name);
console.warn(`[TeamWatcher] ${name} watcher stopped, relying on polling: ${getErrorMessage(err)}`);
}
/** Creates each watcher that is not running; one still running since the last call ends its outage. */
private setupFsWatchers(): void {
if (this.teamsWatcher) this.watchersDown.delete('teams');
else this.setupTeamsWatcher();
if (this.tasksWatcher) this.watchersDown.delete('tasks');
else this.setupTasksWatcher();
}
private setupTeamsWatcher(): void {
try {
this.teamsWatcher = chokidarWatch(this.teamsDir, {
depth: 2,
@@ -69,12 +87,15 @@ export class TeamWatcher extends EventEmitter {
this.teamsWatcher.on('unlink', teamsHandler);
this.teamsWatcher.on('unlinkDir', teamsHandler);
closeWatcherOnError(this.teamsWatcher, (err) => {
console.warn(`[TeamWatcher] teams watcher stopped, relying on polling: ${getErrorMessage(err)}`);
this.teamsWatcher = null;
this.watcherStopped('teams', err);
});
} catch (err) {
console.warn('[TeamWatcher] Failed to set up teams chokidar watcher, relying on polling:', err);
this.watcherStopped('teams', err);
}
}
private setupTasksWatcher(): void {
try {
this.tasksWatcher = chokidarWatch(this.tasksDir, {
depth: 1,
@@ -87,10 +108,11 @@ export class TeamWatcher extends EventEmitter {
this.tasksWatcher.on('add', () => this.pollTasks().catch(() => {})); // Ignore - poll errors are non-fatal, next poll will retry
this.tasksWatcher.on('change', () => this.pollTasks().catch(() => {})); // Ignore - poll errors are non-fatal, next poll will retry
closeWatcherOnError(this.tasksWatcher, (err) => {
console.warn(`[TeamWatcher] tasks watcher stopped, relying on polling: ${getErrorMessage(err)}`);
this.tasksWatcher = null;
this.watcherStopped('tasks', err);
});
} catch (err) {
console.warn('[TeamWatcher] Failed to set up tasks chokidar watcher, relying on polling:', err);
this.watcherStopped('tasks', err);
}
}
+2 -1
View File
@@ -26,7 +26,8 @@ import type { FSWatcher } from 'chokidar';
/**
* Close `watcher` on its first `'error'` and pass that error to `onClosed`,
* once. The caller's polling has to cover what the watcher watched.
* once. The caller's polling has to cover what the watcher watched and create it
* again later: chokidar 3 reports a one-off ENOENT race through the same error.
*/
export function closeWatcherOnError(watcher: FSWatcher, onClosed: (err: unknown) => void): void {
watcher.once('error', (err: unknown) => {