diff --git a/source/educators/concepts/exercise_tools/about_external_graders.rst b/source/educators/concepts/exercise_tools/about_external_graders.rst index 38e8a619a..cd95dd7a5 100644 --- a/source/educators/concepts/exercise_tools/about_external_graders.rst +++ b/source/educators/concepts/exercise_tools/about_external_graders.rst @@ -8,10 +8,11 @@ About External Grader Problems An external grader is a service that receives learner responses to a problem, processes those responses, and returns feedback and a problem grade to the platform. Course teams build and deploy a grader on an -external platform, such as Amazon Web Services (AWS) or Azure, and then -link their external grader to the platform using an interface called -XQueue. XQueue is a Representational State Transfer (RESTful) interface -that uses JavaScript Object Notation (JSON) objects. +external platform, such as Amazon Web Services (AWS) or Azure, and the +grader connects to the LMS through an interface called XQueue. XQueue is a +Representational State Transfer (RESTful) interface that uses JavaScript +Object Notation (JSON) objects. `xqueue-watcher`_ is the maintained grader +that implements that interface. For more information, see the following sections. @@ -24,20 +25,20 @@ For more information, see the following sections. External Grader Workflow ************************ -XQueue uses the following workflow for external grader problems. +External grader problems use the following workflow. #. In the learning management system (LMS), the learner either enters code or attaches a file for a problem, then selects **Submit**. -#. The LMS sends the learner's code to XQueue. -#. The external grader actively retrieves, or pulls, the code from XQueue. +#. The LMS places the submission in the queue named by the problem. +#. The external grader actively retrieves, or pulls, the submission from the + LMS. #. The external grader runs the tests that you created on the code. #. The external grader returns the grade for the submission, as well as any - messaging in a JSON, to XQueue. -#. The XQueue delivers the results to the LMS. + messaging in a JSON document, to the LMS. #. In the LMS, the learner sees the problem results and the grade. -For the code for the XQueue interface, see the ``urls.py`` file in the `Open -edX XQueue repository`_. +Your site operations team runs the grader and tells you the queue name to use +in your problems. For the interface, see :ref:`XQueue Reference`. .. _External Grader Example: @@ -75,6 +76,9 @@ Your site operations team :ref:`builds and deploys the external grader ``: The learner attaches and submits a code file in the unit. -* (optional) An element that contains a JSON object that you send to the - external grader. For example, you can use the ```` element to - tell the grader which tests to run for a problem. +* A ```` element that contains a JSON object. Its ``grader`` + key names the grader file, relative to the grader's root directory, that + runs for this problem. Three optional keys control xqueue-watcher's built-in + graders: ``skip_grader`` gives every submission full credit without running + the grader, ``hide_output`` hides the per-test output from the learner, and + ``lang`` picks the language of the grader's feedback. No other keys reach + the grader, and before Willow only ``grader`` does. To give problems + different tests or expected output, give each problem its own grader file. The following example shows the OLX definition of a problem that uses an external grader. @@ -77,7 +82,7 @@ external grader. print "hello world" - {"output": "hello world", "max_length": 2} + {"grader": "hello_world/grade.py"} diff --git a/source/educators/references/course_development/advanced_problem_editor.rst b/source/educators/references/course_development/advanced_problem_editor.rst index d88ba9e5e..b0aa7a0b3 100644 --- a/source/educators/references/course_development/advanced_problem_editor.rst +++ b/source/educators/references/course_development/advanced_problem_editor.rst @@ -22,8 +22,8 @@ settings to the right of the problem editor after clicking **Show advanced settings**. The Advanced Editor retains several settings from the simple editor such as -**Scoring**, **Show answer**, **Show reset option**, **Time between attempts** -and **MATLAB API Key** as well as introduces the **Randomization** setting. +**Scoring**, **Show answer**, **Show reset option** and **Time between attempts** +as well as introduces the **Randomization** setting. While the other settings are not shown on the collapsible panes to the right of the problem editor, they can be added via editing the OLX. diff --git a/source/links.txt b/source/links.txt index 9e2bbedd7..fb9ae46fc 100644 --- a/source/links.txt +++ b/source/links.txt @@ -184,7 +184,6 @@ .. _12 Principles of Multimedia Learning: http://hartford.edu/academics/faculty/fcld/data/documentation/technology/presentation/powerpoint/12_principles_multimedia.pdf -.. _Open edX XQueue repository: https://github.com/openedx/xqueue/tree/master/xqueue .. _GnuPG website: https://www.gnupg.org diff --git a/source/site_ops/how-tos/enable_xqueue.md b/source/site_ops/how-tos/enable_xqueue.md index 6a62bf162..7d26f383a 100644 --- a/source/site_ops/how-tos/enable_xqueue.md +++ b/source/site_ops/how-tos/enable_xqueue.md @@ -1,332 +1,170 @@ (Enable XQueue)= -# Enable and Test Event Bus–Based XQueue Submissions +# Enable External Grading ```{tags} -site operator, reference +site operator, how-to ``` -This guide explains how to enable and test **event bus–based XQueue submissions** in `openedx-platform`. -It is written for Open edX contributors and operators who want to validate event-driven external grading on the `master` branch. +External grader problems (`` in OLX) send learner submissions +to a grader that runs outside the LMS. The LMS queues those submissions itself, +using the [edx-submissions](https://github.com/openedx/edx-submissions) app, +and exposes the same REST API that the retired XQueue service used to. +[xqueue-watcher](https://github.com/openedx/xqueue-watcher), or any grader +that speaks the XQueue API, polls the LMS directly. No separate XQueue service +is needed. + +If you run the XQueue service today, see {ref}`Migrate from XQueue` first. ```{contents} Contents :depth: 1 :local: ``` -## Architecture Overview - -With event bus enabled, submission flow changes from direct polling to event-driven routing: - -Learner Submission - -→ LMS stores submission - -→ LMS publishes submission event - -→ Event bus distributes event - -→ XQueue worker / watcher consumes event - -→ Watcher posts result via edx-submissions API - -→ LMS updates grade +## How It Works -The watcher authenticates using a Django superuser that must belong to the `xqueue` group. - ---- +1. A learner submits a response to a `` problem. +2. The LMS stores the submission in edx-submissions, in the queue named by the + problem's `queuename` attribute. A problem without a `queuename` uses + `-` from the course key. +3. The grader logs in to the LMS as a dedicated user and polls + `/xqueue/get_submission/` for its queue. +4. The grader posts its result to `/xqueue/put_result/`. The LMS records the + score and updates the learner's problem. ## Prerequisites -* Local clone of `openedx-platform` (master branch recommended) -* Custom watcher script (e.g., {ref}`XQueue Custom Watcher`) -* Django superuser account -* Superuser added to group named `xqueue` -* Event bus enabled in LMS settings -* edx-submissions service available - ---- - -## Environment Setup - -### Option A (Using Tutor (Recommended Modern Setup)) - -Repository reference: [https://github.com/overhangio/tutor](https://github.com/overhangio/tutor) - -### Step 1. Install Tutor - -```bash -pip install tutor -``` - -Initialize: - -```bash -tutor local quickstart -``` - -Start platform: - -```bash -tutor local start -``` - ---- - -### Step 2. Create Superuser - -```bash -tutor local run lms ./manage.py createsuperuser -``` - ---- - -### Step 3. Create `xqueue` Group - -```bash -tutor local run lms ./manage.py shell -``` - -Then: - -```python -from django.contrib.auth.models import Group -Group.objects.get_or_create(name="xqueue") -``` - -Add user to group: - -```python -from django.contrib.auth import get_user_model -User = get_user_model() -user = User.objects.get(username="your_username") -user.groups.add(Group.objects.get(name="xqueue")) -``` - ---- - -### Step 4. Ensure Event Bus is Running - -Tutor uses Redis by default. - -Verify: - -```bash -tutor local ps -``` - -Redis should be listed. - ---- - -### Step 5. Enable XQueue Event Bus Waffle Flag - -Open LMS admin: - -``` -http://localhost:8000/admin -``` - -Go to: - -Waffle → Flags - -Create flag: - -Name: `enable_xqueue_event_bus` - -Active: True - -Rollout: 100 - -Optional: course override via Waffle → Overrides. - ---- - -### Option B (Using Deprecated Legacy Devstack) - -Repository reference: [https://github.com/edx/devstack](https://github.com/edx/devstack) - -### Step 1. Clone and Start Devstack - -```bash -git clone https://github.com/edx/devstack.git -cd devstack -make dev.up -``` - -This brings up LMS, CMS, MySQL, Mongo, Redis, etc. - -To rebuild after changes: +* Open edX Willow or newer. Verawood also works with the two extra steps in + {ref}`Migrate from XQueue`. Teak and Ulmo do not serve the API. +* A grader. [xqueue-watcher](https://github.com/openedx/xqueue-watcher) is the + maintained reference implementation; see its operator guide for installing + it. {ref}`XQueue Reference` describes the JSON that any other grader must + accept and return. -```bash -make dev.build -make dev.up -``` - ---- - -### Step 2. Create Superuser - -```bash -make lms-shell -``` +## Step 1: Create the Grader Account -Inside container: +The grader authenticates to the LMS with the username and password of a Django +user. That user must belong to a group named `xqueue`. It does not need to be +staff or a superuser; use a dedicated service account, not a person's login. ```bash -./manage.py createsuperuser +./manage.py lms manage_group xqueue +./manage.py lms manage_user xqueue_watcher xqueue_watcher@example.com --groups xqueue +./manage.py lms changepassword xqueue_watcher ``` ---- - -### Step 3. Create `xqueue` Group +`manage_group` creates the group if it does not exist. `manage_user` creates +the user if it does not exist and sets its groups; the group must already +exist. `changepassword` prompts for the password the grader will use. -Inside LMS shell: +`PREVENT_CONCURRENT_LOGINS` is on by default and keeps one session per user. +Each xqueue-watcher connection logs in as the same user, so they would evict +each other. Exempt the grader account in your LMS settings: ```python -from django.contrib.auth.models import Group -Group.objects.get_or_create(name="xqueue") -``` - -Add superuser to group: - -```python -from django.contrib.auth import get_user_model -User = get_user_model() -user = User.objects.get(username="your_username") -user.groups.add(Group.objects.get(name="xqueue")) -``` - ---- - -### Step 4. Enable Event Bus - -Ensure Redis container is running: - -```bash -docker ps | grep redis -``` - -If needed: - -```bash -make dev.up -``` - -Confirm event bus settings in LMS environment file. - ---- - -## Create Advanced `code_response` Problem - -In Studio, add Advanced problem with: - -```xml - - - - +SINGLE_LOGIN_EXEMPT_GROUPS = ["xqueue"] ``` -Ensure: - -* `grader="xqueue"` -* The course is published +## Step 2: Configure File Storage (File Submissions Only) ---- - -## Running the Custom Watcher - -Ensure credentials inside watcher: +For `` problems the LMS stores the uploaded file and passes +its URL to the grader in `xqueue_files`. Files go to the LMS's default +storage, `STORAGES["default"]`, unless you configure a dedicated backend: ```python -LMS_URL = "http://localhost:8000" -USERNAME = "admin" -PASSWORD = "password" -``` - -Run: - -```bash -python custom_watcher.py -``` - -Expected output: - -Connected to LMS -Polling for jobs -Graded submission with score = 1 - ---- - -## Submit as Learner - -1. Log in as learner -2. Open problem -3. Submit response -4. Wait a few seconds - -You should see score update automatically. - ---- - -## Verification and Debugging - -### Check Submissions in Django Shell - -```python -from submissions.models import Submission -Submission.objects.all() -``` - -### Check Logs - -Tutor: - -```bash -tutor local logs lms -``` - -Legacy Devstack: - -```bash -docker logs lms -``` - -### Common Issues - -403 Forbidden → User not in xqueue group - -No grading → Watcher not running - -No event → Waffle flag disabled - -Auth failure → Wrong credentials - ---- - -## Final Checklist - -* Tutor (or Devstack) running -* Superuser created -* xqueue group exists -* User added to xqueue group -* Waffle flag enabled -* Advanced problem created -* Watcher running -* Submission graded successfully - ---- +EDX_SUBMISSIONS = { + "MEDIA": { + "BACKEND": "storages.backends.s3boto3.S3Boto3Storage", + "OPTIONS": {"bucket_name": "my-submissions-bucket"}, + } +} +``` + +The grader must be able to fetch the URL that storage produces. Use a backend +that returns absolute, reachable URLs when the grader runs on another host. + +## Step 3: Point the Grader at the LMS + +In the xqueue-watcher queue configuration, set `SERVER` to the LMS root URL +(no `/xqueue` suffix) and `AUTH` to the account from Step 1. The key is the +queue name used by the problems' `queuename` attribute. + +```json +{ + "my_course_queue": { + "SERVER": "https://lms.example.com", + "AUTH": ["xqueue_watcher", "choose-a-strong-password"], + "CONNECTIONS": 2, + "HANDLERS": [ + { + "HANDLER": "xqueue_watcher.containergrader.ContainerGrader", + "KWARGS": { + "grader_root": "/graders/", + "image": "registry.example.com/my-course-grader:latest", + "backend": "docker" + } + } + ] + } +} +``` + +`ContainerGrader` runs each submission in a container built from your grader +image. See the [xqueue-watcher operator guide](https://github.com/openedx/xqueue-watcher/blob/master/docs/operators.md) +for its other options, the Kubernetes backend, `SERVER_REF` and the rest of +the configuration. + +## Step 4: Test a Problem + +1. In Studio, {ref}`add an external grader problem ` + whose `queuename` matches the watcher configuration, then publish the unit. +2. Start the watcher. Its log shows a successful login and polling. +3. Open the unit in the LMS as a learner and submit a response. The problem + reports that the submission is queued, then shows the score once the grader + replies. +4. In Django admin, open **Submissions > External grader details**. The + submission moves from `pending` to `pulled` to `retired`. + +## Troubleshooting + +* **The grader gets 403 Forbidden.** The account is not in the `xqueue` + group, or the grader is not logged in or did not send the CSRF token. See + {ref}`XQueue Endpoints`. +* **The grader logs in repeatedly.** `PREVENT_CONCURRENT_LOGINS` is evicting + its sessions. See Step 1. +* **Submissions stay `pending`.** No grader is polling that queue name. Check + the problem's `queuename` against the watcher configuration. +* **Submissions stay `pulled`.** The grader took the submission but did not + post a result the LMS accepted. A reply that is not valid JSON gets HTTP 400 + and leaves the submission `pulled`; check the grader's log. The LMS offers a + `pulled` submission to graders again once five minutes have passed. +* **Submissions end up `failed`.** The LMS could not record the grader's + score five times, for example because `score` was not a number. Fix the + grader, then use the **Reset failed submissions to pending** action in the + admin. The reset does not clear the failure count, so one more failure marks + the submission `failed` again. +* **"No grader has been set up for this problem".** The problem was tested in + Studio preview. External graders only run in the LMS. +* **"MATLAB problems can no longer be graded."** The problem uses + ``. The MATLAB integration was removed in Willow + ([DEPR: MATLAB](https://github.com/openedx/public-engineering/issues/192)). + Existing problems keep their scores but take no new submissions. + +## Related Settings + +* `XQUEUE_WAITTIME_BETWEEN_REQUESTS`: minimum seconds between two + submissions by the same learner to the same problem. Default 5. +* `EDX_SUBMISSIONS["MEDIA"]`: storage for submitted files (Step 2). +* `SINGLE_LOGIN_EXEMPT_GROUPS` and `SINGLE_LOGIN_EXEMPT_USERNAMES`: + accounts exempt from single-session enforcement (Step 1). ```{seealso} +{ref}`Migrate from XQueue` (how-to) + {ref}`XQueue Reference` (reference) +{ref}`XQueue Custom Watcher` (reference) + {ref}`About External Grader Problems` (concept) {ref}`Add an External Grader Problem` (how-to) @@ -336,4 +174,4 @@ Auth failure → Wrong credentials | Review Date | Working Group Reviewer | Release | Test Situation | |-------------|------------------------|---------|----------------| -| 2025-03-25 | Usama S |Verawood | Pass | +| | | | | diff --git a/source/site_ops/how-tos/migrate_from_xqueue.rst b/source/site_ops/how-tos/migrate_from_xqueue.rst new file mode 100644 index 000000000..299d9a7c2 --- /dev/null +++ b/source/site_ops/how-tos/migrate_from_xqueue.rst @@ -0,0 +1,222 @@ +.. _Migrate from XQueue: + +Migrate from the XQueue Service +############################### + +.. tags:: site operator, how-to + +The XQueue service is deprecated and its repository is archived +(`DEPR: XQueue`_). The LMS now queues external grader submissions itself, +through `edx-submissions`_, and serves the API that graders poll. From Willow +onward the LMS can no longer send submissions to the XQueue service or receive +its results. This guide moves a deployment that runs the XQueue service onto +the in-platform queue, on Verawood or Willow. + +On Verawood you can move one course at a time and roll back, using a course +waffle flag. That is the path in Steps 1 to 5, and it is the safer one: +migrate before you upgrade to Willow. If you are already upgrading, see +`Migrating During a Willow Upgrade`_. Teak and Ulmo do not serve the API the +grader polls, so upgrade to Verawood first or migrate during the Willow +upgrade. + +.. contents:: Contents + :local: + :depth: 1 + +What Changes and What Does Not +****************************** + +Little changes for course teams. Existing ```` problems keep +working with the same ``queuename`` values, and the JSON exchanged with the +grader keeps the same shape. The one exception is ````: on +Willow only its ``grader``, ``skip_grader``, ``hide_output`` and ``lang`` keys +reach the grader, and on Verawood only ``grader``. A problem that passed other +keys, such as an expected output or a list of tests, needs its own grader file +instead. + +Nothing changes in `xqueue-watcher`_'s code. It needs new configuration: the +LMS URL instead of the XQueue URL, and an LMS account instead of an XQueue +account. + +What goes away is the XQueue service itself: its database, its S3 bucket, the +``lms`` user inside it, and the ``XQUEUE_INTERFACE`` settings that told the LMS +where it was. Willow removes ``XQUEUE_INTERFACE`` and the ``xqueue_callback`` +URL from the LMS. + +Before You Start +**************** + +* Upgrade to Open edX Verawood or newer. +* List the queue names in use. They are the keys of your xqueue-watcher + ``conf.d`` files and the ``queuename`` attributes in course problems. +* If any problem uses ````, decide where the LMS will store + uploaded files. See Step 2 of :ref:`Enable XQueue`. +* Search course exports for ```` elements with keys other than + ``grader``, ``skip_grader``, ``hide_output`` and ``lang``. Move per-problem + configuration into a grader file per problem. See + :ref:`Add an External Grader Problem`. + +Step 1: Create the Grader Account on the LMS +******************************************** + +Follow Step 1 of :ref:`Enable XQueue`: a Django user in the ``xqueue`` group, +exempted from ``PREVENT_CONCURRENT_LOGINS``. The XQueue service's own user +database is not consulted, so reuse the username if you like, but create it +on the LMS. + +Verawood has no ``SINGLE_LOGIN_EXEMPT_GROUPS`` setting, so the account cannot +be exempted and every login evicts the account's other sessions. Give each +watcher connection its own account: set ``CONNECTIONS`` to 1 and use a +separate account for each queue entry and each watcher process. After the +Willow upgrade you can exempt the accounts and share one. + +Step 2: Enable the Flag for a Pilot Course +****************************************** + +This step and Step 4 apply to releases before Willow. Willow removed the flag. + +Turn on ``send_to_submission_course.enable`` for one course: in Django admin +under **Waffle_utils > Waffle flag course overrides**, add a row with the flag +name, the course ID and **Override choice: Force On**, and check **Enabled**. +A row without **Enabled** checked has no effect. Submissions +from that course now go to edx-submissions; every other course still goes to +the XQueue service. + +Step 3: Run a Watcher Against the LMS +************************************* + +Start a second xqueue-watcher instance, or a second queue entry, whose +``SERVER`` is the LMS root URL and whose ``AUTH`` is the account from Step 1. +Copy ``HANDLERS`` from the queue's existing entry; the graders themselves do +not change. Keep the existing watcher running against the XQueue service so it +drains whatever is already queued there. + +.. code-block:: json + + { + "my_course_queue": { + "SERVER": "https://lms.example.com", + "AUTH": ["xqueue_watcher", "choose-a-strong-password"], + "CONNECTIONS": 1, + "HANDLERS": [ + { + "HANDLER": "xqueue_watcher.containergrader.ContainerGrader", + "KWARGS": { + "grader_root": "/graders/", + "image": "registry.example.com/my-course-grader:latest", + "backend": "docker" + } + } + ] + } + } + +Submit a problem in the pilot course and confirm it is graded. **Submissions > +External grader details** in Django admin shows each submission's status. + +Step 4: Switch Every Course +*************************** + +Enable the flag for everyone: + +.. code-block:: shell + + ./manage.py lms waffle_flag send_to_submission_course.enable --everyone --create + +From this point the LMS no longer sends anything to the XQueue service. +Submissions already in XQueue are still graded by the old watcher and still +reach the LMS through the callback URL, so leave both running until the old +queues are empty. + +Step 5: Decommission XQueue +*************************** + +#. Stop the old watcher instance, or remove the old queue entries. +#. Stop the XQueue service and delete its database and bucket. +#. Remove your ``XQUEUE_INTERFACE`` override from the LMS settings. Verawood + still reads the default value, so remove the override rather than setting + it to an empty value. +#. On Verawood, leave the ``send_to_submission_course.enable`` flag on: + turning it off sends submissions back to the XQueue service. Delete it + after the Willow upgrade, which removed the flag. +#. With Tutor, disable the plugin and rebuild the configuration: + + .. code-block:: shell + + tutor plugins disable xqueue + tutor config save + +Differences to Expect +********************* + +The LMS API is compatible with xqueue-watcher, but it is not byte-for-byte +XQueue. If you run a custom grader, check these points against +:ref:`XQueue Reference`. + +* Errors return HTTP 400, 401, 403, 404, 409 or 500 with the usual + ``{"return_code": 1, "content": ...}`` body. XQueue returned 200 for + everything. +* There is no ``/xqueue/get_queuelen/`` endpoint. xqueue-watcher does not use + it. +* ``grader_payload`` reaching the grader contains the ``grader``, + ``skip_grader``, ``hide_output`` and ``lang`` keys from the problem's + ````. Other keys are not forwarded. Releases before Willow + forward only ``grader``. +* ``student_info`` has no ``random_seed``. XQueue sent the problem's seed; + xqueue-watcher does not read it. Releases before Willow send ``1``, and send + ``submission_time`` as a Unix timestamp instead of ``YYYYMMDDHHMMSS``. +* A submission a grader pulled but never answered is offered again once five + minutes have passed. XQueue used ``SUBMISSION_PROCESSING_DELAY``. +* Push (passive) graders, which XQueue called with the submission, are not + supported. The LMS only serves pull graders. +* Results reach the problem in-process. There is no callback URL, and Willow + removed the ``xqueue_callback`` view. +* ```` problems no longer queue anything. The MATLAB integration + only worked through the XQueue service and was removed in Willow + (`DEPR: MATLAB`_). The problems keep their scores, and a new submission is + refused with "MATLAB problems can no longer be graded." + +Migrating During a Willow Upgrade +********************************* + +A Willow LMS sends every course's submissions to edx-submissions as soon as +it starts. It has no callback URL, so it cannot receive results for anything +still queued in the XQueue service. + +#. Before the upgrade, let the old queues empty. Learners whose submissions + are still queued in the XQueue service when the LMS is upgraded have to + submit again. +#. During the upgrade, create the grader account (Step 1) and start a watcher + pointed at the LMS (Step 3). Skip Steps 2 and 4. +#. After the upgrade, decommission XQueue (Step 5). + +Rolling Back +************ + +Releases before Willow only. Turning the flag off for a course sends that +course's new submissions to the XQueue service again. Submissions already +queued in edx-submissions stay there until a watcher pointed at the LMS grades +them, so keep that watcher running while you roll back. + +From Willow onward there is no XQueue path to roll back to. + +.. seealso:: + + :ref:`Enable XQueue` (how-to) + + :ref:`XQueue Reference` (reference) + + :ref:`About External Grader Problems` (concept) + +.. _DEPR\: XQueue: https://github.com/openedx/public-engineering/issues/286 +.. _DEPR\: MATLAB: https://github.com/openedx/public-engineering/issues/192 +.. _edx-submissions: https://github.com/openedx/edx-submissions +.. _xqueue-watcher: https://github.com/openedx/xqueue-watcher + +**Maintenance chart** + ++--------------+-------------------------------+----------------+--------------------------------+ +| Review Date | Working Group Reviewer | Release |Test situation | ++--------------+-------------------------------+----------------+--------------------------------+ +| | | | | ++--------------+-------------------------------+----------------+--------------------------------+ diff --git a/source/site_ops/references/xqueue_custom_watcher.rst b/source/site_ops/references/xqueue_custom_watcher.rst index ce17d6986..76566f735 100644 --- a/source/site_ops/references/xqueue_custom_watcher.rst +++ b/source/site_ops/references/xqueue_custom_watcher.rst @@ -3,18 +3,21 @@ XQueue Custom Watcher Script ############################ -As part of :ref:`enabling XQueue submissions `, you'll need to -develop a custom Python script to evaluate a learner's submission. What follows -is an example of one such script. +.. tags:: site operator, reference + +`xqueue-watcher`_'s built-in graders cover most courses. If you need your own +grading logic, the smallest option is a script that uses its ``XQueueClient`` +and registers a handler. The script below accepts every submission; replace +``always_ok_handler`` with real grading. See :ref:`Enable XQueue` for the +LMS side and :ref:`XQueue Reference` for the JSON it receives and returns. .. code-block:: python ''' - Custom script to evaluate learner submissions using event bus. - Set to always return ok for a submission. + Minimal xqueue-watcher client that accepts every submission. - Note: Needs to be placed inside xqueue-watcher at root for work. + Run it from a checkout of xqueue-watcher, with its requirements installed. ''' import json import logging @@ -57,9 +60,9 @@ is an example of one such script. def main(): # Adjust to your values - QUEUE_NAME = "openedx" - XQUEUE_URL = "http://local.openedx.io:8000" - XQUEUE_AUTH = ("abdulrehman", "rehman123") + QUEUE_NAME = "my_course_queue" # the problem's queuename + XQUEUE_URL = "http://local.openedx.io:8000" # the LMS root URL + XQUEUE_AUTH = ("xqueue_watcher", "password") # a user in the xqueue group client = XQueueClient( queue_name=QUEUE_NAME, @@ -91,6 +94,7 @@ is an example of one such script. :ref:`Add an External Grader Problem` (how-to) +.. _xqueue-watcher: https://github.com/openedx/xqueue-watcher **Maintenance chart** diff --git a/source/site_ops/references/xqueue_external_grader.rst b/source/site_ops/references/xqueue_external_grader.rst index c87b92b11..a35585994 100644 --- a/source/site_ops/references/xqueue_external_grader.rst +++ b/source/site_ops/references/xqueue_external_grader.rst @@ -3,13 +3,15 @@ XQueue Reference ############################ -.. tags:: site operator, how-to +.. tags:: site operator, reference -An external grader problem requires :ref:`Enable XQueue` before it can be defined and published in Studio. +This reference describes the interface between the LMS and an external +grader: the HTTP endpoints the grader calls and the JSON exchanged. The LMS +serves this API itself; see :ref:`Enable XQueue` for setting it up. The name +"XQueue" is kept for compatibility with the retired XQueue service and with +`xqueue-watcher`_, which implements the grader side. -This reference guide details external grader requirements and technical specifications of XQueue submissions. - -.. contents:: +.. contents:: :local: :depth: 1 @@ -22,20 +24,21 @@ External Grader Requirements When you build your external grader, keep the following requirements in mind. -* To communicate with XQueue, the grader must accept and return JSON objects. - For more information, see :ref:`XQueue JSON Objects`. +* The grader must accept and return JSON objects. For more information, see + :ref:`XQueue JSON Objects`. -* The external grader must actively retrieve, or pull, submissions from the - XQueue through a RESTful interface at regular intervals. +* The grader must actively retrieve, or pull, submissions from the LMS + through the endpoints below at regular intervals. The LMS does not push + submissions to graders. * The grader must be scalable. Many learners might submit responses at one time, such as shortly before an exam is due. The grader must be able to process many submissions in a limited time, without failure or unexpected delays. -* The grader must not constantly send queries unless the grader detects that - the ``queuelen`` value is growing. If XQueue receives too many requests from - a grader, the XQueue system administrator may apply throttling to the grader. +* The grader must post a result for every submission it pulls. A submission + that is pulled and not answered is offered to graders again after five + minutes. * The grader must implement security features. @@ -46,25 +49,71 @@ When you build your external grader, keep the following requirements in mind. language that you are using and your deployment architecture. You must ensure that malicious code will not damage your server. - * You must have a plan to immediately notify the team that operates your grader, as well as your Open edX system administrator, if the grader fails. You must also have a process to quickly identify the cause of the failure, - whether the problem is with your grader or XQueue. + whether the problem is with your grader or the LMS. If you know the grader will be unavailable at a certain time for maintenance, you might consider contacting course teams so they can :ref:`add a course update `. +.. _XQueue Endpoints: + +Endpoints +========= + +All endpoints are relative to the LMS root URL. Except for ``status``, every +response body is ``{"return_code": 0, "content": ...}`` on success and +``{"return_code": 1, "content": ""}`` on failure, with an HTTP status +that matches the outcome. A request that is not logged in, comes from a user +outside the ``xqueue`` group, or fails the CSRF check gets HTTP 403. + +.. list-table:: + :header-rows: 1 + :widths: 30 10 60 + + * - Endpoint + - Method + - Purpose + * - ``/xqueue/login/`` + - GET, POST + - GET sets the CSRF cookie. POST ``username`` and ``password`` to open a + session. The user must be in the ``xqueue`` group for every other + endpoint. + * - ``/xqueue/logout/`` + - POST + - End the session. + * - ``/xqueue/get_submission/`` + - GET + - ``?queue_name=``. Returns the oldest waiting submission in that + queue and marks it pulled. ``content`` is the submission JSON below, as a + string. An empty queue returns ``return_code`` 1 with HTTP 200. + * - ``/xqueue/put_result/`` + - POST + - Post the grader response below. Requires the ``submission_key`` that + came with the submission. Returns 403 for a wrong key, 404 for an + unknown submission, 409 if another grader is posting for the same + submission. A ``return_code`` of 0 means the LMS accepted the reply, + not that it recorded the score; see `Submission Lifecycle`_. + * - ``/xqueue/status/`` + - POST + - Liveness check. The body is not JSON; check the HTTP status only. + +Mutating requests after login need the ``X-CSRFToken`` header set from the +``csrftoken`` cookie. Over HTTPS they also need a ``Referer`` header on the LMS +host, as Django's CSRF check requires. xqueue-watcher handles both. + .. _XQueue JSON Objects: XQueue JSON Objects =================== -Both the learner submission that XQueue sends to the grader, and the response -that the grader sends to XQueue, are JSON objects. +Both the learner submission that the LMS sends to the grader, and the response +that the grader sends back, are JSON objects. Each of the three top-level +values is itself a JSON-encoded string. .. note:: - XQueue does not send the learner ID to the external grader. Your grader + The LMS does not send the learner ID to the external grader. Your grader cannot access IDs or associate learner IDs with submissions. .. _XQueue Learner Submissions: @@ -72,41 +121,44 @@ that the grader sends to XQueue, are JSON objects. Learner Submissions ******************* -The grader must receive a JSON object as a learner submission. The JSON object -has the following keys. +The grader receives a JSON object with the following keys. -* ``xqueue_header``: A dictionary that the grader uses to identify the - submission when the grader returns the submission to XQueue. Do not edit this - dictionary. Return this dictionary to XQueue in the same ``xqueue_header`` - part of the JSON object. +* ``xqueue_header``: Identifies the submission. Return it unchanged in the + ``xqueue_header`` of the grader response. -* ``xqueue_files``: A dictionary of key/value pairs that contains a list of - files that the learner submitted. In this dictionary, each key is a file - name, and each value is the location of the file. + * ``submission_id``: The submission's integer ID. -* ``xqueue_body``: A dictionary that contains the actual submission as JSON. + * ``submission_key``: A secret issued when the submission was pulled. Only + the grader that holds it can post the result. - * ``student_info``: A dictionary that contains the following information - about the student in relation to this submission. +* ``xqueue_files``: A dictionary that maps each file name the learner + submitted to a URL from which the grader can download it. Empty unless the + problem uses ````. - * ``anonymous_student_id``: A string that contains an anonymized identifier - of the student. +* ``xqueue_body``: The submission. - * ``submission_time``: A string that contains a timestamp with the time of - submission in ``YYYYMMDDHHMMSS`` format. + * ``student_info``: Information about the learner in relation to this + submission. - * ``random_seed``: If a randomization script is attached to the problem, - ``random_seed`` is an integer that contains the seed that initializes the - randomization script. + * ``anonymous_student_id``: An anonymized identifier of the learner. - * ``student_response``: A string that contains the learner’s code submission. - A learner can submit code by entering a string in the LMS or by attaching a - file. XQueue stores files that learners upload in ``xqueue_files``. + * ``submission_time``: The time the learner submitted, in UTC, as a + ``YYYYMMDDHHMMSS`` string. - * ``grader_payload``: An optional string that you can specify when you create - the problem. For more information, see :ref:`Add an External Grader Problem`. + * ``student_response``: The code the learner entered in the LMS. Empty for + ```` problems; download the files from ``xqueue_files``. -An example JSON object for a learner submission follows. + * ``grader_payload``: A JSON string containing the ``grader`` key from the + problem's ```` element, plus ``skip_grader``, + ``hide_output`` and ``lang`` when the problem sets them. Other keys in the + element are not forwarded. Verawood forwards only ``grader``. For more + information, see + :ref:`Add an External Grader Problem`. + +An example learner submission follows, shown with each value decoded. On the +wire, ``xqueue_header``, ``xqueue_files`` and ``xqueue_body`` are each a +JSON-encoded string, and ``student_info`` and ``grader_payload`` inside the +body are JSON-encoded strings too. :: @@ -114,66 +166,91 @@ An example JSON object for a learner submission follows. "xqueue_header": { "submission_id": 72, "submission_key": "ffcd933556c926a307c45e0af5131995" - } + }, "xqueue_files": { - "helloworld.c": "http://download.location.com/helloworld.c" - } - "xqueue_body": - "{ + "helloworld.c": "https://storage.example.com/my_course_queue/0f1e2d3c" + }, + "xqueue_body": { "student_info": { "anonymous_student_id": "106ecd878f4148a5cabb6bbb0979b730", - "submission_time": "20160324104521", - "random_seed": 334 + "submission_time": "20240324103541" }, "student_response": "def double(x):\n return 2*x\n", - "grader_payload": "problem_2" - }" + "grader_payload": {"grader": "problem_2/grade.py"} + } } - .. _XQueue External Grader Responses: External Grader Responses ************************* After the grader runs tests and records the results for a submission, the -grader must return information by posting a JSON response. The JSON string -contains a value that indicates the following information. +grader posts a JSON response to ``/xqueue/put_result/``. It contains the +header it received and a body with the following keys. -* Whether the submission was correct. -* The score for the problem. -* Any message that the tests create. +* ``correct``: Whether the submission was correct. +* ``score``: The fraction of the problem's points to award, from 0 to 1. +* ``msg``: HTML shown to the learner with the result. Elements with the + ``longform`` class are collapsed behind **See full output**. In the following example external grader response, the learner's submission was -correct, the score was 1, and the tests created a brief message. +correct, the score was 1, and the tests created a brief message. As with the +submission, both values are sent as JSON-encoded strings. :: { - “xqueue_header”: { - Identical to what was received from the xqueue - }, - “xqueue_body”: { + "xqueue_header": { + "submission_id": 72, + "submission_key": "ffcd933556c926a307c45e0af5131995" + }, + "xqueue_body": { "correct": true, "score": 1, "msg": "

The code passed all tests.

" - } + } } +.. _XQueue Submission Lifecycle: + +Submission Lifecycle +==================== + +Each submission has a status, visible in Django admin under **Submissions > +External grader details**. + +* ``pending``: Waiting for a grader. +* ``pulled``: Handed to a grader. Offered to graders again once five minutes + have passed without a result. A reply the LMS rejects, such as one that is + not valid JSON, leaves the submission ``pulled``. +* ``retired``: Graded. The score is recorded and the learner's problem is + updated. +* ``retry``: The LMS accepted the reply but could not record the score, for + example because ``score`` was not a number. The submission is offered again. +* ``failed``: Recording the score failed five times. The **Reset failed + submissions to pending** admin action resets ``failed`` submissions to + ``pending``. It does not clear the failure count, so one more failure marks + the submission ``failed`` again. .. seealso:: - - :ref:`Enable XQueue` + + :ref:`Enable XQueue` (how-to) + + :ref:`Migrate from XQueue` (how-to) + + :ref:`XQueue Custom Watcher` (reference) :ref:`About External Grader Problems` (concept) :ref:`Add an External Grader Problem` (how-to) +.. _xqueue-watcher: https://github.com/openedx/xqueue-watcher **Maintenance chart** +--------------+-------------------------------+----------------+--------------------------------+ | Review Date | Working Group Reviewer | Release |Test situation | +--------------+-------------------------------+----------------+--------------------------------+ -| 2025-03-25 | Usama S | Verawood | Pass | -+--------------+-------------------------------+----------------+--------------------------------+ \ No newline at end of file +| | | | | ++--------------+-------------------------------+----------------+--------------------------------+