mirror of
https://github.com/jakejarvis/sofa.git
synced 2026-10-15 19:45:42 -04:00
fix(core): allow scheduler jitter in update-check and telemetry throttles
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
4 files changed
+69
-4
No files matched your search
@@ -7,6 +7,9 @@ import { getInstanceId, getSetting, getUserCount, setSetting } from "./settings"
|
||||
const APP_VERSION = process.env.APP_VERSION || "0.0.0";
|
||||
const PUBLIC_API_URL = process.env.PUBLIC_API_URL || "https://public-api.sofa.watch";
|
||||
const REPORT_INTERVAL_MS = 24 * 60 * 60 * 1000; // 24 hours
|
||||
// Cron fires on exact boundaries but the timestamp is written after the network
|
||||
// call finishes, so allow some slack or every other scheduled run is skipped.
|
||||
const SCHEDULE_TOLERANCE_MS = 10 * 60 * 1000;
|
||||
|
||||
const log = createLogger("telemetry");
|
||||
|
||||
@@ -40,7 +43,7 @@ export async function performTelemetryReport(): Promise<void> {
|
||||
const lastReported = getSetting("telemetryLastReportedAt");
|
||||
if (lastReported) {
|
||||
const elapsed = Date.now() - new Date(lastReported).getTime();
|
||||
if (elapsed < REPORT_INTERVAL_MS) {
|
||||
if (elapsed < REPORT_INTERVAL_MS - SCHEDULE_TOLERANCE_MS) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,6 +9,9 @@ const log = createLogger("update-check");
|
||||
const VERSION_PREFIX_RE = /^v/;
|
||||
const PUBLIC_API_URL = process.env.PUBLIC_API_URL || "https://public-api.sofa.watch";
|
||||
const CHECK_INTERVAL_MS = 6 * 60 * 60 * 1000; // 6 hours
|
||||
// Cron fires on exact boundaries but the timestamp is written after the network
|
||||
// call finishes, so allow some slack or every other scheduled run is skipped.
|
||||
const SCHEDULE_TOLERANCE_MS = 10 * 60 * 1000;
|
||||
|
||||
export interface UpdateCheckResult {
|
||||
updateAvailable: boolean;
|
||||
@@ -63,7 +66,7 @@ export async function performUpdateCheck(): Promise<UpdateCheckResult> {
|
||||
const lastChecked = getSetting("updateCheckLastCheckedAt");
|
||||
if (lastChecked) {
|
||||
const elapsed = Date.now() - new Date(lastChecked).getTime();
|
||||
if (elapsed < CHECK_INTERVAL_MS) {
|
||||
if (elapsed < CHECK_INTERVAL_MS - SCHEDULE_TOLERANCE_MS) {
|
||||
return getCachedUpdateCheck();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -91,6 +91,19 @@ describe("performTelemetryReport", () => {
|
||||
expect(globalThis.fetch).toHaveBeenCalledOnce();
|
||||
});
|
||||
|
||||
test("reports on the next daily run despite last run's latency", async () => {
|
||||
setSetting("telemetryEnabled", "true");
|
||||
const lastReported = new Date(TEST_NOW.getTime() - (24 * 60 * 60 * 1000 - 30_000));
|
||||
setSetting("telemetryLastReportedAt", lastReported.toISOString());
|
||||
|
||||
vi.spyOn(globalThis, "fetch").mockImplementation(
|
||||
(async () => new Response(null, { status: 200 })) as unknown as typeof fetch,
|
||||
);
|
||||
|
||||
await performTelemetryReport();
|
||||
expect(globalThis.fetch).toHaveBeenCalledOnce();
|
||||
});
|
||||
|
||||
test("does not throw on fetch failure", async () => {
|
||||
setSetting("telemetryEnabled", "true");
|
||||
|
||||
|
||||
@@ -1,6 +1,24 @@
|
||||
import { describe, expect, test } from "vitest";
|
||||
import { afterAll, beforeAll, beforeEach, describe, expect, test, vi } from "vitest";
|
||||
|
||||
import { isNewerVersion } from "../src/update-check";
|
||||
import { clearAllTables } from "@sofa/test/db";
|
||||
|
||||
import { setSetting } from "../src/settings";
|
||||
import { isNewerVersion, performUpdateCheck } from "../src/update-check";
|
||||
|
||||
const TEST_NOW = new Date("2026-03-01T12:00:00Z");
|
||||
|
||||
beforeAll(() => {
|
||||
vi.useFakeTimers();
|
||||
vi.setSystemTime(TEST_NOW);
|
||||
});
|
||||
|
||||
afterAll(() => {
|
||||
vi.useRealTimers();
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
clearAllTables();
|
||||
});
|
||||
|
||||
describe("isNewerVersion", () => {
|
||||
test("newer major version", () => {
|
||||
@@ -43,3 +61,31 @@ describe("isNewerVersion", () => {
|
||||
expect(isNewerVersion("v1.0.0", "v1.0.0")).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("performUpdateCheck", () => {
|
||||
test("checks again on the next 6-hour run despite last run's latency", async () => {
|
||||
const lastChecked = new Date(TEST_NOW.getTime() - (6 * 60 * 60 * 1000 - 30_000));
|
||||
setSetting("updateCheckLastCheckedAt", lastChecked.toISOString());
|
||||
|
||||
vi.spyOn(globalThis, "fetch").mockImplementation(
|
||||
(async () =>
|
||||
new Response(JSON.stringify({ version: "9.9.9", release_url: "https://x" }), {
|
||||
status: 200,
|
||||
headers: { "Content-Type": "application/json" },
|
||||
})) as unknown as typeof fetch,
|
||||
);
|
||||
|
||||
const result = await performUpdateCheck();
|
||||
|
||||
expect(globalThis.fetch).toHaveBeenCalledOnce();
|
||||
expect(result.latestVersion).toBe("9.9.9");
|
||||
});
|
||||
|
||||
test("skips when checked within the last few minutes", async () => {
|
||||
setSetting("updateCheckLastCheckedAt", new Date(TEST_NOW.getTime() - 60_000).toISOString());
|
||||
|
||||
const fetchSpy = vi.spyOn(globalThis, "fetch");
|
||||
await performUpdateCheck();
|
||||
expect(fetchSpy).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user