Skip to content

Open-source AI teammates in Slack, Teams, Discord, Telegram and Mattermost. Self-host it, with any agent harness and any model.

fix(console): leave Mattermost out of the local-server watch

Simon Flack5dfa8868c178
diff --git a/console/apps/switch-console-desktop/src/main/core/managed-switch-server/local-server-service.test.ts b/console/apps/switch-console-desktop/src/main/core/managed-switch-server/local-server-service.test.ts
index e4d3b21c5..0a2ca56a9 100644
--- a/console/apps/switch-console-desktop/src/main/core/managed-switch-server/local-server-service.test.ts
+++ b/console/apps/switch-console-desktop/src/main/core/managed-switch-server/local-server-service.test.ts
@@ -109,11 +109,7 @@ const native = vi.hoisted(() => ({
discard: vi.fn(async () => {}),
failureLine: vi.fn(async (): Promise<string | null> => null),
}));
-const watch = vi.hoisted(() => ({
- health: vi.fn(async () => true),
- mattermostAnswers: vi.fn(async () => true),
- mattermostExpected: vi.fn(async () => false),
-}));
+const watch = vi.hoisted(() => ({ health: vi.fn(async () => true) }));
vi.mock('./health', () => ({ probeHealth: watch.health }));
vi.mock('./native/native-stack', () => ({
startNativeStack: native.start,
@@ -143,8 +139,6 @@ vi.mock('./native/mattermost', () => ({
readMattermostEnabled: async () => null,
writeMattermostEnabled: mattermost.write,
stopMattermost: mattermost.stop,
- mattermostAnswers: watch.mattermostAnswers,
- mattermostExpected: watch.mattermostExpected,
}));
const { LocalServerService } = await import('./local-server-service');
@@ -190,8 +184,6 @@ beforeEach(() => {
native.start.mockResolvedValue({ result: started, bringUpMattermost: null });
native.failureLine.mockResolvedValue(null);
watch.health.mockResolvedValue(true);
- watch.mattermostAnswers.mockResolvedValue(true);
- watch.mattermostExpected.mockResolvedValue(false);
// Docker servers stay on Docker unless a test is about moving them.
move.blocked.mockResolvedValue('not moved in this test');
});
@@ -864,54 +856,3 @@ it('ignores what it saw when a stop began while it looked', async () => {
expect(service.getStatus().phase).toBe('stopped');
expect(m.record).not.toHaveBeenCalledWith('crashed', expect.anything());
});
-
-it('checks the Mattermost of a server found running at launch, and notices it going', async () => {
- m.ports.mockResolvedValue({ api: 1, gateway: 2, postgres: 3, mattermost: 8065 });
- watch.mattermostExpected.mockResolvedValue(true);
- const service = await bootRunningNative();
- expect(service.getStatus().mattermostState).toBeNull();
-
- await service.watch();
- expect(watch.mattermostAnswers).toHaveBeenCalledWith(8065);
- expect(service.getStatus().mattermostState).toBe('running');
-
- watch.mattermostAnswers.mockResolvedValue(false);
- await service.watch();
- await service.watch();
- expect(service.getStatus().mattermostState).toBe('running');
- await service.watch();
- expect(service.getStatus()).toMatchObject({ phase: 'running', mattermostState: 'failed' });
- expect(service.getStatus().notice).toMatch(/Mattermost stopped answering/);
-
- watch.mattermostAnswers.mockResolvedValue(true);
- await service.watch();
- expect(service.getStatus()).toMatchObject({ mattermostState: 'running', notice: null });
-});
-
-it('does not look for a Mattermost that was never set up or is turned off', async () => {
- m.ports.mockResolvedValue({ api: 1, gateway: 2, postgres: 3, mattermost: 8065 });
- const service = await bootRunningNative();
- await service.watch();
- expect(watch.mattermostAnswers).not.toHaveBeenCalled();
- expect(service.getStatus().mattermostState).toBeNull();
-});
-
-it('leaves a Mattermost that failed during its start to the start to report', async () => {
- runtime.read.mockResolvedValue('native');
- m.ports.mockResolvedValue({ api: 1, gateway: 2, postgres: 3, mattermost: 8065 });
- native.start.mockResolvedValue({
- result: started,
- bringUpMattermost: async () => {
- throw new Error('seeding failed');
- },
- });
- const service = new LocalServerService();
- await boot(service);
- await service.start();
- native.running.mockResolvedValue(true);
- await vi.waitFor(() => expect(service.getStatus().mattermostState).toBe('failed'));
-
- await service.watch();
- expect(watch.mattermostAnswers).not.toHaveBeenCalled();
- expect(service.getStatus().notice).toMatch(/seeding failed/);
-});
diff --git a/console/apps/switch-console-desktop/src/main/core/managed-switch-server/local-server-service.ts b/console/apps/switch-console-desktop/src/main/core/managed-switch-server/local-server-service.ts
index fd9e47f34..888fffbc6 100644
--- a/console/apps/switch-console-desktop/src/main/core/managed-switch-server/local-server-service.ts
+++ b/console/apps/switch-console-desktop/src/main/core/managed-switch-server/local-server-service.ts
@@ -50,13 +50,7 @@ import {
type UpgradeJournal,
upgradeState,
} from './managed-upgrade';
-import {
- mattermostAnswers,
- mattermostExpected,
- readMattermostEnabled,
- stopMattermost,
- writeMattermostEnabled,
-} from './native/mattermost';
+import { readMattermostEnabled, stopMattermost, writeMattermostEnabled } from './native/mattermost';
import {
type DockerExport,
exportDockerStack,
@@ -104,10 +98,6 @@ const WATCH_INTERVAL_MS = 5_000;
* answering: one miss is a busy moment, not a verdict. */
const MISSES_ALLOWED = 3;
-const MATTERMOST_LOST_NOTICE =
- 'The server is running, but Mattermost stopped answering, so nothing reaches the server ' +
- 'from it. Restart the server to bring Mattermost back.';
-
function errorMessage(error: unknown): string {
return error instanceof Error ? error.message : String(error);
}
@@ -160,14 +150,12 @@ export class LocalServerService {
/** Watches the running server for it stopping on its own; see {@link watch}. */
private watchTimer: ReturnType<typeof setInterval> | null = null;
private watching = false;
- /** The {@link mattermostRun} the counters below were counted in: a start or
- * stop since begins them again. */
- private watchedRun = -1;
+ /** Counts phase changes, so a watch that a start, stop or reset overtook
+ * while it looked does not report on the server that replaced the one it saw. */
+ private phaseChanges = 0;
+ /** Probes missed in a row, counted since {@link missesSince} phase changes. */
private coreMisses = 0;
- private mattermostMisses = 0;
- /** Whether the watch, rather than a start, marked Mattermost failed: only
- * then does Mattermost answering again mean it is back. */
- private mattermostLost = false;
+ private missesSince = -1;
getStatus(): LocalServerStatus {
return this.status;
@@ -254,6 +242,7 @@ export class LocalServerService {
private setStatus(patch: Partial<LocalServerStatus>): void {
// Any change of phase settles what failed, unless the patch says.
const failedTo = patch.phase !== undefined ? (patch.failedTo ?? null) : this.status.failedTo;
+ if (patch.phase !== undefined) this.phaseChanges++;
this.status = { ...this.status, ...patch, failedTo };
events.emit(localServerStatusChannel, this.status);
}
@@ -414,8 +403,6 @@ export class LocalServerService {
* reached. A server whose process is gone is marked so at once; one whose
* process is there but whose gateway misses {@link MISSES_ALLOWED} probes in
* a row is marked as not answering, and back to running if it answers again.
- * The Mattermost beside a native server is watched the same way, including
- * one found running at launch, which nothing else checks.
*
* Runs on a timer from boot; public so a test can drive it.
*/
@@ -425,16 +412,14 @@ export class LocalServerService {
const lost = phase === 'error' && failedTo === 'run';
if (phase !== 'running' && !lost) return;
this.watching = true;
- const run = this.mattermostRun;
- if (run !== this.watchedRun) {
- this.watchedRun = run;
+ const seen = this.phaseChanges;
+ if (seen !== this.missesSince) {
+ this.missesSince = seen;
this.coreMisses = 0;
- this.mattermostMisses = 0;
- this.mattermostLost = false;
}
// A start, stop or reset that began while this looked: what it saw is of
// a server that is being replaced.
- const overtaken = () => run !== this.mattermostRun || this.busy || this.starting !== null;
+ const overtaken = () => seen !== this.phaseChanges || this.busy || this.starting !== null;
const host: ServerHost = new LocalServerHost();
try {
const server = await getManagedServer();
@@ -488,10 +473,7 @@ export class LocalServerService {
`switch-core is running but has not answered for about ${seconds} seconds.`
);
return;
- } else {
- return;
}
- if (native) await this.watchMattermost(host, layout, overtaken);
} catch (error) {
log.warn('local-switch-server: could not check on the server', { error: String(error) });
} finally {
@@ -516,45 +498,6 @@ export class LocalServerService {
await recordLocalActivity('crashed', error);
}
- /** The Mattermost half of {@link watch}, for a native server that answers. */
- private async watchMattermost(
- host: ServerHost,
- layout: NativeLayout,
- overtaken: () => boolean
- ): Promise<void> {
- const state = this.status.mattermostState;
- // Null: running since before this launch, so nothing has looked yet.
- // Starting is the start's to report, and off is off; failed is watched
- // only when this is what said so.
- const watched =
- state === 'running' ||
- (state === null && (await mattermostExpected(layout, this.status.mattermost))) ||
- (state === 'failed' && this.mattermostLost);
- if (!watched) return;
- const ports = await readPersistedPorts(host);
- if (ports === null) return;
- const answers = await mattermostAnswers(ports.mattermost);
- if (overtaken() || this.status.phase !== 'running' || this.status.mattermostState !== state) {
- return;
- }
- if (answers) {
- this.mattermostMisses = 0;
- if (state === 'running') return;
- if (this.mattermostLost) log.info('local-switch-server: Mattermost is answering again');
- this.mattermostLost = false;
- this.setStatus({
- mattermostState: 'running',
- ...(state === 'failed' ? { notice: null } : {}),
- });
- return;
- }
- if (state === 'failed' || ++this.mattermostMisses < MISSES_ALLOWED) return;
- this.mattermostMisses = 0;
- this.mattermostLost = true;
- log.warn('local-switch-server: Mattermost is not answering');
- this.setStatus({ mattermostState: 'failed', notice: MATTERMOST_LOST_NOTICE });
- }
-
/**
* Resolves once sessions may run against this server, waiting out the boot
* check and any upgrade in flight. Throws when the server still owes an
diff --git a/console/apps/switch-console-desktop/src/main/core/managed-switch-server/native/mattermost.ts b/console/apps/switch-console-desktop/src/main/core/managed-switch-server/native/mattermost.ts
index 5f50cd075..2666e0b1a 100644
--- a/console/apps/switch-console-desktop/src/main/core/managed-switch-server/native/mattermost.ts
+++ b/console/apps/switch-console-desktop/src/main/core/managed-switch-server/native/mattermost.ts
@@ -243,37 +243,6 @@ export function pullMattermostImage(onLog: (line: string) => void): Promise<void
});
}
-/**
- * Whether Mattermost has been brought up beside this server at some point and
- * is not turned off: what a server Console finds running at launch is
- * expected to have beside it. `enabled` is {@link readMattermostEnabled}'s
- * answer, null when the user never chose.
- */
-export async function mattermostExpected(
- layout: NativeLayout,
- enabled: boolean | null
-): Promise<boolean> {
- if (enabled === false) return false;
- try {
- await readFile(path.join(layout.root, COMPOSE_FILE), 'utf8');
- return true;
- } catch {
- return false;
- }
-}
-
-/** Whether the Mattermost on `port` answers. */
-export async function mattermostAnswers(port: number, timeoutMs = 5000): Promise<boolean> {
- try {
- const res = await fetch(`http://localhost:${port}/api/v4/system/ping`, {
- signal: AbortSignal.timeout(timeoutMs),
- });
- return res.ok;
- } catch {
- return false;
- }
-}
-
/** Stop Mattermost, keeping its data. A machine without Docker, or one that
* never started it, has nothing to stop. */
export async function stopMattermost(layout: NativeLayout, removeData: boolean): Promise<void> {