11import { Injectable , Logger } from "@nestjs/common" ;
22import { 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" ;
49import { RconService } from "../../rcon/rcon.service" ;
510import { User } from "../../auth/types/User" ;
611import { 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 ,
0 commit comments