mirror of
https://github.com/QingdaoU/OpenVJ.git
synced 2024-12-28 15:31:48 +00:00
分离robot和server的配置
This commit is contained in:
parent
a4cda490f0
commit
12acb60076
7
dockerfiles/robot/Dockerfile
Normal file
7
dockerfiles/robot/Dockerfile
Normal file
@ -0,0 +1,7 @@
|
||||
FROM python:3.5
|
||||
ENV PYTHONBUFFERED 1
|
||||
RUN mkdir -p /code /code/log
|
||||
COPY requirements.txt /tmp
|
||||
RUN pip install -i http://pypi.douban.com/simple -r /tmp/requirements.txt --trusted-host pypi.douban.com
|
||||
WORKDIR /code
|
||||
CMD bash /code/dockerfiles/robot/run.sh
|
5
dockerfiles/robot/docker-compose.remote-example.yml
Normal file
5
dockerfiles/robot/docker-compose.remote-example.yml
Normal file
@ -0,0 +1,5 @@
|
||||
robot:
|
||||
image: robot
|
||||
environment:
|
||||
- REDIS_PORT_6379_TCP_ADDR=
|
||||
- REDIS_PASSWORD=
|
6
dockerfiles/robot/requirements.txt
Normal file
6
dockerfiles/robot/requirements.txt
Normal file
@ -0,0 +1,6 @@
|
||||
django
|
||||
pymysql
|
||||
celery
|
||||
requests
|
||||
djangorestframework
|
||||
redis
|
2
dockerfiles/robot/run.sh
Normal file
2
dockerfiles/robot/run.sh
Normal file
@ -0,0 +1,2 @@
|
||||
#!/usr/bin/env bash
|
||||
celery -A robot.tasks worker -l DEBUG
|
@ -2,6 +2,16 @@ redis:
|
||||
image: redis
|
||||
volumes:
|
||||
- /home/data/vj_redis:/data
|
||||
ports:
|
||||
- "0.0.0.0:3306:3306"
|
||||
command:
|
||||
- redis-server
|
||||
- /etc/redis/redis.conf
|
||||
- --requirepass $REDIS_PASSWORD
|
||||
environment:
|
||||
- robot_env=server
|
||||
# Please change this password to a long random string!!!
|
||||
- REDIS_PASSWORD=
|
||||
|
||||
mysql:
|
||||
image: mysql
|
||||
|
@ -6,4 +6,5 @@ fi
|
||||
find /code -name "*.pyc" -delete
|
||||
python -m compileall /code
|
||||
gunicorn openvj.wsgi:application -b 0.0.0.0:8080 --user nobody --group nogroup --reload &
|
||||
celery -A openvj worker -l DEBUG -Q local &
|
||||
wait
|
@ -21,14 +21,8 @@ DATABASES = {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
REDIS = {
|
||||
"HOST": "127.0.0.1",
|
||||
"PORT": 6379,
|
||||
"PASSWORD": "123456"
|
||||
}
|
||||
|
||||
|
||||
# celery配置
|
||||
BROKER_URL = "redis://{host}:{port}/{db}".format(host=REDIS["HOST"], port=REDIS["PORT"], db=0)
|
||||
CELERY_RESULT_BACKEND = "redis"
|
||||
REDIS_LOCAL_QUEUE = {
|
||||
"host": "127.0.0.1",
|
||||
"port": 6379,
|
||||
"db": 3
|
||||
}
|
@ -26,12 +26,9 @@ DATABASES = {
|
||||
}
|
||||
}
|
||||
|
||||
REDIS_QUEUE = {
|
||||
REDIS_LOCAL_QUEUE = {
|
||||
"host": os.environ["REDIS_PORT_6379_TCP_ADDR"],
|
||||
"port": 6379,
|
||||
"db": 3
|
||||
"db": 3,
|
||||
"password": os.environ["REDIS_PASSWORD"]
|
||||
}
|
||||
|
||||
# celery配置
|
||||
BROKER_URL = 'redis://%s:%s/%s' % (REDIS_QUEUE["host"], str(REDIS_QUEUE["port"]), str(REDIS_QUEUE["db"]))
|
||||
CELERY_RESULT_BACKEND = "redis"
|
||||
|
@ -117,6 +117,21 @@ STATIC_URL = '/static/'
|
||||
|
||||
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
|
||||
|
||||
|
||||
def _redis_password():
|
||||
password = REDIS_LOCAL_QUEUE.get("password")
|
||||
if password:
|
||||
return password + "@"
|
||||
else:
|
||||
return ""
|
||||
|
||||
# celery配置
|
||||
BROKER_URL = "redis://{password}{host}:{port}/{db}".format(password=_redis_password(),
|
||||
host=REDIS_LOCAL_QUEUE["host"],
|
||||
port=REDIS_LOCAL_QUEUE["port"],
|
||||
db=REDIS_LOCAL_QUEUE["db"])
|
||||
CELERY_RESULT_BACKEND = "redis"
|
||||
|
||||
CELERY_ROUTES = {
|
||||
'server.tasks.submit_dispatcher': {'queue': 'local'},
|
||||
'server.tasks.submit_waiting_submission': {'queue': 'local'},
|
||||
|
67
robots/tasks.py
Normal file
67
robots/tasks.py
Normal file
@ -0,0 +1,67 @@
|
||||
import time
|
||||
import os
|
||||
import redis
|
||||
|
||||
from celery import Celery
|
||||
from .utils import Result
|
||||
|
||||
if os.environ.get("robot_env", "local") == "local":
|
||||
REDIS_ROBOT_QUEUE = {
|
||||
"host": "127.0.0.1",
|
||||
"port": 6379,
|
||||
"db": 3
|
||||
}
|
||||
else:
|
||||
REDIS_ROBOT_QUEUE = {
|
||||
"host": os.environ["REDIS_PORT_6379_TCP_ADDR"],
|
||||
"port": 6379,
|
||||
"db": 3,
|
||||
"password": os.environ["REDIS_PASSWORD"]
|
||||
}
|
||||
|
||||
|
||||
def _redis_password():
|
||||
password = REDIS_ROBOT_QUEUE.get("password")
|
||||
if password:
|
||||
return password + "@"
|
||||
else:
|
||||
return ""
|
||||
|
||||
|
||||
app = Celery('tasks', broker="redis://{password}{host}:{port}/{db}".format(password=_redis_password(),
|
||||
host=REDIS_ROBOT_QUEUE["host"],
|
||||
port=REDIS_ROBOT_QUEUE["port"],
|
||||
db=REDIS_ROBOT_QUEUE["db"]))
|
||||
|
||||
|
||||
# remote robot task
|
||||
@app.task
|
||||
def get_problem(robot, url):
|
||||
return robot.get_problem(url)
|
||||
|
||||
|
||||
# remote robot task
|
||||
@app.task
|
||||
def submit(robot, robot_user, submit_url, origin_id, language, code):
|
||||
try:
|
||||
origin_submission_id = robot.submit(submit_url, language, code, origin_id)
|
||||
except Exception as e:
|
||||
return {"result": Result.system_error, "cpu_time": None, "memory": None,
|
||||
"info": {"error": "Failed to submit problem, error: " + str(e)},
|
||||
"origin_submission_id": None}
|
||||
# 2秒后执行
|
||||
time.sleep(2)
|
||||
retries = 20
|
||||
while retries:
|
||||
try:
|
||||
result = robot.get_result(origin_submission_id, robot_user.username)
|
||||
except Exception as e:
|
||||
return {"result": Result.system_error, "cpu_time": None, "memory": None,
|
||||
"info": {"error": "Failed to get submission result, error: " + str(e)},
|
||||
"origin_submission_id": origin_submission_id}
|
||||
if result["result"] != Result.waiting:
|
||||
result["origin_submission_id"] = origin_submission_id
|
||||
return result
|
||||
retries -= 1
|
||||
return {"result": Result.system_error, "cpu_time": None, "memory": None, "info": {"error": "Too many retires"},
|
||||
"origin_submission_id": origin_submission_id}
|
@ -4,43 +4,10 @@ import time
|
||||
import json
|
||||
|
||||
from openvj import celery_app
|
||||
from robots.utils import Result
|
||||
from robots.tasks import submit, get_problem
|
||||
from .models import RobotUserStatus, SubmissionStatus, SubmissionWaitingQueue
|
||||
|
||||
|
||||
# remote robot task
|
||||
@celery_app.task
|
||||
def get_problem(robot, url):
|
||||
return robot.get_problem(url)
|
||||
|
||||
|
||||
# remote robot task
|
||||
@celery_app.task
|
||||
def submit(robot, robot_user, submit_url, origin_id, language, code):
|
||||
try:
|
||||
origin_submission_id = robot.submit(submit_url, language, code, origin_id)
|
||||
except Exception as e:
|
||||
return {"result": Result.system_error, "cpu_time": None, "memory": None,
|
||||
"info": {"error": "Failed to submit problem, error: " + str(e)},
|
||||
"origin_submission_id": None}
|
||||
# 2秒后执行
|
||||
time.sleep(2)
|
||||
retries = 20
|
||||
while retries:
|
||||
try:
|
||||
result = robot.get_result(origin_submission_id, robot_user.username)
|
||||
except Exception as e:
|
||||
return {"result": Result.system_error, "cpu_time": None, "memory": None,
|
||||
"info": {"error": "Failed to get submission result, error: " + str(e)},
|
||||
"origin_submission_id": origin_submission_id}
|
||||
if result["result"] != Result.waiting:
|
||||
result["origin_submission_id"] = origin_submission_id
|
||||
return result
|
||||
retries -= 1
|
||||
return {"result": Result.system_error, "cpu_time": None, "memory": None, "info": {"error": "Too many retires"},
|
||||
"origin_submission_id": origin_submission_id}
|
||||
|
||||
|
||||
def release_robot_user(robot_user):
|
||||
robot_user.status = RobotUserStatus.free
|
||||
robot_user.save(update_fields=["status"])
|
||||
|
Loading…
Reference in New Issue
Block a user