Make scheduled tasks discoverable by hosting account
Coulobre Panel CI / Checks (Python 3.12) (pull_request) Canceled after 0s
Coulobre Panel CI / Checks (Python 3.14) (pull_request) Canceled after 0s

This commit is contained in:
parkel committed 2026-12-01 09:32:56 +01:00
1 parent c36cbb6c15
commit 400a0c539c
11 files changed
+254 -74

No files matched your search

+12 -6
View File
@@ -1450,16 +1450,22 @@ sudo systemctl enable --now coulobre-website-job-worker.service
sudo systemctl restart coulobre-panel coulobre-worker coulobre-beat
```
Set **Website job limit** to the number of saved jobs allowed per hosting
account. Paused jobs count toward the saved limit. The default is zero.
Set **Scheduled tasks per hosting account** to the number of saved tasks
allowed per account. Paused tasks count toward the limit. The default is zero.
If you lower a limit, the oldest enabled jobs retain their places until
the account removes or pauses jobs. Add `website-jobs` to the local service
health list if you monitor this worker from the panel.
Customers can open **Scheduled jobs** from the
account page. Operators need both **Can view hosting account** and **Can
manage scheduled website jobs**. Pick the primary or an active independent
website. Use five cron fields in UTC. For example, `*/15 * * * *` runs every
Customers can open **Advanced Features → Scheduled Tasks** and select a
hosting account. **Add task** is available when the account has an active
website and room in its hosting package. The page explains what to check
when adding tasks is unavailable. Operators and managers can open **System →
Scheduled Task Overview** to inspect tasks in their workspace. Operators
can manage tasks for accounts without an assigned customer when they have
both **Can view hosting account** and **Can manage scheduled website jobs**.
When adding a task, pick the primary or an active independent website.
Use five cron fields in UTC. For example, `*/15 * * * *` runs every
15 minutes. Runs must be at least five minutes apart. When both day fields
are restricted, the date must match both.
@@ -96,7 +96,7 @@
<a class="button secondary" href="{% url 'panel:website_files' account.pk %}">Website files</a>
{% endif %}
{% if manage_website_jobs %}
<a class="button secondary" href="{% url 'panel:website_jobs' account.pk %}">Scheduled jobs</a>
<a class="button secondary" href="{% url 'panel:website_jobs' account.pk %}">Scheduled Tasks</a>
{% endif %}
{% if manage_sftp %}
<a class="button secondary" href="{% url 'panel:sftp_keys' account.pk %}">SFTP access</a>
@@ -13,10 +13,18 @@
{% empty %}<tr><td colspan="6">No hosting accounts are available.</td></tr>{% endfor %}</tbody>
</table></div>
{% elif section == 'tasks' %}
{% if manageable_accounts %}
<div class="section-text">
<h2>Manage tasks by hosting account</h2>
<ul class="workspace-list">
{% for account in manageable_accounts.values %}<li><a href="{% url 'panel:website_jobs' account.pk %}">{{ account.primary_domain }}</a></li>{% endfor %}
</ul>
</div>
{% endif %}
<div class="table-scroll"><table>
<thead><tr><th scope="col">Domain</th><th scope="col">Task</th><th scope="col">Status</th><th scope="col">Next run (UTC)</th></tr></thead>
<tbody>{% for job in tasks %}
<tr><td>{{ job.domain }}</td><th scope="row">{% if target %}<a href="{% url target job.account_id %}">{{ job.name }}</a>{% else %}{{ job.name }}{% endif %}</th><td>{{ job.enabled|yesno:"Enabled,Paused" }}</td><td>{{ job.next_run_at|date:"j M Y H:i"|default:"—" }}</td></tr>
<tr><td>{{ job.domain }}</td><th scope="row">{% if job.account_id in manageable_accounts %}<a href="{% url 'panel:website_jobs' job.account_id %}">{{ job.name }}</a>{% else %}{{ job.name }}{% endif %}</th><td>{{ job.enabled|yesno:"Enabled,Paused" }}</td><td>{{ job.next_run_at|date:"j M Y H:i"|default:"—" }}</td></tr>
{% empty %}<tr><td colspan="4">No scheduled tasks for these accounts.</td></tr>{% endfor %}</tbody>
</table></div>
{% else %}
@@ -1,15 +1,16 @@
{% extends "hosting/workspace_page.html" %}
{% block title %}{% if job %}Edit{% else %}Add{% endif %} website job · Coulobre Panel{% endblock %}
{% block title %}{% if job %}Edit{% else %}Add{% endif %} task · Coulobre Panel{% endblock %}
{% block workspace_selector %}{% endblock %}
{% block page_header %}
<header class="page-heading">
<div>
<h1>{% if job %}Edit{% else %}Add{% endif %} website job</h1>
{% if job %}<p class="heading-meta domain">{{ job.domain }}</p>{% endif %}
<h1>{% if job %}Edit{% else %}Add{% endif %} task</h1>
<p class="heading-meta domain">{{ job.domain|default:account.primary_domain }}</p>
</div>
</header>
{% endblock %}
{% block page_context %}
<a class="quiet-link" href="{{ list_url }}">← Scheduled website jobs</a>
<a class="quiet-link" href="{{ list_url }}">← Scheduled Tasks</a>
{% endblock %}
{% block page_content %}
<section class="surface editor-surface">
@@ -17,9 +18,9 @@
{% csrf_token %}
{{ form.as_div }}
<div class="form-actions">
<button class="button primary" type="submit">Save job</button>
<button class="button primary" type="submit">Save task</button>
<a class="button secondary" href="{{ list_url }}">Cancel</a>
{% if job %}<button class="button secondary" name="action" value="remove" type="submit" formnovalidate>Remove job</button>{% endif %}
{% if job %}<button class="button secondary" name="action" value="remove" type="submit" formnovalidate>Remove task</button>{% endif %}
</div>
</form>
</section>
@@ -1,31 +1,54 @@
{% extends "hosting/workspace_page.html" %}
{% block title %}Scheduled website jobs · Coulobre Panel{% endblock %}
{% block page_header %}
<header class="page-heading">
<div>
<h1>Scheduled website jobs</h1>
<p class="heading-meta">{{ rows|length }} of {{ limit|default_if_none:"Unlimited" }} saved jobs</p>
</div>
{% if add_job %}
<a class="button primary" href="{% url 'panel:website_job_create' account.pk %}">Add job</a>
{% block title %}Scheduled Tasks · Coulobre Panel{% endblock %}
{% block page_title %}Scheduled Tasks{% endblock %}
{% block page_metadata %}
{% if account %}
<p class="heading-meta"><span class="domain">{{ account.primary_domain }}</span> · {{ rows|length }} saved · {% if limit is None %}Unlimited{% else %}{{ limit }} allowed{% endif %}</p>
{% endif %}
</header>
{% endblock %}
{% block page_actions %}
{% if account and not creation_blocker %}
<a class="button primary" href="{% url 'panel:website_job_create' account.pk %}">Add task</a>
{% endif %}
{% endblock %}
{% block workspace_selector %}
{% include "hosting/workspace_account_selector.html" with account_workspace='tasks' workspace_selected=account.pk %}
{% endblock %}
{% block page_context %}
<a class="quiet-link" href="{{ account.get_absolute_url }}">← {{ account.name }}</a>
{% if account %}<a class="quiet-link" href="{{ account.get_absolute_url }}">← {{ account.name }}</a>{% endif %}
{% endblock %}
{% block page_content %}
<section class="surface" aria-label="Scheduled website jobs">
{% if creation_blocker %}
<p class="notice">
{% if creation_blocker == 'service' %}
Scheduled tasks are disabled on this server.
{% if request.user.is_superuser %}<a href="{% url 'panel:operator_settings' 'server' %}">Open Operator Settings</a>{% else %}Ask your hosting provider to enable them.{% endif %}
{% elif creation_blocker == 'suspended' %}
Hosting services are suspended. Resume them before adding a task.
{% elif creation_blocker == 'no-package' %}
This account has no hosting package.
{% if is_customer %}Ask your hosting provider to assign a package that allows scheduled tasks.{% else %}Assign a package that allows scheduled tasks.{% endif %}
{% elif creation_blocker == 'disabled-by-package' %}
This hosting package has a scheduled task limit of zero.
{% if is_customer %}Ask your hosting provider to increase the limit.{% else %}Increase the limit or assign a package that allows tasks.{% endif %}
{% elif creation_blocker == 'no-website' %}
An active website is required. <a href="{{ account.get_absolute_url }}#websites">Review your websites</a>.
{% elif creation_blocker == 'limit' %}
This account has reached its scheduled task limit. Remove a saved task or {% if is_customer %}ask your hosting provider to increase the limit{% else %}increase the hosting package limit{% endif %}.
{% endif %}
</p>
{% endif %}
<section class="surface" aria-label="Scheduled tasks">
{% if rows %}
<div class="table-scroll">
<table>
<thead>
<tr><th scope="col">Job</th><th scope="col">Website</th><th scope="col">Schedule (UTC)</th><th scope="col">Next run</th><th scope="col">Last result</th></tr>
<tr><th scope="col">Task</th><th scope="col">Website</th><th scope="col">Schedule (UTC)</th><th scope="col">Next run</th><th scope="col">Last result</th></tr>
</thead>
<tbody>
{% for job, available in rows %}
<tr>
<th scope="row"><a class="record-link" href="{% url 'panel:website_job_edit' account.pk job.pk %}">{{ job.name }}</a></th>
<th scope="row">{% if service_available %}<a class="record-link" href="{% url 'panel:website_job_edit' account.pk job.pk %}">{{ job.name }}</a>{% else %}{{ job.name }}{% endif %}</th>
<td class="domain">{{ job.domain }}</td>
<td><code>{{ job.expression }}</code></td>
<td>
@@ -42,8 +65,10 @@
</tbody>
</table>
</div>
{% elif account %}
<p class="empty">No tasks have been scheduled for this account.</p>
{% else %}
<p class="empty">No website jobs scheduled.</p>
<p class="empty">No hosting accounts are available. Contact your hosting provider to get started.</p>
{% endif %}
</section>
{% endblock %}
@@ -1,5 +1,5 @@
{% if workspace_accounts %}
<form class="workspace-context form-actions" method="get" action="{% url 'panel:customer_workspace' 'overview' %}">
<form class="workspace-context form-actions" method="get" action="{% url 'panel:customer_workspace' account_workspace|default:'overview' %}">
<label for="workspace-account">Hosting account</label>
<select id="workspace-account" name="account">
{% for account in workspace_accounts %}
@@ -16,7 +16,7 @@
</header>
{% endblock %}
{% block page_tabs %}{% endblock %}
{% include "hosting/workspace_account_selector.html" %}
{% block workspace_selector %}{% include "hosting/workspace_account_selector.html" %}{% endblock %}
{% block page_context %}{% endblock %}
</div>
{% if not request.headers.x_coulobre_status %}
+121 -2
View File
@@ -20,6 +20,7 @@ from .models import (
HostingAccount,
HostingPlan,
IndependentWebsite,
Reseller,
WebActivation,
WebProvisioningPlan,
WebsiteDomain,
@@ -178,7 +179,26 @@ class WebsiteJobWorkflowTests(TestCase):
def test_customer_can_add_pause_and_remove_a_job_for_its_own_site(self):
self.assertContains(
self.client.get(self.account.get_absolute_url()), "Scheduled jobs"
self.client.get(self.account.get_absolute_url()), "Scheduled Tasks"
)
legacy = reverse("panel:system_workspace", args=["tasks"])
workspace = reverse("panel:customer_workspace", args=["tasks"])
self.assertRedirects(
self.client.get(legacy), workspace, fetch_redirect_response=False
)
self.assertRedirects(self.client.get(workspace), self.jobs_url)
response = self.client.get(self.jobs_url)
task_links = [
(group, url)
for group, links in response.context["navigation_groups"]
for url, label, _ in links
if label in {"Scheduled Tasks", "Scheduled Task Overview"}
]
self.assertEqual(task_links, [("Advanced Features", workspace)])
self.assertContains(response, f'href="{self.add_url}"')
self.assertContains(response, "No tasks have been scheduled")
self.assertContains(
self.client.get(self.add_url), "<h1>Add task</h1>", html=True
)
with patch(CLOCK, return_value=NOW):
self.assertRedirects(
@@ -222,9 +242,73 @@ class WebsiteJobWorkflowTests(TestCase):
)
self.assertFalse(WebsiteScheduledJob.objects.exists())
def test_task_selector_keeps_the_selected_account_in_scheduled_tasks(self):
second = HostingAccount.objects.create(
name="Second site account",
primary_domain="second.test",
customer=self.account.customer,
)
workspace = reverse("panel:customer_workspace", args=["tasks"])
second_url = reverse("panel:website_jobs", args=[second.pk])
self.assertRedirects(
self.client.get(workspace, {"account": second.pk}), second_url
)
response = self.client.get(second_url)
self.assertContains(response, f'action="{workspace}"')
self.assertContains(
response,
f'<option value="{second.pk}" selected>second.test</option>',
html=True,
)
self.assertContains(response, "This account has no hosting package")
self.assertNotContains(response, "Add task")
self.assertEqual(
self.client.get(
workspace, {"account": self.foreign.pk}
).status_code,
404,
)
def test_no_accounts_and_unavailable_tasks_have_explanations(self):
self.client.force_login(
Customer.objects.create(
user=get_user_model().objects.create_user("new-customer")
).user
)
workspace = reverse("panel:customer_workspace", args=["tasks"])
self.assertContains(
self.client.get(workspace), "No hosting accounts are available"
)
self.client.force_login(self.user)
with self.settings(WEBSITE_JOBS=False):
response = self.client.get(self.jobs_url)
self.assertContains(
response, "Scheduled tasks are disabled on this server"
)
self.assertNotContains(response, "Add task")
self.assertEqual(self.client.get(self.add_url).status_code, 404)
HostingPlan.objects.filter(pk=self.plan.pk).update(website_job_limit=0)
response = self.client.get(self.jobs_url)
self.assertContains(response, "scheduled task limit of zero")
self.assertNotContains(response, "Add task")
HostingPlan.objects.filter(pk=self.plan.pk).update(website_job_limit=2)
WebActivation.objects.all().delete()
IndependentWebsite.objects.filter(pk=self.site.pk).update(
activated=False
)
response = self.client.get(self.jobs_url)
self.assertContains(response, "An active website is required")
self.assertNotContains(response, "Add task")
def test_holding_paused_jobs_counts_toward_the_plan_limit(self):
self.job()
self.job(enabled=False, next_run_at=None)
response = self.client.get(self.jobs_url)
self.assertContains(response, "has reached its scheduled task limit")
self.assertNotContains(response, "Add task")
self.assertContains(
self.client.post(self.add_url, self.form()),
"has reached its scheduled job limit",
@@ -254,14 +338,49 @@ class WebsiteJobWorkflowTests(TestCase):
Permission.objects.get(codename="view_hostingaccount")
)
self.assertEqual(self.client.get(self.jobs_url).status_code, 403)
missing = reverse("panel:website_jobs", args=[self.foreign.pk + 100])
self.assertEqual(self.client.get(missing).status_code, 403)
self.operator.user_permissions.add(
Permission.objects.get(codename="manage_website_jobs")
)
self.assertEqual(self.client.get(self.jobs_url).status_code, 403)
HostingAccount.objects.filter(pk=self.account.pk).update(customer=None)
self.assertEqual(self.client.get(self.jobs_url).status_code, 200)
overview = reverse("panel:system_workspace", args=["tasks"])
self.assertContains(
self.client.get(overview), f'href="{self.jobs_url}"'
)
with self.settings(WEBSITE_JOBS=False):
self.assertEqual(self.client.get(self.jobs_url).status_code, 404)
self.assertContains(
self.client.get(self.jobs_url),
"Scheduled tasks are disabled on this server",
)
self.assertEqual(self.client.get(self.add_url).status_code, 404)
def test_manager_overview_remains_read_only_and_scoped(self):
manager = Reseller.objects.create(
user=get_user_model().objects.create_user("manager")
)
HostingAccount.objects.filter(pk=self.account.pk).update(
reseller=manager
)
self.job(name="Visible task")
WebsiteScheduledJob.objects.create(
account=self.foreign,
domain=self.foreign.primary_domain,
name="Foreign task",
command="true",
expression="*/15 * * * *",
)
self.client.force_login(manager.user)
overview = reverse("panel:system_workspace", args=["tasks"])
response = self.client.get(overview)
self.assertContains(response, "Scheduled Task Overview")
self.assertContains(response, "Visible task")
self.assertNotContains(response, "Foreign task")
self.assertNotContains(response, f'href="{self.jobs_url}"')
self.assertEqual(self.client.get(self.add_url).status_code, 403)
def test_due_run_is_dispatched_and_only_claimed_once(self):
job = self.job()
+3 -7
View File
@@ -181,8 +181,7 @@ from .website_aliases import (
queue_website_alias,
)
from .website_file_views import can_manage_website_files
from .website_job_views import can_manage_website_jobs
from .website_jobs import website_job_limit
from .website_job_views import can_access_website_jobs
from .workspace_onboarding import onboarding_context
VIEW_ACCOUNTS = "hosting.view_hostingaccount"
@@ -671,12 +670,9 @@ class AccountDetail(TenantScopeMixin, PermissionRequiredMixin, DetailView):
and owns_customer_operations(user, self.object)
)
context["manage_website_files"] = manage_files
context["manage_website_jobs"] = can_manage_website_jobs(
context["manage_website_jobs"] = can_access_website_jobs(
self.request
) and (
website_job_limit(self.object) != 0
or self.object.website_jobs.exists()
)
) and owns_customer_operations(user, self.object)
context["primary_file_access"] = manage_files and site_is_active(
self.object
)
+28 -16
View File
@@ -34,13 +34,9 @@ from .website_jobs import (
MANAGE_JOBS = ("hosting.view_hostingaccount", "hosting.manage_website_jobs")
def can_manage_website_jobs(request):
user = request.user
return (
panel_feature_allowed("WEBSITE_JOBS")
and getattr(request, "impersonation", None) is None
and can_manage_hosting(user, MANAGE_JOBS)
)
def can_access_website_jobs(request):
impersonating = getattr(request, "impersonation", None) is not None
return not impersonating and can_manage_hosting(request.user, MANAGE_JOBS)
class WebsiteJobForm(forms.Form):
@@ -107,13 +103,8 @@ class WebsiteJobAccountView(
customer_relation = "customer"
permission_required = MANAGE_JOBS
def dispatch(self, request, *args, **kwargs):
if not panel_feature_allowed("WEBSITE_JOBS"):
raise Http404
return super().dispatch(request, *args, **kwargs)
def has_permission(self):
return can_manage_website_jobs(
return can_access_website_jobs(
self.request
) and owns_customer_operations(self.request.user, self.account)
@@ -127,9 +118,25 @@ class WebsiteJobList(WebsiteJobAccountView):
account = self.account
jobs = list(account.website_jobs.all())
limit = website_job_limit(account)
service_available = panel_feature_allowed("WEBSITE_JOBS")
sites = (
{} if account.services_suspended else available_job_sites(account)
available_job_sites(account)
if service_available and not account.services_suspended
else {}
)
blocker = None
if not service_available:
blocker = "service"
elif account.services_suspended:
blocker = "suspended"
elif account.hosting_plan_id is None:
blocker = "no-package"
elif limit == 0:
blocker = "disabled-by-package"
elif not sites:
blocker = "no-website"
elif limit is not None and len(jobs) >= limit:
blocker = "limit"
active = {(site.site_id, site.domain) for site in sites.values()}
enabled_count = 0
rows = []
@@ -149,13 +156,18 @@ class WebsiteJobList(WebsiteJobAccountView):
"account": account,
"rows": rows,
"limit": limit,
"add_job": bool(sites)
and (limit is None or len(jobs) < limit),
"service_available": service_available,
"creation_blocker": blocker,
},
)
class WebsiteJobEditor(WebsiteJobAccountView):
def dispatch(self, request, *args, **kwargs):
if not panel_feature_allowed("WEBSITE_JOBS"):
raise Http404
return super().dispatch(request, *args, **kwargs)
@cached_property
def job(self):
if "job_pk" not in self.kwargs:
+31 -18
View File
@@ -8,11 +8,15 @@ from django.utils import timezone
from django.views.decorators.cache import never_cache
from django.views.decorators.http import require_safe
from .access import customer_for_user, reseller_for_user
from .access import (
customer_for_user,
owns_customer_operations,
reseller_for_user,
)
from .account_metrics import account_metrics
from .installation_features import panel_feature_allowed
from .models import HostingAccount, WebsiteScheduledJob
from .website_job_views import can_manage_website_jobs
from .website_job_views import can_access_website_jobs
from .workspace_onboarding import workspace_role
CUSTOMER_ACCOUNT = "coulobre_active_hosting_account"
@@ -35,7 +39,7 @@ CUSTOMER_PAGES = {
"tasks": ("Scheduled Tasks", "website_jobs", ""),
}
SYSTEM_PAGES = {
"tasks": ("Scheduled Tasks", "website_jobs"),
"tasks": ("Scheduled Task Overview", None),
"logs": ("Logs", "website_usage"),
"statistics": ("Statistics", "website_usage"),
}
@@ -131,7 +135,6 @@ def customer_pages(request):
("redirects", panel_feature_allowed("LOCAL_WEB_ACTIVATION")),
("sftp", settings.LOCAL_WEB_SFTP),
("backups", panel_feature_allowed("LOCAL_ACCOUNT_BACKUPS")),
("tasks", panel_feature_allowed("WEBSITE_JOBS")),
):
if not enabled:
pages.pop(key)
@@ -163,6 +166,8 @@ def customer_workspace(request, page):
).first()
account = account or accounts.first()
if account is None:
if page == "tasks":
return render(request, "hosting/website_jobs.html")
return redirect("panel:accounts")
if page == "overview":
return redirect(account)
@@ -194,21 +199,21 @@ def system_workspace(request, page):
raise PermissionDenied
if page not in SYSTEM_PAGES:
raise Http404
if page == "tasks" and customer_for_user(request.user) is not None:
return redirect("panel:customer_workspace", page="tasks")
title, target = SYSTEM_PAGES[page]
accounts = workspace_accounts(request.user).order_by("primary_domain")
operator = (
customer_for_user(request.user) is None
and reseller_for_user(request.user) is None
)
if page == "tasks":
may_open = can_manage_website_jobs(request)
may_view_tasks = not operator or request.user.has_perm(
"hosting.manage_website_jobs"
)
else:
may_open = settings.WEBSITE_USAGE and (
may_open = (
bool(target)
and settings.WEBSITE_USAGE
and (
not operator or request.user.has_perm("hosting.view_websiteusage")
)
)
pagination = Paginator(accounts, 25)
page_obj = pagination.get_page(request.GET.get("page"))
month = timezone.now().strftime("%Y-%m")
@@ -226,13 +231,19 @@ def system_workspace(request, page):
)
rows = [(account, measures.get(account.pk, {})) for account in page_obj]
tasks = ()
task_accounts = {account.pk for account, _ in rows}
if page == "tasks" and may_view_tasks:
tasks = (
WebsiteScheduledJob.objects.filter(account_id__in=task_accounts)
.select_related("account")
.order_by("domain", "name")[:250]
)
manageable_accounts = {}
if page == "tasks" and (
not operator or request.user.has_perm("hosting.manage_website_jobs")
):
if can_access_website_jobs(request):
manageable_accounts = {
account.pk: account
for account, _ in rows
if owns_customer_operations(request.user, account)
}
tasks = WebsiteScheduledJob.objects.filter(
account_id__in=[account.pk for account, _ in rows]
).order_by("domain", "name")[:250]
return render(
request,
"hosting/system_workspace.html",
@@ -241,6 +252,7 @@ def system_workspace(request, page):
"section": page,
"rows": rows,
"tasks": tasks,
"manageable_accounts": manageable_accounts,
"month": month,
"page_obj": page_obj,
"is_paginated": pagination.num_pages > 1,
@@ -452,6 +464,7 @@ def groups_for(request, *, customer, manager):
system = [
link("system_workspace", title, key)
for key, (title, _) in SYSTEM_PAGES.items()
if not customer or key != "tasks"
]
if user.is_superuser:
system.append(link("updates", "Updates"))