Skip to content

Commit c7f0d87

Browse files
authored
bug: fix cvars not finishing (#129)
1 parent ca987fc commit c7f0d87

1 file changed

Lines changed: 18 additions & 17 deletions

File tree

src/rcon/rcon.service.ts

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ export class RconService {
2121
) {}
2222

2323
private CONNECTION_TIMEOUT = 3 * 1000;
24+
private readonly GENERATE_CVARS_CACHE_KEY = "generate_cvars";
2425

2526
private connections: Record<string, RconClient> = {};
2627
private connectTimeouts: Record<string, NodeJS.Timeout> = {};
27-
private activeOperations = new Set<string>();
2828

2929
public async connect(serverId: string): Promise<RconClient | null> {
3030
if (this.connections[serverId]) {
@@ -125,12 +125,12 @@ export class RconService {
125125

126126
const version = server.game_server_node?.version;
127127
if (server.is_dedicated && !version) {
128-
if ((await this.cache.has("cvars")) === false) {
129-
this.genreateCvars(serverId).catch(() => {});
128+
if ((await this.cache.has(this.GENERATE_CVARS_CACHE_KEY)) === false) {
129+
this.generateCvars(serverId).catch(() => {});
130130
}
131131
} else if (version?.current === true && version?.cvars === false) {
132-
if ((await this.cache.has("cvars")) === false) {
133-
this.genreateCvars(serverId).catch(() => {});
132+
if ((await this.cache.has(this.GENERATE_CVARS_CACHE_KEY)) === false) {
133+
this.generateCvars(serverId).catch(() => {});
134134
}
135135
}
136136
} catch {
@@ -179,9 +179,6 @@ export class RconService {
179179

180180
private setupConnectionTimeout(serverId: string) {
181181
clearTimeout(this.connectTimeouts[serverId]);
182-
if (this.activeOperations.has(serverId)) {
183-
return;
184-
}
185182
this.connectTimeouts[serverId] = setTimeout(async () => {
186183
await this.disconnect(serverId);
187184
}, this.CONNECTION_TIMEOUT);
@@ -197,7 +194,7 @@ export class RconService {
197194
}
198195
}
199196

200-
public async genreateCvars(serverId: string) {
197+
public async generateCvars(serverId: string) {
201198
const { servers_by_pk: server } = await this.hasuraService.query({
202199
servers_by_pk: {
203200
__args: {
@@ -258,13 +255,13 @@ export class RconService {
258255

259256
this.logger.log(`generating cvars for build: ${buildId}`);
260257

258+
let rcon: RconClient | null = null;
261259
try {
262-
const rcon = await this.connect(serverId);
260+
rcon = await this.connect(serverId);
263261
if (!rcon) {
264262
throw Error(`unable to connect to server ${serverId}`);
265263
}
266264

267-
this.activeOperations.add(serverId);
268265
clearTimeout(this.connectTimeouts[serverId]);
269266
delete this.connectTimeouts[serverId];
270267

@@ -282,16 +279,19 @@ export class RconService {
282279
description: string;
283280
}> = [];
284281

285-
try {
286-
for (const prefix of prefixes) {
282+
for (const prefix of prefixes) {
283+
try {
287284
const parsedCvars = this.parseCvarList(
288285
await rcon.send(`Cvarlist ${prefix}`),
289286
);
290287
allCvars.push(...parsedCvars);
288+
} catch (error) {
289+
this.logger.error(
290+
`unable to generate cvars for build: ${buildId} on prefix ${prefix}`,
291+
error,
292+
);
293+
throw error;
291294
}
292-
} finally {
293-
this.activeOperations.delete(serverId);
294-
this.setupConnectionTimeout(serverId);
295295
}
296296

297297
await this.typeSenseService.resetCvars();
@@ -307,7 +307,7 @@ export class RconService {
307307
},
308308
});
309309

310-
await this.cache.put("cvars", true);
310+
await this.cache.put(this.GENERATE_CVARS_CACHE_KEY, true);
311311
this.logger.log(
312312
`generated ${allCvars.length} cvars for build: ${buildId}`,
313313
);
@@ -318,6 +318,7 @@ export class RconService {
318318
);
319319
await this.cache.put(failureCacheKey, true, 600);
320320
} finally {
321+
await this.disconnect(serverId);
321322
await this.releaseCvarsLock(buildId);
322323
}
323324
}

0 commit comments

Comments
 (0)