Remove autoscaling action cooldown

This commit is contained in:
2026-10-07 15:51:52 -04:00
parent eadf97a428
commit a7a600b687
13 changed files with 80 additions and 27 deletions
+32 -3
View File
@@ -14,7 +14,7 @@ const maps = zombieMaps('t6');
it('satisfies a T5 game-wide policy with the ten standard Zombies maps', () => {
const now = 2_000_000;
const policy: ScalingPolicy = { ...scalingSettings({ ...settings, game: 't5' }),
id: 'policy', revision: 1, nextActionAt: 0, pausedReason: null, updatedAt: '' };
id: 'policy', revision: 1, pausedReason: null, updatedAt: '' };
const members: ScalingMember[] = zombieMaps('t5').map(map => ({ name: `t5_zombie_${map.slug}_1`, mapSlug: map.slug,
identity: map.slug, state: 'empty', players: 0, observedAt: now, source: 'rcon', emptySince: 1, blocker: null, operationId: null }));
const snapshot = { members, reservedNames: members.map(m => m.name), keyPool: { total: 20, available: 10 }, blockers: [] };
@@ -32,7 +32,7 @@ async function setup() {
it('applies targets and spare deficits independently to every map', () => {
const now = 2_000_000;
const policy: ScalingPolicy = { ...scalingSettings(settings), id: 'policy', revision: 1, nextActionAt: 0, pausedReason: null, updatedAt: '' };
const policy: ScalingPolicy = { ...scalingSettings(settings), id: 'policy', revision: 1, pausedReason: null, updatedAt: '' };
const members: ScalingMember[] = maps.flatMap(map => [1, 2].map(n => ({ name: `t6_zombie_${map.slug}_${n}`, mapSlug: map.slug,
identity: `${map.slug}:${n}`, state: map.slug === 'town' ? 'occupied' : 'empty', players: map.slug === 'town' ? 1 : 0,
observedAt: now, source: 'rcon', emptySince: 1, blocker: null, operationId: null })));
@@ -42,7 +42,6 @@ it('applies targets and spare deficits independently to every map', () => {
snapshot.members = members.filter(m => m.name !== 't6_zombie_town_2');
snapshot.reservedNames = snapshot.members.map(m => m.name);
expect(planScaling(policy, snapshot, now)).toMatchObject({ action: 'create', server: 't6_zombie_town_2' });
expect(planScaling({ ...policy, nextActionAt: now + 1 }, snapshot, now).action).toBe('wait');
snapshot.members[0] = { ...snapshot.members[0], state: 'creating' };
expect(planScaling(policy, snapshot, now).action).toBe('wait');
});
@@ -87,3 +86,33 @@ it('holds game-wide growth when its shared pool is exhausted by another map', as
expect(await scaling.runAutoscaling()).toBe(false);
expect(db().prepare('SELECT COUNT(*) AS n FROM server_creation_jobs').get()).toEqual({ n: 0 });
});
it('moves to the next map as soon as the prior creation settles, without waiting between actions', async () => {
const { host, versions } = await prepareNativeHost();
const { db } = await import('../../src/lib/server/db/index.js');
const { config } = await import('../../src/lib/server/config.js');
const scaling = await import('../../src/lib/server/autoscaling/index.js');
for (const n of [1, 2]) db().prepare("INSERT INTO server_keys(id,game,pluto_key) VALUES(?,'t6',?)").run(`key-${n}`, `game-key-${n}`);
vi.useFakeTimers({ toFake: ['Date'] });
try {
const at = new Date().toISOString();
scaling.saveScaling(settings, undefined, undefined, scaling.previewScaling(settings).token);
expect(await scaling.runAutoscaling()).toBe(true);
const job = db().prepare('SELECT deployment_id,server FROM server_creation_jobs').get() as { deployment_id: string; server: string };
expect(job.server).toBe('t6_zombie_transit_1');
expect(await scaling.runAutoscaling()).toBe(false);
expect(db().prepare('SELECT COUNT(*) AS n FROM server_creation_jobs').get()).toEqual({ n: 1 });
// Record the first lobby becoming ready without advancing the clock.
db().prepare("UPDATE server_deployments SET state='done',finished_at=? WHERE id=?").run(at, job.deployment_id);
db().prepare(`INSERT INTO native_instances(id,server,host_id,library_id,image,port,created_at)
VALUES('ready-instance',?,?,?,?,28960,?)`).run(job.server, host.id, versions.get('t6'), config.defaultRuntimeImage, at);
db().prepare(`INSERT INTO servers(name,game,map,map_slug,container_state,container_checked_at,last_seen_at,last_checked_at,last_players)
VALUES(?,'t6','zm_transit','transit','running',?,?,?,0)`).run(job.server, at, at, at);
expect(await scaling.runAutoscaling()).toBe(true);
expect(db().prepare('SELECT server FROM server_creation_jobs ORDER BY rowid').all()).toEqual([
{ server: 't6_zombie_transit_1' }, { server: 't6_zombie_town_1' }
]);
expect(db().prepare('SELECT COUNT(*) AS n FROM autoscaling_decisions WHERE resolved=0').get()).toEqual({ n: 1 });
} finally { vi.useRealTimers(); }
});