Skip to content

Commit

Permalink
feat: docker에서 stdout, stderr, time 결과를 내도록 만듦
Browse files Browse the repository at this point in the history
  • Loading branch information
yechan2468 committed Nov 15, 2023
1 parent f5b5fe1 commit acb5197
Show file tree
Hide file tree
Showing 15 changed files with 73 additions and 957 deletions.
2 changes: 2 additions & 0 deletions be/algo-with-me-docker/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
docker-sh/
build.sh
132 changes: 0 additions & 132 deletions be/algo-with-me-docker/.gitignore

This file was deleted.

9 changes: 4 additions & 5 deletions be/algo-with-me-docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ARG NODE_VERSION=18.16.0
ARG NODE_VERSION=20.9.0
ARG ALPINE_VERSION=3.18

FROM node:${NODE_VERSION}-alpine
Expand All @@ -7,9 +7,8 @@ MAINTAINER Yechan Lee <[email protected]>
CMD "mkdir -p /algo-with-me"

WORKDIR /algo-with-me
COPY . /algo-with-me
RUN npm install
COPY --chmod=555 ./node-sh /algo-with-me/node-sh

EXPOSE 3000
# EXPOSE 3000

CMD ["node", "./dist/app.js"]
CMD ./node-sh/run.sh $COMPETITION_ID $USER_ID $PROBLEM_ID
1 change: 1 addition & 0 deletions be/algo-with-me-docker/docker-sh/createNetwork.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sudo docker network create --driver bridge isolatedNetwork

This comment has been minimized.

Copy link
@rladydgn

rladydgn Nov 15, 2023

Collaborator

컨테이너간 통신할때 쓰는거라는데 맞나요? 맞다면 왜 통신이 필요한가요?

This comment has been minimized.

Copy link
@yechan2468

yechan2468 Nov 15, 2023

Author Collaborator

네 컨테이너 간 통신할 때, 혹은 통신을 제한할 때 사용됩니다.
이후 통신을 제한하려고 만들어둔 것이기는 한데, 현재 createNetwork.sh는 사용되지 않고 있습니다.
일단 이 파일은 무시하셔도 좋을 것 같아요.

다만 나중에 통신이 필요한 경우가 한 가지 있을 것 같은데, docker에서 코드 실행이 다 끝났을 때 채점 서버에게 알려주는 용도로 통신이 필요할 것 같습니다.
docker가 채점을 마친 이후에 채점 서버에게 알려주지 않으면, 채점 서버는 docker가 코드 실행이 언제 끝날지 몰라 주기적으로 polling을 해야 할텐데 그것 때문에 필요하지 않을까 싶습니다.

3 changes: 3 additions & 0 deletions be/algo-with-me-docker/docker-sh/prune.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
sudo docker container stop algo-with-me-judge
sudo docker container prune --force
sudo docker image prune --force
14 changes: 14 additions & 0 deletions be/algo-with-me-docker/docker-sh/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
sudo docker run \
-p 3000:3000 \
-e COMPETITION_ID=$1 \
-e USER_ID=$2 \
-e PROBLEM_ID=$3 \
-v $HOME/algo-with-me/problems:/algo-with-me/problems:ro \
-v $HOME/algo-with-me/testcases:/algo-with-me/testcases:ro \
-v $HOME/algo-with-me/submissions:/algo-with-me/submissions \
--user $(id -u):$(id -g) \
--name algo-with-me-judge \
algo-with-me-judge:latest

#--network none \
#--network isolatedNetwork \
File renamed without changes.
30 changes: 30 additions & 0 deletions be/algo-with-me-docker/node-sh/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# PARAM
# $1 COMPETITION_ID
# $2 USER_ID
# $3 PROBLEM_ID

# DESCRIPTION
# SUBMISSION_JS_FILE에서 파일을 읽어, node로 실행한다.
# DETAIL_FILE에는 사용한 시간(sec)과 최대 memory 메모리 사용량(KB)이 공백(' ')으로 분리되어 기록된다.

mkdir -p "/algo-with-me/submissions/$1/$2/"

SUBMISSION_JS_FILE="/algo-with-me/submissions/$1/$2/$3.js"
DETAIL_FILE="/algo-with-me/submissions/$1/$2/$3.detail"

# 제출된 js 파일이 있으면 node로 js 파일 실행
# 주의: judge.sh와 run.sh는 execute 권한이 부여되어야 함
if [ -f "$SUBMISSION_JS_FILE" ]; then
echo "[algo-with-me] run.sh: started running $SUBMISSION_JS_FILE"
# -o FILE Write result to FILE
# -f FMT Custom format
# U Total number of CPU-seconds that the process used directly (in user mode), in seconds.
# e Elapsed real (wall clock) time used by the process, in seconds.
# M Maximum resident set size of the process during its lifetime, in Kilobytes.
/usr/bin/time -o "$DETAIL_FILE" -f "%e %M" /algo-with-me/node-sh/runJs.sh "$1" "$2" "$3"
echo "[algo-with-me] run.sh: successfully ran $SUBMISSION_JS_FILE"
else
echo "[algo-with-me] run.sh: cannot find submitted js file $SUBMISSION_JS_FILE"
fi

exit
19 changes: 19 additions & 0 deletions be/algo-with-me-docker/node-sh/runJs.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# PARAM
# $1 COMPETITION_ID
# $2 USER_ID
# $3 PROBLEM_ID

# DESCRIPTION
# node로 제출한 파일을 실행한다.
# stdout, stderr는 각각 STDOUT_FILE, STDERR_FILE에 기록된다.
# RESULT_FILE에는 제출한 파일의 solution() 값이 기록되는데, shell script 상에서는 할 수 없어 템플릿 코드에서 기록해주어야 한다.
# node 실행할 때 첫번째 인자로 RESULT_FILE의 파일 경로를 입력해준다.

SUBMISSION_JS_FILE="/algo-with-me/submissions/$1/$2/$3.js"
STDOUT_FILE="/algo-with-me/submissions/$1/$2/$3.stdout"
STDERR_FILE="/algo-with-me/submissions/$1/$2/$3.stderr"
RESULT_FILE="/algo-with-me/submissions/$1/$2/$3.result"

node "$SUBMISSION_JS_FILE" "$RESULT_FILE" 1> "$STDOUT_FILE" 2> "$STDERR_FILE"

exit
Binary file added be/algo-with-me-docker/node-sh/time
Binary file not shown.
Loading

0 comments on commit acb5197

Please sign in to comment.