Skip to content

Commit 2d70ef0

Browse files
committed
feature: store server logs for 1 day
1 parent 9af86ba commit 2d70ef0

4 files changed

Lines changed: 56 additions & 9 deletions

File tree

src/k8s/logging/logging.service.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,20 @@ export class LoggingService {
262262
}
263263
}
264264

265+
public async getJobStatus(jobName: string) {
266+
try {
267+
const job = await this.batchApi.readNamespacedJob({
268+
name: jobName,
269+
namespace: this.namespace,
270+
});
271+
return job.status;
272+
} catch (error) {
273+
if (error.code.toString() !== "404") {
274+
throw error;
275+
}
276+
}
277+
}
278+
265279
public async getJobPod(jobName: string) {
266280
try {
267281
const kc = new KubeConfig();

src/matches/match-assistant/match-assistant.service.ts

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import { Injectable, Logger } from "@nestjs/common";
22
import { HasuraService } from "../../hasura/hasura.service";
3-
import { BatchV1Api, CoreV1Api, KubeConfig } from "@kubernetes/client-node";
3+
import {
4+
BatchV1Api,
5+
CoreV1Api,
6+
KubeConfig,
7+
Exec,
8+
} from "@kubernetes/client-node";
49
import { RconService } from "../../rcon/rcon.service";
510
import { User } from "../../auth/types/User";
611
import { InjectQueue } from "@nestjs/bullmq";
@@ -447,7 +452,7 @@ export class MatchAssistantService {
447452
this.logger.log(`[${matchId}] assigning on demand server`);
448453

449454
if (match.server_id) {
450-
await this.stopOnDemandServer(matchId);
455+
await this.stopOnDemandServer(matchId, true);
451456
}
452457

453458
const kc = new KubeConfig();
@@ -613,6 +618,7 @@ export class MatchAssistantService {
613618
name: jobName,
614619
},
615620
spec: {
621+
ttlSecondsAfterFinished: 60 * 60 * 24,
616622
template: {
617623
metadata: {
618624
name: jobName,
@@ -787,7 +793,7 @@ export class MatchAssistantService {
787793

788794
return true;
789795
} catch (error) {
790-
await this.stopOnDemandServer(matchId);
796+
await this.stopOnDemandServer(matchId, true);
791797

792798
this.logger.error(
793799
`[${matchId}] unable to create on demand server`,
@@ -889,8 +895,8 @@ export class MatchAssistantService {
889895
}
890896
}
891897

892-
public async stopOnDemandServer(matchId: string) {
893-
this.logger.log(`[${matchId}] stopping match server`);
898+
public async stopOnDemandServer(matchId: string, remove = false) {
899+
this.logger.log(`[${matchId}] stopping match servers`);
894900

895901
const jobName = MatchAssistantService.GetMatchServerJobId(matchId);
896902

@@ -908,6 +914,20 @@ export class MatchAssistantService {
908914

909915
for (const pod of podList.items) {
910916
this.logger.verbose(`[${matchId}] remove pod`);
917+
918+
if (!remove) {
919+
await new Exec(kc).exec(
920+
this.namespace,
921+
pod.metadata!.name!,
922+
pod.spec!.containers?.at(0)?.name,
923+
["kill", "-SIGUSR1", "1"],
924+
process.stdout,
925+
process.stderr,
926+
process.stdin,
927+
false,
928+
);
929+
continue;
930+
}
911931
await core
912932
.deleteNamespacedPod({
913933
name: pod.metadata!.name!,
@@ -920,7 +940,12 @@ export class MatchAssistantService {
920940
});
921941
}
922942

943+
if (!remove) {
944+
return;
945+
}
946+
923947
this.logger.verbose(`[${matchId}] remove job`);
948+
924949
await batch
925950
.deleteNamespacedJob({
926951
name: jobName,

src/matches/matches.controller.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ 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";
3433

3534
@Controller("matches")
3635
export class MatchesController {

src/system/system.gateway.ts.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ export class SystemGateway {
3636
return;
3737
}
3838

39+
const isJob = service.startsWith("cs-update:") || service.startsWith("m-");
40+
3941
const stream = new PassThrough();
4042

4143
stream.on("data", (chunk) => {
@@ -51,19 +53,26 @@ export class SystemGateway {
5153
stream.end();
5254
});
5355

54-
stream.on("end", () => {
56+
stream.on("end", async () => {
57+
let jobFinshed = false;
58+
if (isJob) {
59+
const jobStatus = await this.loggingService.getJobStatus(service);
60+
if (jobStatus?.succeeded) {
61+
jobFinshed = true;
62+
}
63+
}
64+
5565
client.send(
5666
JSON.stringify({
5767
event: `logs:${service}`,
5868
data: JSON.stringify({
5969
end: true,
70+
job_finshed: jobFinshed,
6071
}),
6172
}),
6273
);
6374
});
6475

65-
let isJob = service.startsWith("cs-update:");
66-
6776
try {
6877
await this.loggingService.getServiceLogs(
6978
service.startsWith("cs-update:")

0 commit comments

Comments
 (0)