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
570 changes: 570 additions & 0 deletions generated/schema.graphql

Large diffs are not rendered by default.

709 changes: 691 additions & 18 deletions generated/schema.ts

Large diffs are not rendered by default.

107,828 changes: 54,276 additions & 53,552 deletions generated/types.ts

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions hasura/metadata/databases/default/tables/public_servers.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ select_permissions:
- enabled
- game
- game_server_node_id
- hibernating
- host
- id
- is_dedicated
Expand Down Expand Up @@ -157,6 +158,7 @@ select_permissions:
- connected
- enabled
- game
- hibernating
- host
- id
- label
Expand Down Expand Up @@ -190,6 +192,7 @@ select_permissions:
- enabled
- game
- game_server_node_id
- hibernating
- host
- id
- is_dedicated
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE public.servers DROP COLUMN IF EXISTS hibernating;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE public.servers ADD COLUMN IF NOT EXISTS hibernating boolean NOT NULL DEFAULT false;
92 changes: 92 additions & 0 deletions src/game-server-node/game-server-node.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,95 @@ describe("GameServerNodeController ping disk alerts", () => {
expect(notifications.send).not.toHaveBeenCalled();
});
});

describe("GameServerNodeController server ping hibernation", () => {
let hasura: { query: jest.Mock; mutation: jest.Mock };
let controller: GameServerNodeController;

const server = (hibernating: boolean) => ({
plugin_version: "1.0.0",
plugin_runtime: "swiftlys2",
connected: true,
hibernating,
enabled: true,
steam_relay: null as null,
is_dedicated: true,
game_server_node_id: null as null,
current_match: null as null,
});

const ping = (query: Record<string, string>) =>
controller.ping({
params: { serverId: "server-1" },
query: { map: "de_overpass", pluginVersion: "1.0.0", ...query },
} as any);

const setsHibernating = (hibernating: boolean) =>
expect.objectContaining({
update_servers_by_pk: expect.objectContaining({
__args: expect.objectContaining({ _set: { hibernating } }),
}),
});

beforeEach(() => {
hasura = {
query: jest.fn(),
mutation: jest.fn().mockResolvedValue({}),
};
const queue = { add: jest.fn(), remove: jest.fn() };

controller = new GameServerNodeController(
{ warn: jest.fn(), log: jest.fn() } as any,
{} as any,
{ get: jest.fn().mockReturnValue({}) } as any,
hasura as any,
{} as any,
{} as any,
{} as any,
{} as any,
{} as any,
{} as any,
queue as any,
queue as any,
{} as any,
queue as any,
queue as any,
{} as any,
{} as any,
);
});

it("records a server that says it is hibernating", async () => {
hasura.query.mockResolvedValue({ servers_by_pk: server(false) });

await ping({ hibernating: "true" });

expect(hasura.mutation).toHaveBeenCalledWith(setsHibernating(true));
});

it("clears it once the server wakes", async () => {
hasura.query.mockResolvedValue({ servers_by_pk: server(true) });

await ping({ hibernating: "false" });

expect(hasura.mutation).toHaveBeenCalledWith(setsHibernating(false));
});

it("writes nothing while the state has not changed", async () => {
hasura.query.mockResolvedValue({ servers_by_pk: server(true) });

await ping({ hibernating: "true" });

expect(hasura.mutation).not.toHaveBeenCalledWith(setsHibernating(true));
expect(hasura.mutation).not.toHaveBeenCalledWith(setsHibernating(false));
});

// Plugins from before the flag existed never send it.
it("reads a plugin that does not say as awake", async () => {
hasura.query.mockResolvedValue({ servers_by_pk: server(true) });

await ping({});

expect(hasura.mutation).toHaveBeenCalledWith(setsHibernating(false));
});
});
22 changes: 22 additions & 0 deletions src/game-server-node/game-server-node.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -852,6 +852,7 @@ UNIT
plugin_version: true,
plugin_runtime: true,
connected: true,
hibernating: true,
enabled: true,
steam_relay: true,
is_dedicated: true,
Expand Down Expand Up @@ -884,6 +885,7 @@ UNIT
pk_columns: { id: serverId },
_set: {
connected: false,
hibernating: false,
offline_at: new Date().toISOString(),
},
},
Expand All @@ -897,6 +899,26 @@ UNIT
return;
}

// A server sleeping until someone connects is healthy, and says so on its
// ping. Plugins from before this never send the flag and read as awake.
const hibernating = request.query.hibernating === "true";

if (server.hibernating !== hibernating) {
await this.hasura.mutation({
update_servers_by_pk: {
__args: {
pk_columns: {
id: serverId,
},
_set: {
hibernating,
},
},
__typename: true,
},
});
}

if (
pluginVersion &&
(server.plugin_version !== pluginVersion ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ describe("MarkDedicatedServerOffline", () => {

expect(offlineWrite()).toEqual({
connected: false,
hibernating: false,
offline_at: expect.any(String),
});
expect(notifications.send).toHaveBeenCalledWith(
Expand Down
1 change: 1 addition & 0 deletions src/game-server-node/jobs/MarkDedicatedServerOffline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export class MarkDedicatedServerOffline extends WorkerHost {
},
_set: {
connected: false,
hibernating: false,
offline_at: server.offline_at ?? new Date().toISOString(),
},
},
Expand Down
Loading