Schedule jobs for hosted websites #104
No files matched your search
@@ -1,5 +1,9 @@
|
||||
import json
|
||||
from datetime import UTC, datetime, timedelta
|
||||
from io import BytesIO, StringIO
|
||||
from types import SimpleNamespace
|
||||
from unittest.mock import patch
|
||||
from uuid import uuid4
|
||||
|
||||
from django.contrib.auth import get_user_model
|
||||
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.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 (
|
||||
Customer,
|
||||
HostingAccount,
|
||||
@@ -17,6 +25,7 @@ from .models import (
|
||||
WebsiteDomain,
|
||||
WebsiteScheduledJob,
|
||||
)
|
||||
from .node_client import encode_manifest
|
||||
from .tasks import dispatch_website_jobs, execute_website_job
|
||||
from .website_job_schedule import next_website_run
|
||||
from .website_jobs import (
|
||||
@@ -30,6 +39,37 @@ NOW = datetime(2026, 11, 23, 10, 15, tzinfo=UTC)
|
||||
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):
|
||||
def test_utc_schedule_advances_to_the_next_matching_minute(self):
|
||||
self.assertEqual(
|
||||
|
||||
@@ -1,14 +1,13 @@
|
||||
import fcntl
|
||||
import os
|
||||
import subprocess
|
||||
import sys
|
||||
|
||||
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 .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):
|
||||
@@ -52,23 +51,11 @@ def execute_website_job(request):
|
||||
|
||||
|
||||
def serve_website_job():
|
||||
run_id = ""
|
||||
try:
|
||||
request = WebsiteJobRequest.from_manifest(
|
||||
read_header(
|
||||
sys.stdin.buffer,
|
||||
maximum=MAX_REQUEST_HEADER,
|
||||
label="scheduled website job request",
|
||||
)
|
||||
)
|
||||
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
|
||||
return serve_apply_request(
|
||||
JOB_MANIFEST_VERSION,
|
||||
WebsiteJobRequest.from_manifest,
|
||||
execute_website_job,
|
||||
limit=MAX_REQUEST_BYTES,
|
||||
identity=lambda request: {"run_id": request.run_id},
|
||||
include_result=True,
|
||||
)
|
||||
@@ -4,6 +4,7 @@ from uuid import UUID
|
||||
from .web_node import WebSite
|
||||
|
||||
JOB_SOCKET = "/run/coulobre-website-jobs.sock"
|
||||
JOB_MANIFEST_VERSION = 1
|
||||
JOB_RUNTIME = 60
|
||||
MAX_JOB_COMMAND_LENGTH = 1000
|
||||
|
||||
@@ -38,7 +39,7 @@ class WebsiteJobRequest:
|
||||
not isinstance(data, dict)
|
||||
or set(data) != {"version", "site", "run_id", "command"}
|
||||
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.")
|
||||
return cls(
|
||||
@@ -49,7 +50,7 @@ class WebsiteJobRequest:
|
||||
|
||||
def manifest(self):
|
||||
return {
|
||||
"version": 1,
|
||||
"version": JOB_MANIFEST_VERSION,
|
||||
"site": self.site.manifest(),
|
||||
"run_id": self.run_id,
|
||||
"command": self.command,
|
||||
|
||||
Reference in new issue
Block a user