Make xqueue actually do something

Xqueue was mostly broken in the previous releases. In this version, we
address the following issues:

- Proper initialisation by creating the right users
- Shift from 8040 to the more standard 8000 port
- Expose xqueue service on the internet for accessing its API
- Properly define the xqueue name ("openedx")
- Make sure that all logs go to the console
- Add convenient CLI for using the awkward Xqueue API
- Properly document how to use Xqueue
This commit is contained in:
Régis Behmo 2020-05-05 16:30:06 +02:00
parent 721c8d3ced
commit ece1ab9983
15 changed files with 291 additions and 17 deletions

View File

@ -6,7 +6,7 @@ This is a plugin for `Tutor <https://docs.tutor.overhang.io>`_ that provides the
Installation
------------
The plugin is currently bundled with the `binary releases of Tutor <https://github.com/overhangio/tutor/releases>`_. If you have installed Tutor from source, you will have to install this plugin from source, too::
The plugin is currently bundled with the `binary releases of Tutor <https://github.com/overhangio/tutor/releases>`__. If you have installed Tutor from source, you will have to install this plugin from source, too::
pip install tutor-xqueue
@ -14,15 +14,89 @@ Then, to enable this plugin, run::
tutor plugins enable xqueue
Usage
-----
In the Open edX studio, edit a course and add a new "Advanced blank problem" ("Problem" 🠆 "Advanced" 🠆 "Advanced blank problem"). Then, click "Edit" and copy-paste the following in the editor::
```
<problem>
<coderesponse queuename="openedx">
<label>Write a program that prints "hello world".</label>
<textbox rows="10" cols="80" mode="python" tabsize="4"/>
<codeparam>
<initial_display>
# students write your program here
print ""
</initial_display>
<answer_display>
print "hello world"
</answer_display>
<grader_payload>
{"output": "hello world", "max_length": 2}
</grader_payload>
</codeparam>
</coderesponse>
</problem>
```
.. note::
The queue name must be "openedx".
Save and publish the created unit. Then, access the unit from the LMS and attempt to answer the problem. The answer is sent to the Xqueue service. If you know how to use the Xqueue API, you can access it at http(s)://xqueue.LMS_HOST (in production) or http://xqueue.localhost (in development). However, the Xqueue API is a bit awkward to use. Tutor provides a simple command-line interface to interact with the Xqueue service.
Count the number of submissions that need to be graded::
$ tutor xqueue submissions -u http://xqueue.localhost count
{
"content": 0,
"return_code": 0
}
.. note::
By default, ``tutor xqueue submissions`` will hit the Xqueue API running at http(s)://xqueue.LMS_HOST. When running locally, you will want to interact with http://xqueue.localhost. To do so, you should pass the ``--url=http://xqueue.localhost`` option to the CLI.
Show the first submission that should be graded::
$ tutor xqueue submissions show
{
"id": 1,
"key": "692c2896cdfc8bdc2d073bc3b3daf928",
"body": {
"student_info": "{\"random_seed\": 1, \"anonymous_student_id\": \"af46c9d6c05627aee45257d155ec0b79\", \"submission_time\": \"20200504101653\"}",
"grader_payload": "\n {\"output\": \"hello world\", \"max_length\": 2}\n ",
"student_response": " # students write your program here\r\n print \"42\"\r\n "
},
"return_code": 0
}
Grade the submission (in this case, mark it as being correct)::
$ tutor xqueue submissions grade 1 692c2896cdfc8bdc2d073bc3b3daf928 0.9 true "Good job\!"
{
"content": "",
"return_code": 0
}
.. warning::
When running locally, the Xqueue client will report back to the LMS at the production url, so there are great chances that it will not work. A workaround is to manually edit the Submission record stored in the Xqueue database and modify the ``lms_callback_url`` attribute, but this is for advanced users only.
The submission should then appear as correct with the message that you provided on the command line:
.. image:: https://github.com/overhangio/tutor-xqueue/raw/master/screenshots/correctanswer.png
:alt: Correct answer
:align: center
Configuration
-------------
- ``XQUEUE_AUTH_PASSWORD`` (default: ``"{{ 8|random_string }}"``)
- ``XQUEUE_MYSQL_PASSWORD`` (default: ``"{{ 8|random_string }}"``)
- ``XQUEUE_SECRET_KEY`` (default: ``"{{ 24|random_string }}"``)
- ``XQUEUE_DOCKER_IMAGE`` (default: ``"overhangio/openedx-xqueue:{{ TUTOR_VERSION }}"``)
- ``XQUEUE_AUTH_USERNAME`` (default: ``"lms"``)
- ``XQUEUE_DOCKER_IMAGE`` (default: ``"overhangio/openedx-xqueue:{{ TUTOR_VERSION }}"``)
- ``XQUEUE_HOST`` (default: ``"xqueue.{{ LMS_HOST }}"``)
- ``XQUEUE_MYSQL_PASSWORD`` (default: ``"{{ 8|random_string }}"``)
- ``XQUEUE_MYSQL_DATABASE`` (default: ``"xqueue"``
- ``XQUEUE_MYSQL_USERNAME`` (default: ``"xqueue"``)
- ``XQUEUE_SECRET_KEY`` (default: ``"{{ 24|random_string }}"``)
These values can be modified with ``tutor config save --set PARAM_NAME=VALUE`` commands.

Binary file not shown.

After

Width:  |  Height:  |  Size: 34 KiB

View File

@ -31,7 +31,7 @@ setup(
packages=find_packages(exclude=["tests*"]),
include_package_data=True,
python_requires=">=3.5",
install_requires=["tutor-openedx"],
install_requires=["tutor-openedx", "requests"],
entry_points={"tutor.plugin.v0": ["xqueue = tutorxqueue.plugin"]},
classifiers=[
"Development Status :: 3 - Alpha",

View File

@ -1 +1 @@
__version__ = "0.1.6"
__version__ = "0.2.0"

View File

@ -0,0 +1 @@
certbot certonly --standalone -n --agree-tos -m admin@{{ LMS_HOST }} -d {{ XQUEUE_HOST }}

View File

@ -18,7 +18,7 @@ spec:
- name: xqueue
image: {{ DOCKER_REGISTRY }}{{ XQUEUE_DOCKER_IMAGE }}
ports:
- containerPort: 8040
- containerPort: 8000
env:
- name: DJANGO_SETTINGS_MODULE
value: xqueue.tutor

View File

@ -0,0 +1,6 @@
- host: {{ XQUEUE_HOST }}
http:
paths:
- backend:
serviceName: nginx
servicePort: {% if ACTIVATE_HTTPS %}443{% else %}80{% endif %}

View File

@ -0,0 +1 @@
- {{ XQUEUE_HOST }}

View File

@ -6,7 +6,7 @@ metadata:
spec:
type: NodePort
ports:
- port: 8040
- port: 8000
protocol: TCP
selector:
app.kubernetes.io/name: xqueue

View File

@ -0,0 +1,37 @@
### Xqueue
upstream xqueue-backend {
server xqueue:8000 fail_timeout=0;
}
{% if ACTIVATE_HTTPS %}
server {
server_name {{ XQUEUE_HOST }};
listen 80;
return 301 https://$server_name$request_uri;
}
{% endif %}
server {
{% if ACTIVATE_HTTPS %}listen 443 {{ "" if WEB_PROXY else "ssl" }};{% else %}listen 80;{% endif %}
server_name xqueue.localhost {{ XQUEUE_HOST }};
{% if ACTIVATE_HTTPS and not WEB_PROXY %}
ssl_certificate /etc/letsencrypt/live/{{ XQUEUE_HOST }}/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/{{ XQUEUE_HOST }}/privkey.pem;
{% endif %}
# Disables server version feedback on pages and in headers
server_tokens off;
location / {
{% if not WEB_PROXY %}
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Port $server_port;
proxy_set_header X-Forwarded-For $remote_addr;
{% endif %}
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://xqueue-backend;
}
}

View File

@ -3,5 +3,5 @@ XQUEUE_INTERFACE = {
"username": "{{ XQUEUE_AUTH_USERNAME }}",
"password": "{{ XQUEUE_AUTH_PASSWORD }}"
},
"url": "http://xqueue:8040"
"url": "http://xqueue:8000"
}

View File

@ -1,7 +1,13 @@
from glob import glob
import json
import os
import click
import pkg_resources
import requests
from tutor import config as tutor_config
from tutor.exceptions import TutorError
from .__about__ import __version__
@ -14,8 +20,9 @@ config = {
},
"defaults": {
"VERSION": __version__,
"DOCKER_IMAGE": "overhangio/openedx-xqueue:{{ XQUEUE_VERSION }}",
"AUTH_USERNAME": "lms",
"DOCKER_IMAGE": "overhangio/openedx-xqueue:{{ XQUEUE_VERSION }}",
"HOST": "xqueue.{{ LMS_HOST }}",
"MYSQL_DATABASE": "xqueue",
"MYSQL_USERNAME": "xqueue",
},
@ -39,3 +46,144 @@ def patches():
content = patch_file.read()
all_patches[name] = content
return all_patches
@click.group(help="Interact with the Xqueue server")
def command():
pass
@click.group(help="List and grade submissions")
@click.pass_obj
@click.option("-q", "--queue", default="openedx", show_default=True, help="Queue name")
@click.option(
"-u",
"--url",
help="Xqueue server base url. By default, this value will be defined from the plugin configuration.",
)
def submissions(context, queue, url):
context.queue = queue
context.url = url
@click.command(name="count", help="Count submissions in queue")
@click.pass_obj
def count_submissions(context):
print_result(context, "count_submissions", context.queue)
@click.command(name="show", help="Show last submission")
@click.pass_obj
def show_submission(context):
print_result(context, "show_submission", context.queue)
@click.command(name="grade", help="Grade a specific submission")
@click.argument("submission_id")
@click.argument("submission_key")
@click.argument("grade", type=click.FLOAT)
@click.argument("correct", type=click.BOOL)
@click.argument("message")
@click.pass_obj
def grade_submission(context, submission_id, submission_key, grade, correct, message):
print_result(
context,
"grade_submission",
submission_id,
submission_key,
grade,
correct,
message,
)
def print_result(context, client_func_name, *args, **kwargs):
user_config = tutor_config.load(context.root)
client = Client(user_config, url=context.url)
func = getattr(client, client_func_name)
result = func(*args, **kwargs)
print(json.dumps(result, indent=2))
class Client:
def __init__(self, user_config, url=None):
self._session = None
self.username = user_config["XQUEUE_AUTH_USERNAME"]
self.password = user_config["XQUEUE_AUTH_PASSWORD"]
self.base_url = url
if not self.base_url:
scheme = "https" if user_config["ACTIVATE_HTTPS"] else "http"
host = host or user_config["XQUEUE_HOST"]
self.base_url = "{}://{}".format(scheme, host)
self.login()
@property
def session(self):
if self._session is None:
self._session = requests.Session()
return self._session
def url(self, endpoint):
# Don't forget to add a trailing slash to all endpoints: this is how xqueue
# works...
return self.base_url + endpoint
def login(self):
response = self.request(
"/xqueue/login/",
method="POST",
data={"username": self.username, "password": self.password},
)
message = response.get("content")
if message != "Logged in":
raise TutorError(
"Could not login to xqueue server at {}. Response: '{}'".format(
self.base_url, message
)
)
def show_submission(self, queue):
response = self.request("/xqueue/get_submission/", params={"queue_name": queue})
if response["return_code"] != 0:
return response
data = json.loads(response["content"])
header = json.loads(data["xqueue_header"])
submission_body = json.loads(data["xqueue_body"])
submission_id = header["submission_id"]
submission_key = header["submission_key"]
return {
"id": submission_id,
"key": submission_key,
"body": submission_body,
"return_code": response["return_code"],
}
def count_submissions(self, queue):
return self.request("/xqueue/get_queuelen/", params={"queue_name": queue})
def grade_submission(self, submission_id, submission_key, grade, correct, msg):
return self.request(
"/xqueue/put_result/",
method="POST",
data={
"xqueue_header": json.dumps(
{"submission_id": submission_id, "submission_key": submission_key}
),
"xqueue_body": json.dumps(
{"correct": correct, "score": grade, "msg": msg}
),
},
)
def request(self, endpoint, method="GET", data=None, params=None):
func = getattr(self.session, method.lower())
response = func(self.url(endpoint), data=data, params=params)
# TODO handle errors >= 400 and non-parsable json responses
return response.json()
submissions.add_command(count_submissions)
submissions.add_command(show_submission)
submissions.add_command(grade_submission)
command.add_command(submissions)

View File

@ -1,5 +1,11 @@
from .settings import *
ALLOWED_HOSTS = [
"{{ XQUEUE_HOST }}",
"xqueue",
"xqueue.localhost",
]
DATABASES = {
"default": {
"ENGINE": "django.db.backends.mysql",
@ -8,14 +14,14 @@ DATABASES = {
"NAME": "{{ XQUEUE_MYSQL_DATABASE }}",
"USER": "{{ XQUEUE_MYSQL_USERNAME }}",
"PASSWORD": "{{ XQUEUE_MYSQL_PASSWORD }}",
"OPTIONS": {
"init_command": "SET sql_mode='STRICT_TRANS_TABLES'",
},
"OPTIONS": {"init_command": "SET sql_mode='STRICT_TRANS_TABLES'",},
}
}
LOGGING = get_logger_config(log_dir="/openedx/data/", logging_env="tutor", dev_env=True)
LOGGING["loggers"][""]["handlers"].append("console")
SECRET_KEY = "{{ XQUEUE_SECRET_KEY }}"
XQUEUE_USERS = {"{{ XQUEUE_AUTH_USERNAME }}": "{{ XQUEUE_AUTH_PASSWORD}}"}
USERS = {"{{ XQUEUE_AUTH_USERNAME }}": "{{ XQUEUE_AUTH_PASSWORD}}"}
XQUEUES = {"openedx": None}

View File

@ -11,5 +11,5 @@ WORKDIR /openedx/xqueue
RUN pip install -r requirements.txt
EXPOSE 8040
CMD gunicorn --workers=2 --name xqueue --bind=0.0.0.0:8040 --max-requests=1000 xqueue.wsgi:application
EXPOSE 8000
CMD gunicorn --workers=2 --name xqueue --bind=0.0.0.0:8000 --max-requests=1000 xqueue.wsgi:application

View File

@ -1 +1,2 @@
./manage.py migrate
./manage.py update_users