Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import * as apply from './features/apply.js';
import * as modcases from './features/modcases.js';
import * as batchmod from './features/batchmod.js';
import * as teamroles from './features/teamroles.js';
import * as perfsweep from './features/perfsweep.js';
import { startServer } from './mcp.js';
import { adminRoutes } from './admin.js';
import * as launcherGithub from './launcher-github.js';
Expand All @@ -50,7 +51,7 @@ import { dataFile, freeze } from './store.js';
import { publicKey } from './secrets.js';
import { applyBrand } from './brand.js';

const FEATURES = [modcases, moderation, batchmod, levels, rooms, community, forums, github, workboard, contributors, notify, site, smp, triage, agents, hub, updates, x, kiraAi, chatclear, sponsors, staff, support, apply, teamroles, teamvote, poll, age, elections];
const FEATURES = [modcases, moderation, batchmod, levels, rooms, community, forums, github, workboard, contributors, notify, site, smp, triage, agents, hub, updates, x, kiraAi, chatclear, sponsors, staff, support, apply, teamroles, teamvote, poll, age, elections, perfsweep];
const DISCORD = process.env.KIRA_DISCORD !== 'off';
const handlers = Object.assign({}, ...FEATURES.map((f) => f.handlers));
const commands = FEATURES.flatMap((f) => f.commands);
Expand Down
23 changes: 23 additions & 0 deletions src/features/perfsweep.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// The perf viewer's clean-up (horizon-site /api/perf/sweep): every 5 minutes Kira asks the site to delete expired
// and abandoned capture uploads from its bucket. Only when PERF_SWEEP_TOKEN is set, the same value as the site's;
// without it Kira never calls.
import { LINKS } from '../config.js';

export const EVERY_MS = 5 * 60_000;

// Returns how many captures the site deleted, or null when no token is set.
export async function sweep({ token = process.env.PERF_SWEEP_TOKEN, fetchImpl = fetch } = {}) {
if (!token) return null;
const response = await fetchImpl(`${LINKS.horizonSite}/api/perf/sweep`, {
method: 'POST',
headers: { Authorization: `Bearer ${token}` },
signal: AbortSignal.timeout(30_000),
});
if (!response.ok) throw new Error(`HTTP ${response.status}`);
return (await response.json()).deleted;
}

export function setup() {
if (!process.env.PERF_SWEEP_TOKEN) return;
setInterval(() => sweep().catch((e) => console.error('perf sweep', e.message)), EVERY_MS).unref?.();
}
5 changes: 4 additions & 1 deletion test/elections.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,10 @@ test('council polls: money lasts 48 hours, a rule only when asked; veto is never
});

test('the timer closes votes at 24 hours and takes the veto button away after 24 more', async () => {
const e = openE().find((x) => x.stage === 'vote');
// Its own vote at NOW: /elect and /vote above start on the real clock, so which of theirs is still open depends on the day.
const { election: e } = await el.startVote(M1, { body: 'mods', question: 'Does the timer close this vote on time?' }, { now: NOW });
for (const [x, v] of [[M1, 'yes'], [M3, 'yes'], [M4, 'no']]) await press(x, e.id, v);
assert.ok(core.state.elections.open[e.id], 'two Yes and one No of five settle nothing early');
await el.tick(e.endsAt + 1);
assert.equal(core.state.elections.open[e.id], undefined);
const left = Object.values(core.state.elections.closed).filter((c) => !c.vetoPoll);
Expand Down
15 changes: 15 additions & 0 deletions test/perfsweep.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { sweep } from '../src/features/perfsweep.js';

test('the sweep posts to the site with the token, and does nothing without one', async () => {
const calls = [];
const fetchImpl = async (url, options) => { calls.push({ url, options }); return { ok: true, json: async () => ({ deleted: 3 }) }; };
assert.equal(await sweep({ token: '', fetchImpl }), null);
assert.equal(calls.length, 0);
assert.equal(await sweep({ token: 's3cret', fetchImpl }), 3);
assert.equal(calls[0].url, 'https://playhorizon.dev/api/perf/sweep');
assert.equal(calls[0].options.method, 'POST');
assert.equal(calls[0].options.headers.Authorization, 'Bearer s3cret');
await assert.rejects(sweep({ token: 's3cret', fetchImpl: async () => ({ ok: false, status: 403 }) }), /HTTP 403/);
});
Loading