Skip to content

Commit

Permalink
feat: 컨테이너 서버 준비까지 기다리는 로직 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
flydog98 committed Dec 18, 2023
1 parent 55e1b38 commit 95b88cf
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 7 deletions.
8 changes: 8 additions & 0 deletions .github/workflows/backend-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ jobs:
-e CONTAINER_SERVER_HOST=${{ secrets.CONTAINER_SERVER_HOST }} \
-e CONTAINER_POOL_MAX=${{ secrets.CONTAINER_POOL_MAX }} \
${{ secrets.DOCKERHUB_USERNAME }}/git-challenge-backend:0.1
until [[ "$(curl -s http://localhost:8081/api/v1/health)" == "OK" ]]; do
echo 'Waiting for the container to be ready...'
sleep 5
done
docker rm -f backend-blue || true
curl -X POST -H "Content-Type: application/json" \
-d '{"status":"green"}' \
Expand All @@ -98,6 +102,10 @@ jobs:
-e CONTAINER_SERVER_HOST=${{ secrets.CONTAINER_SERVER_HOST }} \
-e CONTAINER_POOL_MAX=${{ secrets.CONTAINER_POOL_MAX }} \
${{ secrets.DOCKERHUB_USERNAME }}/git-challenge-backend:0.1
until [[ "$(curl -s http://localhost:8080/api/v1/health)" == "OK" ]]; do
echo 'Waiting for the container to be ready...'
sleep 5
done
docker rm -f backend-green || true
curl -X POST -H "Content-Type: application/json" \
-d '{"status":"blue"}' \
Expand Down
18 changes: 11 additions & 7 deletions packages/backend/src/app.controller.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import { Controller } from '@nestjs/common';
import { Controller, Get } from '@nestjs/common';
import { AppService } from './app.service';
import { ContainersService } from './containers/containers.service';

@Controller()
@Controller('api/v1')
export class AppController {
constructor(private readonly appService: AppService) {}
constructor(
private readonly appService: AppService,
private readonly containersService: ContainersService,
) {}

// @Get()
// getHello(): string {
// return this.appService.getHello();
// }
@Get('/health')
checkHealth(): string {
return this.containersService.isInitialized() ? 'OK' : 'Initializing';
}
}
4 changes: 4 additions & 0 deletions packages/backend/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import { LoggingInterceptor } from './common/logging.interceptor';
import { QuizWizardModule } from './quiz-wizard/quiz-wizard.module';
import { AiModule } from './ai/ai.module';
import { CommandModule } from './command/command.module';
import { ContainersModule } from './containers/containers.module';
import { ContainersService } from './containers/containers.service';

@Module({
imports: [
Expand Down Expand Up @@ -44,6 +46,7 @@ import { CommandModule } from './command/command.module';
QuizWizardModule,
AiModule,
CommandModule,
ContainersModule,
],
controllers: [AppController],
providers: [
Expand All @@ -52,6 +55,7 @@ import { CommandModule } from './command/command.module';
provide: 'APP_INTERCEPTOR',
useClass: LoggingInterceptor,
},
ContainersService,
],
})
export class AppModule {}
9 changes: 9 additions & 0 deletions packages/backend/src/containers/containers.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const BRANCH_ESCAPE = '23ASDF2312-ASDFAS223-ASDF2223';
@Injectable()
export class ContainersService {
private availableContainers: Map<number, string[]> = new Map();
private initialized: boolean = false;

constructor(
private configService: ConfigService,
Expand All @@ -25,9 +26,15 @@ export class ContainersService {
) {
if (this.configService.get<string>('SERVER_MODE') !== 'dev') {
this.initializeContainers();
} else {
this.initialized = true;
}
}

isInitialized() {
return this.initialized;
}

async initializeContainers() {
const { stdoutData: existingContainers } =
await this.commandService.executeCommand('docker ps -aq');
Expand All @@ -48,6 +55,8 @@ export class ContainersService {
this.availableContainers.set(i, containers);
}

this.initialized = true;

existingContainersSet.forEach((containerId) => {
this.commandService.executeCommand(`docker rm -f ${containerId}`);
});
Expand Down

0 comments on commit 95b88cf

Please sign in to comment.