Phase8
This commit is contained in:
@@ -15,6 +15,7 @@
|
||||
"@hexahost/catalog": "workspace:*",
|
||||
"@hexahost/config": "workspace:*",
|
||||
"@hexahost/database": "workspace:*",
|
||||
"@hexahost/dns": "workspace:*",
|
||||
"@hexahost/metering": "workspace:*",
|
||||
"@hexahost/scheduler": "workspace:*",
|
||||
"@hexahost/storage": "workspace:*",
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { randomUUID } from 'node:crypto';
|
||||
|
||||
import { prisma } from '@hexahost/database';
|
||||
import { assignJoinAddress } from '@hexahost/dns';
|
||||
import { selectNodeForAllocation } from '@hexahost/scheduler';
|
||||
import { Job } from 'bullmq';
|
||||
import { z } from 'zod';
|
||||
@@ -11,7 +12,6 @@ import { loadNodeSnapshots } from '../node-snapshots';
|
||||
import { publishNodeCommand, waitForNodeResponse } from '../redis';
|
||||
import { transitionServerStatus } from '../server-state';
|
||||
|
||||
const BASE_HOST_PORT = 25565;
|
||||
const PROVISION_TIMEOUT_MS = 5 * 60 * 1000;
|
||||
|
||||
export const provisionServerJobSchema = z.object({
|
||||
@@ -23,6 +23,11 @@ function buildDataPath(serverId: string): string {
|
||||
}
|
||||
|
||||
async function findAvailableHostPort(nodeId: string): Promise<number> {
|
||||
const node = await prisma.gameNode.findUniqueOrThrow({
|
||||
where: { id: nodeId },
|
||||
select: { portRangeStart: true, portRangeEnd: true },
|
||||
});
|
||||
|
||||
const [allocations, servers] = await Promise.all([
|
||||
prisma.gameServerAllocation.findMany({
|
||||
where: { nodeId, status: 'ACTIVE' },
|
||||
@@ -44,9 +49,12 @@ async function findAvailableHostPort(nodeId: string): Promise<number> {
|
||||
}
|
||||
}
|
||||
|
||||
let port = BASE_HOST_PORT;
|
||||
let port = node.portRangeStart;
|
||||
while (usedPorts.has(port)) {
|
||||
port += 1;
|
||||
if (port > node.portRangeEnd) {
|
||||
throw new Error(`No free host port in range ${node.portRangeStart}-${node.portRangeEnd}`);
|
||||
}
|
||||
}
|
||||
|
||||
return port;
|
||||
@@ -171,6 +179,8 @@ export async function processProvisionServerJob(job: Job): Promise<{ status: str
|
||||
correlationId: messageId,
|
||||
});
|
||||
|
||||
await assignJoinAddress(serverId);
|
||||
|
||||
logger.info({ serverId, nodeId: node.id, hostPort }, 'Server provisioned');
|
||||
return { status: 'stopped' };
|
||||
}
|
||||
|
||||
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user