Skip to content

Commit bcc4243

Browse files
committed
bug: dont restart dedicated servers too fast
1 parent 0bb967d commit bcc4243

2 files changed

Lines changed: 47 additions & 33 deletions

File tree

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { MatchAssistantService } from "../match-assistant/match-assistant.service";
2+
import { Job } from "bullmq";
3+
import { UseQueue } from "../../utilities/QueueProcessors";
4+
import { MatchQueues } from "../enums/MatchQueues";
5+
import { WorkerHost } from "@nestjs/bullmq";
6+
7+
@UseQueue("Matches", MatchQueues.ScheduledMatches)
8+
export class RestartDedicatedServer extends WorkerHost {
9+
constructor(private readonly matchAssistant: MatchAssistantService) {
10+
super();
11+
}
12+
13+
async process(
14+
job: Job<{
15+
serverId: string;
16+
}>,
17+
): Promise<void> {
18+
const { serverId } = job.data;
19+
await this.matchAssistant.restartDedicatedServer(serverId);
20+
}
21+
}

src/matches/matches.controller.ts

Lines changed: 26 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import { S3Service } from "src/s3/s3.service";
3030
import { ChatService } from "src/chat/chat.service";
3131
import { ChatLobbyType } from "src/chat/enums/ChatLobbyTypes";
3232
import { MatchRelayService } from "./match-relay/match-relay.service";
33+
import { RestartDedicatedServer } from "./jobs/RestartDedicatedServer";
3334

3435
@Controller("matches")
3536
export class MatchesController {
@@ -345,41 +346,33 @@ export class MatchesController {
345346
},
346347
});
347348

348-
if (server.is_dedicated) {
349-
await this.matchAssistant.restartDedicatedServer(serverId);
350-
} else {
351-
if (status === "Canceled" || data.op === "DELETE") {
352-
await this.matchAssistant.stopOnDemandServer(matchId);
353-
} else {
354-
const { match_options_by_pk: matchOptions } = await this.hasura.query(
355-
{
356-
match_options_by_pk: {
357-
__args: {
358-
id: data.new.match_options_id,
359-
},
360-
tv_delay: true,
361-
},
362-
},
363-
);
364-
365-
if (!matchOptions) {
366-
await this.matchAssistant.stopOnDemandServer(matchId);
367-
} else {
368-
const tvDelay = matchOptions.tv_delay || 0;
369-
370-
this.logger.log(
371-
`[${matchId}] adding stop on demand server job in ${tvDelay} seconds`,
372-
);
373-
374-
await this.scheduledMatchesQueue.add(
375-
StopOnDemandServer.name,
376-
{ matchId },
377-
tvDelay ? { delay: tvDelay * 1000 } : undefined,
378-
);
379-
}
380-
}
349+
const { match_options_by_pk: matchOptions } = await this.hasura.query({
350+
match_options_by_pk: {
351+
__args: {
352+
id: data.new.match_options_id,
353+
},
354+
tv_delay: true,
355+
},
356+
});
357+
358+
let delay = matchOptions?.tv_delay || 1;
359+
360+
if (status === "Canceled" || data.op === "DELETE") {
361+
delay = 0;
381362
}
382363

364+
this.logger.log(
365+
`[${matchId}] adding stop / restart server job in ${delay} seconds`,
366+
);
367+
368+
await this.scheduledMatchesQueue.add(
369+
server.is_dedicated
370+
? RestartDedicatedServer.name
371+
: StopOnDemandServer.name,
372+
{ serverId },
373+
delay ? { delay: delay * 1000 } : undefined,
374+
);
375+
383376
await this.hasura.mutation({
384377
update_matches_by_pk: {
385378
__args: {

0 commit comments

Comments
 (0)