Suno-style web UI for a self-hosted MiniMax-Music3 inference server
TypeScript ai-musicdiffusersdockerexpressminimaxminimax-musicmusic-generationreactself-hostedsgl-omnisunotailwindcsstext-to-musictypescriptvite
fix(server): recover failed JSON store writes; skip disk snapshots for render-only updates
Adam Benhassenc54126937c45
diff --git a/server/src/library.ts b/server/src/library.tsindex 1b72bf2..382f690 100644--- a/server/src/library.ts+++ b/server/src/library.ts@@ -40,8 +40,9 @@ export class JsonStore<T extends { id: string }> {async update(id: string, patch: Partial<T>): Promise<T | undefined> {const idx = this.tracks.findIndex((t) => t.id === id);if (idx === -1) return undefined;- this.tracks[idx] = { ...this.tracks[idx], ...patch };- await this.persist();+ const current = this.tracks[idx];+ this.tracks[idx] = { ...current, ...patch };+ if (this.shouldPersistUpdate(current, patch)) await this.persist();return this.tracks[idx];}@@ -53,16 +54,28 @@ export class JsonStore<T extends { id: string }> {return removed;}+ protected shouldPersistUpdate(_current: T, _patch: Partial<T>): boolean {+ return true;+ }+private persist(): Promise<void> {const snapshot = JSON.stringify(this.tracks, null, 2);- this.writing = this.writing.then(async () => {+ const write = this.writing.then(async () => {await fs.mkdir(path.dirname(this.file), { recursive: true });const tmp = `${this.file}.${process.pid}.tmp`;await fs.writeFile(tmp, snapshot, 'utf8');await fs.rename(tmp, this.file);});+ this.writing = write.catch((err) => {+ console.error(`failed to persist ${this.file}:`, err);+ });return this.writing;}}-export class Library extends JsonStore<Track> {}+export class Library extends JsonStore<Track> {+ /** Render state is volatile; lifecycle transitions are the durable snapshots. */+ protected override shouldPersistUpdate(current: Track, patch: Partial<Track>): boolean {+ return patch.status !== undefined && patch.status !== current.status;+ }+}diff --git a/server/src/settings.ts b/server/src/settings.tsindex c699603..b496f48 100644--- a/server/src/settings.ts+++ b/server/src/settings.ts@@ -103,12 +103,15 @@ export class SettingsStore {private persist(): Promise<void> {const snapshot = JSON.stringify(this.stored, null, 2);- this.writing = this.writing.then(async () => {+ const write = this.writing.then(async () => {await fs.mkdir(path.dirname(this.file), { recursive: true });const tmp = `${this.file}.${process.pid}.tmp`;await fs.writeFile(tmp, snapshot, { encoding: 'utf8', mode: 0o600 });await fs.rename(tmp, this.file);});+ this.writing = write.catch((err) => {+ console.error(`failed to persist ${this.file}:`, err);+ });return this.writing;}}diff --git a/server/test/library.test.ts b/server/test/library.test.tsindex 1965ca2..bcc3eb4 100644--- a/server/test/library.test.ts+++ b/server/test/library.test.ts@@ -1,4 +1,4 @@-import { describe, it, expect } from 'vitest';+import { describe, it, expect, afterEach, vi } from 'vitest';import fs from 'node:fs/promises';import os from 'node:os';import path from 'node:path';@@ -11,6 +11,8 @@ const mk = (id: string): Track => ({eta: null, error: null, file: null, createdAt: new Date().toISOString(), finishedAt: null,});+afterEach(() => vi.restoreAllMocks());+describe('Library', () => {it('loads empty when file missing, persists and reloads', async () => {const dir = await fs.mkdtemp(path.join(os.tmpdir(), 'lib-'));@@ -30,4 +32,40 @@ describe('Library', () => {expect(lib2.all().map((t) => t.id)).toEqual(['a']);expect(await lib2.update('nope', {})).toBeUndefined();});++ it('recovers and persists after a write fails', async () => {+ const dir = await fs.mkdtemp(path.join(os.tmpdir(), 'lib-'));+ const file = path.join(dir, 'library.json');+ const writeFile = vi.spyOn(fs, 'writeFile').mockRejectedValueOnce(new Error('disk full'));+ const error = vi.spyOn(console, 'error').mockImplementation(() => {});+ const lib = new Library(file);+ await lib.load();++ await expect(lib.add(mk('a'))).resolves.toMatchObject({ id: 'a' });+ await expect(lib.add(mk('b'))).resolves.toMatchObject({ id: 'b' });++ expect(error).toHaveBeenCalled();+ expect(writeFile).toHaveBeenCalledTimes(2);+ expect(JSON.parse(await fs.readFile(file, 'utf8')).map((t: Track) => t.id)).toEqual(['b', 'a']);+ });++ it('keeps render progress in memory until the next status transition', async () => {+ const dir = await fs.mkdtemp(path.join(os.tmpdir(), 'lib-'));+ const file = path.join(dir, 'library.json');+ const lib = new Library(file);+ await lib.load();+ await lib.add(mk('a'));+ const writeFile = vi.spyOn(fs, 'writeFile');++ await lib.update('a', { status: 'running', stage: 'semantic', progress: 0 });+ await lib.update('a', { stage: 'denoise', progress: 0.6, eta: 12, elapsed: 4, renderedSeconds: 3 });++ expect(writeFile).toHaveBeenCalledTimes(1);+ expect(lib.get('a')).toMatchObject({ stage: 'denoise', progress: 0.6, eta: 12, elapsed: 4, renderedSeconds: 3 });+ expect(JSON.parse(await fs.readFile(file, 'utf8'))[0]).toMatchObject({ status: 'running', stage: 'semantic', progress: 0 });++ await lib.update('a', { status: 'done', stage: 'done', progress: 1, eta: 0, elapsed: 10, renderedSeconds: null });+ expect(writeFile).toHaveBeenCalledTimes(2);+ expect(JSON.parse(await fs.readFile(file, 'utf8'))[0]).toMatchObject({ status: 'done', stage: 'done', progress: 1, eta: 0, elapsed: 10, renderedSeconds: null });+ });});diff --git a/server/test/settings.test.ts b/server/test/settings.test.tsnew file mode 100644index 0000000..b5c6ce4--- /dev/null+++ b/server/test/settings.test.ts@@ -0,0 +1,25 @@+import { describe, it, expect, afterEach, vi } from 'vitest';+import fs from 'node:fs/promises';+import os from 'node:os';+import path from 'node:path';+import { SettingsStore } from '../src/settings.js';++afterEach(() => vi.restoreAllMocks());++describe('SettingsStore', () => {+ it('recovers and persists after a write fails', async () => {+ const dir = await fs.mkdtemp(path.join(os.tmpdir(), 'settings-'));+ const file = path.join(dir, 'settings.json');+ const writeFile = vi.spyOn(fs, 'writeFile').mockRejectedValueOnce(new Error('disk full'));+ const error = vi.spyOn(console, 'error').mockImplementation(() => {});+ const settings = new SettingsStore(file, { musicApi: null, apiKey: null });+ await settings.load();++ await expect(settings.update({ musicApi: 'http://127.0.0.1:7862' })).resolves.toBeUndefined();+ await expect(settings.update({ compat: true })).resolves.toBeUndefined();++ expect(error).toHaveBeenCalled();+ expect(writeFile).toHaveBeenCalledTimes(2);+ expect(JSON.parse(await fs.readFile(file, 'utf8'))).toEqual({ musicApi: 'http://127.0.0.1:7862', apiKey: null, compat: true });+ });+});