File size: 1,772 Bytes
cd8bd0a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import { NextRequest, NextResponse } from "next/server";
import { isAuthenticated } from "@/shared/utils/apiAuth";
import { runNow, getState } from "@/lib/db/vacuumScheduler";

export async function POST(request: NextRequest) {
  if (!(await isAuthenticated(request))) {
    return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
  }

  try {
    // Delegate to the scheduler so the manual run also writes
    // lastRunAt / lastDurationMs to key_value, which the UI reads
    // via getDatabaseSettings().stats.lastVacuumAt. Using the old
    // runManualVacuum() from core.ts was the root cause of the
    // "vacuum never persists" bug (issue #4437).
    const result = await runNow();

    if (result.success) {
      return NextResponse.json({
        success: true,
        message: `VACUUM completed in ${result.durationMs}ms`,
        duration: result.durationMs,
      });
    } else if (result.error === "already_running") {
      return NextResponse.json(
        {
          success: false,
          error: "A vacuum is already in progress",
        },
        { status: 409 }
      );
    } else {
      return NextResponse.json(
        {
          success: false,
          error: result.error || "VACUUM failed",
          duration: result.durationMs,
        },
        { status: 500 }
      );
    }
  } catch (error: any) {
    console.error("[API] VACUUM endpoint error:", error);
    return NextResponse.json(
      { error: "Failed to run VACUUM", details: error.message },
      { status: 500 }
    );
  }
}

export async function GET(request: NextRequest) {
  if (!(await isAuthenticated(request))) {
    return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
  }
  return NextResponse.json({ state: getState() });
}