Skip to content

Commit 0d288e8

Browse files
committed
refactor(manage): run frontend
1 parent fb6fce6 commit 0d288e8

File tree

4 files changed

+68
-33
lines changed

4 files changed

+68
-33
lines changed

account/decorators.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from .models import User
66
from problem.models import Problem
7-
from contest.models import Contest, ContestType, ContestStatus, ContestRuleType
7+
from contest.models import Contest, ContestType, ContestStatus
88
from utils.api import JSONResponse, APIError
99
from utils.constants import CONTEST_PASSWORD_SESSION_KEY
1010
from .models import ProblemPermission
@@ -150,7 +150,6 @@ def ensure_created_by(obj, user):
150150
raise e
151151

152152

153-
# 确定对Lab Contest有管理权限
154153
def ensure_managed_by(contest: Contest, user: User):
155154
e = APIError("Illegal Manager")
156155
if not user.is_admin_role():

manage

+62-28
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ function print_help {
99

1010
cmd=$1
1111
apps=("account" "announcement" "conf" "contest" "options" "problem" "submission")
12+
backend="http://127.0.0.1:7890"
13+
backend_cmd="python3 manage.py runserver"
14+
frontend_cmd="node build/dev-server.js"
1215

1316
function clean {
1417
rm -rf ./data/zips
@@ -33,59 +36,90 @@ function run_onl {
3336
# using postgres
3437
# docker run -it -d -e POSTGRES_DB=onlinejudge -e POSTGRES_USER=onlinejudge -e POSTGRES_PASSWORD=onlinejudge -p 5435:5432 --name oj-postgres-dev postgres:10-alpine
3538

39+
if ! command which python3 | grep venv > /dev/null; then
40+
source venv/bin/activate
41+
fi
42+
3643
# run dramatiq
37-
pgrep dramatiq > /dev/null
38-
if [[ $? -ne 0 ]]; then
44+
if ! command pgrep dramatiq > /dev/null; then
3945
echo "running dramatiq ..."
4046
python3 manage.py rundramatiq &>data/log/dramatiq.log &
47+
[[ $? -ne 0 ]] && echo "Error: fail to run dramatiq"
4148
else
42-
echo "dramatiq is already running ..."
49+
echo "dramatiq is already running"
4350
fi
4451

4552
# run redis
46-
docker container ls | grep redis > /dev/null
47-
if [[ $? -ne 0 ]]; then
53+
if ! command docker container ls | grep redis > /dev/null; then
4854
echo "running redis in docker ..."
49-
docker ps -a | grep redis > /dev/null
50-
if [[ $? -ne 0 ]]; then
55+
if ! command docker ps -a | grep redis > /dev/null; then
5156
docker run -it -d -p 6380:6379 --name oj-redis-dev redis:4.0-alpine > /dev/null
5257
else
5358
container_id=$(docker ps -a | grep redis | awk '{print $1}')
5459
docker container start $container_id
5560
fi
61+
[[ $? -ne 0 ]] && echo "Error: fail to run redis docker"
5662
else
5763
echo "redis is already running"
5864
fi
5965

6066
# run onl
61-
pgrep -f "python3 manage.py runserver" > /dev/null
62-
if [[ $? -ne 0 ]]; then
63-
echo "running onl ..."
67+
if ! command pgrep -f "$backend_cmd" > /dev/null; then
68+
echo "running onl controller ..."
6469
python3 manage.py runserver 0.0.0.0:7890 &>data/log/onl.log &
70+
[[ $? -ne 0 ]] && echo "Error: fail to run onl controll"
71+
else
72+
echo "onl controller is already running"
73+
fi
74+
75+
# run frontend
76+
if ! command pgrep -f "$frontend_cmd" > /dev/null; then
77+
echo "running onl frontend ..."
78+
if [[ -z $FRONTEND ]]; then
79+
echo "environment variable FRONTEND is empty, you should run frontend yourself"
80+
else
81+
fe_log_path="$(pwd)/data/log/npm.log"
82+
export NVM_DIR="$HOME/.nvm"
83+
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
84+
(cd $FRONTEND; nvm use v8.12.0; TARGET=$backend npm run dev &> $fe_log_path &)
85+
[[ $? -ne 0 ]] && echo "Error: fail to run onl frontend"
86+
fi
6587
else
66-
echo "onl is already running ..."
88+
echo "onl frontend is already running"
6789
fi
6890
}
6991

7092
function stop_onl {
7193
pkill dramatiq
72-
pkill -f "python3 manage.py runserver"
94+
pkill -f "$backend_cmd"
7395
docker container stop oj-redis-dev > /dev/null
96+
pkill -f "$frontend_cmd"
7497
}
7598

76-
if [[ $cmd == "make" || $cmd == "makemigrations" ]]; then
77-
for app in "${apps[@]}"; do
78-
python3 manage.py makemigrations $app
79-
done
80-
elif [[ $cmd == "migrate" ]]; then
81-
migrate
82-
elif [[ $cmd == "clean" ]]; then
83-
clean
84-
elif [[ $cmd == "rebuild" ]]; then
85-
clean
86-
migrate
87-
elif [[ $cmd == "run" ]]; then
88-
run_onl
89-
elif [[ $cmd == "stop" ]]; then
90-
stop_onl
91-
fi
99+
case $cmd in
100+
"make" | "makemigrations" )
101+
for app in "${apps[@]}"; do
102+
python3 manage.py makemigrations $app
103+
done
104+
;;
105+
"migrate" )
106+
migrate
107+
;;
108+
"clean" )
109+
clean
110+
;;
111+
"rebuild" )
112+
clean
113+
rebuild
114+
;;
115+
"run" )
116+
run_onl
117+
;;
118+
"stop" )
119+
stop_onl
120+
;;
121+
* )
122+
echo "unknown commmand: $cmd"
123+
print_help
124+
;;
125+
esac

nginx.conf

+4
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@ events {
1818
worker_connections 1024;
1919
}
2020

21+
error_log /var/log/nginx/error.log;
22+
2123
http {
24+
access_log /var/log/nginx/access.log;
25+
2226
# Don't tell nginx version to clients.
2327
server_tokens off;
2428

submission/views/user.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
from account.models import User, UserProfile
1010
from conf.models import JudgeServer
1111
from contest.models import Contest, ContestStatus
12-
from judge.testing import SubmissionTester
1312
from options.options import SysOptions
1413
from problem.models import Problem
1514
from judge.tasks import local_judge_task
@@ -21,7 +20,6 @@
2120

2221
from ..models import Submission
2322
from ..serializers import (
24-
CreateSubmissionSerializer,
2523
SubmissionModelSerializer,
2624
ShareSubmissionSerializer,
2725
)
@@ -124,7 +122,7 @@ def post(self, request):
124122
submission.save()
125123

126124
# execute judge task in dramatiq
127-
local_judge_task.send(submission.id, problem._id, str(request.user.id))
125+
local_judge_task.send(submission.id, problem.id, str(request.user.id))
128126

129127
return self.success(SubmissionModelSerializer(submission).data)
130128

0 commit comments

Comments
 (0)