Use the existing node protocol for job requests
This commit is contained in:
1 parent
30f19727c5
commit
a01f318330
3 files changed
+53
-25
No files matched your search
@@ -1,5 +1,9 @@
|
|||||||
|
import json
|
||||||
from datetime import UTC, datetime, timedelta
|
from datetime import UTC, datetime, timedelta
|
||||||
|
from io import BytesIO, StringIO
|
||||||
|
from types import SimpleNamespace
|
||||||
from unittest.mock import patch
|
from unittest.mock import patch
|
||||||
|
from uuid import uuid4
|
||||||
|
|
||||||
from django.contrib.auth import get_user_model
|
from django.contrib.auth import get_user_model
|
||||||
from django.contrib.auth.models import Permission
|
from django.contrib.auth.models import Permission
|
||||||
@@ -7,6 +11,10 @@ from django.test import SimpleTestCase, TestCase, override_settings
|
|||||||
from django.urls import reverse
|
from django.urls import reverse
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
|
|
||||||
|
from coulobre.web_node import WebSite
|
||||||
|
from coulobre.web_node_jobs import serve_website_job
|
||||||
|
from coulobre.website_job import WebsiteJobRequest
|
||||||
|
|
||||||
from .models import (
|
from .models import (
|
||||||
Customer,
|
Customer,
|
||||||
HostingAccount,
|
HostingAccount,
|
||||||
@@ -17,6 +25,7 @@ from .models import (
|
|||||||
WebsiteDomain,
|
WebsiteDomain,
|
||||||
WebsiteScheduledJob,
|
WebsiteScheduledJob,
|
||||||
)
|
)
|
||||||
|
from .node_client import encode_manifest
|
||||||
from .tasks import dispatch_website_jobs, execute_website_job
|
from .tasks import dispatch_website_jobs, execute_website_job
|
||||||
from .website_job_schedule import next_website_run
|
from .website_job_schedule import next_website_run
|
||||||
from .website_jobs import (
|
from .website_jobs import (
|
||||||
@@ -30,6 +39,37 @@ NOW = datetime(2026, 11, 23, 10, 15, tzinfo=UTC)
|
|||||||
CLOCK = "django.utils.timezone.now"
|
CLOCK = "django.utils.timezone.now"
|
||||||
|
|
||||||
|
|
||||||
|
class WebsiteJobReceiverTests(SimpleTestCase):
|
||||||
|
def test_receiver_accepts_the_client_protocol_without_echoing_commands(
|
||||||
|
self,
|
||||||
|
):
|
||||||
|
request = WebsiteJobRequest(
|
||||||
|
WebSite(42, "example.test"), uuid4().hex, "exit 7"
|
||||||
|
)
|
||||||
|
request_body = BytesIO(encode_manifest(request.manifest()))
|
||||||
|
response = StringIO()
|
||||||
|
with (
|
||||||
|
patch("sys.stdin", SimpleNamespace(buffer=request_body)),
|
||||||
|
patch("sys.stdout", response),
|
||||||
|
patch(
|
||||||
|
"coulobre.web_node_jobs.execute_website_job",
|
||||||
|
return_value={"timed_out": False, "exit_code": 7},
|
||||||
|
) as execute,
|
||||||
|
):
|
||||||
|
self.assertEqual(serve_website_job(), 0)
|
||||||
|
execute.assert_called_once_with(request)
|
||||||
|
self.assertEqual(
|
||||||
|
json.loads(response.getvalue()),
|
||||||
|
{
|
||||||
|
"run_id": request.run_id,
|
||||||
|
"state": "configured",
|
||||||
|
"problem": None,
|
||||||
|
"timed_out": False,
|
||||||
|
"exit_code": 7,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class WebsiteJobScheduleTests(SimpleTestCase):
|
class WebsiteJobScheduleTests(SimpleTestCase):
|
||||||
def test_utc_schedule_advances_to_the_next_matching_minute(self):
|
def test_utc_schedule_advances_to_the_next_matching_minute(self):
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
|
|||||||
@@ -1,14 +1,13 @@
|
|||||||
import fcntl
|
import fcntl
|
||||||
import os
|
import os
|
||||||
import subprocess
|
import subprocess
|
||||||
import sys
|
|
||||||
|
|
||||||
from . import web_node_apply as web
|
from . import web_node_apply as web
|
||||||
from .node_stream import encode_header, read_header
|
from .node_requests import serve_apply_request
|
||||||
from .web_node_access import website_suspension_path
|
from .web_node_access import website_suspension_path
|
||||||
from .website_job import JOB_RUNTIME, WebsiteJobRequest
|
from .website_job import JOB_MANIFEST_VERSION, JOB_RUNTIME, WebsiteJobRequest
|
||||||
|
|
||||||
MAX_REQUEST_HEADER = 16 * 1024
|
MAX_REQUEST_BYTES = 16 * 1024
|
||||||
|
|
||||||
|
|
||||||
def execute_website_job(request):
|
def execute_website_job(request):
|
||||||
@@ -52,23 +51,11 @@ def execute_website_job(request):
|
|||||||
|
|
||||||
|
|
||||||
def serve_website_job():
|
def serve_website_job():
|
||||||
run_id = ""
|
return serve_apply_request(
|
||||||
try:
|
JOB_MANIFEST_VERSION,
|
||||||
request = WebsiteJobRequest.from_manifest(
|
WebsiteJobRequest.from_manifest,
|
||||||
read_header(
|
execute_website_job,
|
||||||
sys.stdin.buffer,
|
limit=MAX_REQUEST_BYTES,
|
||||||
maximum=MAX_REQUEST_HEADER,
|
identity=lambda request: {"run_id": request.run_id},
|
||||||
label="scheduled website job request",
|
include_result=True,
|
||||||
)
|
)
|
||||||
)
|
|
||||||
run_id = request.run_id
|
|
||||||
result = execute_website_job(request)
|
|
||||||
response = {"state": "configured", "problem": None, **result}
|
|
||||||
except (OSError, ValueError):
|
|
||||||
response = {
|
|
||||||
"state": "failed",
|
|
||||||
"problem": "The web node could not start the website job.",
|
|
||||||
}
|
|
||||||
sys.stdout.buffer.write(encode_header({"run_id": run_id, **response}))
|
|
||||||
sys.stdout.buffer.flush()
|
|
||||||
return 0
|
|
||||||
@@ -4,6 +4,7 @@ from uuid import UUID
|
|||||||
from .web_node import WebSite
|
from .web_node import WebSite
|
||||||
|
|
||||||
JOB_SOCKET = "/run/coulobre-website-jobs.sock"
|
JOB_SOCKET = "/run/coulobre-website-jobs.sock"
|
||||||
|
JOB_MANIFEST_VERSION = 1
|
||||||
JOB_RUNTIME = 60
|
JOB_RUNTIME = 60
|
||||||
MAX_JOB_COMMAND_LENGTH = 1000
|
MAX_JOB_COMMAND_LENGTH = 1000
|
||||||
|
|
||||||
@@ -38,7 +39,7 @@ class WebsiteJobRequest:
|
|||||||
not isinstance(data, dict)
|
not isinstance(data, dict)
|
||||||
or set(data) != {"version", "site", "run_id", "command"}
|
or set(data) != {"version", "site", "run_id", "command"}
|
||||||
or type(data["version"]) is not int
|
or type(data["version"]) is not int
|
||||||
or data["version"] != 1
|
or data["version"] != JOB_MANIFEST_VERSION
|
||||||
):
|
):
|
||||||
raise ValueError("Expected a version 1 scheduled job request.")
|
raise ValueError("Expected a version 1 scheduled job request.")
|
||||||
return cls(
|
return cls(
|
||||||
@@ -49,7 +50,7 @@ class WebsiteJobRequest:
|
|||||||
|
|
||||||
def manifest(self):
|
def manifest(self):
|
||||||
return {
|
return {
|
||||||
"version": 1,
|
"version": JOB_MANIFEST_VERSION,
|
||||||
"site": self.site.manifest(),
|
"site": self.site.manifest(),
|
||||||
"run_id": self.run_id,
|
"run_id": self.run_id,
|
||||||
"command": self.command,
|
"command": self.command,
|
||||||
|
|||||||
Reference in new issue
Block a user