Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(games): fix rcon error handling #2785

Merged
merged 2 commits into from
Jan 31, 2024
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
2 changes: 1 addition & 1 deletion src/game-coordinator/services/game-runtime.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export class GameRuntimeService implements OnModuleInit {
});
} catch (e) {
assertIsError(e);
this.logger.error(e.message);
this.logger.error(e);
}

return game;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ class RconStub {
send = jest.fn().mockResolvedValue(null);
end = jest.fn();
connect = jest.fn();
on = jest.fn();
off = jest.fn();
}

describe('ServerConfiguratorService', () => {
Expand Down
12 changes: 9 additions & 3 deletions src/game-coordinator/services/server-configurator.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,14 @@ export class ServerConfiguratorService implements OnModuleInit {
)
.filter((line) => Boolean(line));

const endListener = () => {
this.logger.verbose(`[${game.gameServer!.name}] rcon ended remotely`);
};

let rcon: Rcon | undefined;
try {
rcon = await controls.rcon();
rcon.on('end', () => endListener);

// reset connect info
game = await this.gamesService.update(game._id, {
Expand All @@ -163,12 +168,12 @@ export class ServerConfiguratorService implements OnModuleInit {
throw new GameServerNotAssignedError(game.id);
}

this.logger.debug(
this.logger.verbose(
`[${game.gameServer.name}] logsecret is ${game.logSecret}`,
);

for (const line of configLines) {
this.logger.debug(`[${game.gameServer.name}] ${line}`);
this.logger.verbose(`[${game.gameServer.name}] ${line}`);
await rcon.send(line);
if (/^changelevel|exec/.test(line)) {
await waitABit(1000 * 10);
Expand All @@ -180,7 +185,7 @@ export class ServerConfiguratorService implements OnModuleInit {
}
}

this.logger.debug(`[${game.gameServer.name}] server ready.`);
this.logger.verbose(`[${game.gameServer.name}] server ready.`);

const connectString = makeConnectString({
address: game.gameServer.address,
Expand Down Expand Up @@ -222,6 +227,7 @@ export class ServerConfiguratorService implements OnModuleInit {
assertIsError(error);
throw new CannotConfigureGameError(game, error.message);
} finally {
rcon?.off('end', endListener);
await rcon?.end();
}
}
Expand Down
23 changes: 9 additions & 14 deletions src/utils/create-rcon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,13 @@ interface CreateRconOptions {
rconPassword: string;
}

export const createRcon = (opts: CreateRconOptions): Promise<Rcon> =>
new Promise((resolve, reject) => {
const rcon = new Rcon({
host: opts.host,
port: opts.port,
password: opts.rconPassword,
timeout: 30000,
});

rcon.on('error', (error) => {
return reject(error);
});

rcon.connect().then(resolve).catch(reject);
export const createRcon = async (opts: CreateRconOptions): Promise<Rcon> => {
const rcon = new Rcon({
host: opts.host,
port: opts.port,
password: opts.rconPassword,
timeout: 30000,
});

return await rcon.connect();
};
Loading