Skip to content

packages/django-dramatiq-postgres: batch task purge and stagger first run - #27810

Merged
BeryJu merged 7 commits into
mainfrom
packages/django-dramatiq-postgres/batch-task-purge
Nov 19, 2026
Merged

BeryJu merged 7 commits into
mainfrom
packages/django-dramatiq-postgres/batch-task-purge

Conversation

@dewi-tik

Copy link
Copy Markdown
Contributor

Details

Batches the worker's inline task purge with delete_in_batches(). Deleting row by row blocked consumers from fetching messages and could stall the queue. The first purge now falls at a random point within task_purge_interval instead of running on boot. It isn't now() because pods that restart more often than the interval would never purge.

Backport to version-2026.8, where delete_in_batches already exists via #27634.

What does this PR change?

Why is this change needed?

How was this tested?

Linked issues


Checklist

  • The project has been linted, built, and tested (make all)
  • The documentation has been updated and formatted (make docs)
  • I have read the AI usage policy.

… run

The inline purge deleted expired tasks row by row, blocking consumer
threads from fetching messages while it ran. Use delete_in_batches()
instead.

Schedule the first purge at a random point within the interval rather
than on boot. Not now(), so pods that restart more often than the
interval still purge.
@dewi-tik dewi-tik self-assigned this Nov 13, 2026
@dewi-tik dewi-tik added area:backend backport/version-2026.8 Add this label to PRs to backport changes to version-2026.8 labels Nov 13, 2026
@netlify

netlify Bot commented Nov 13, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for authentik-docs ready!

Name Link
🔨 Latest commit afd3f6f
🔍 Latest deploy log https://app.netlify.com/projects/authentik-docs/deploys/6aff1345d62e8fbb4f6a8e7d
😎 Deploy Preview https://deploy-preview-27810--authentik-docs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@codecov

codecov Bot commented Nov 13, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.12%. Comparing base (85ca589) to head (ec7b797).
⚠️ Report is 1 commits behind head on main.
✅ All tests successful. No failed tests found.

Additional details and impacted files
@@           Coverage Diff           @@
##             main   #27810   +/-   ##
=======================================
  Coverage   93.12%   93.12%           
=======================================
  Files        1238     1238           
  Lines       86901    86986   +85     
  Branches     4894     4894           
=======================================
+ Hits        80923    81009   +86     
+ Misses       5891     5890    -1     
  Partials       87       87           
Flag Coverage Δ
conformance 30.22% <0.00%> (-0.03%) ⬇️
e2e 34.85% <0.00%> (-0.04%) ⬇️
integration 27.74% <0.00%> (-0.02%) ⬇️
rust 51.12% <ø> (ø)
unit 94.66% <100.00%> (+<0.01%) ⬆️
unit-migrate 94.66% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

@github-actions

github-actions Bot commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Playwright e2e

Download this run's HTML report and traces, then open the report:

gh run download 44569730060 -D playwright-artifacts
npx playwright show-report playwright-artifacts/playwright-report

Installation instructions

Instructions for docker-compose

Add the following block to your .env file:

AUTHENTIK_IMAGE=ghcr.io/goauthentik/dev-server
AUTHENTIK_TAG=gh-ec7b7972269bd8348563703112cc097ec11114f2
# Distroless image: AUTHENTIK_TAG=gh-ec7b7972269bd8348563703112cc097ec11114f2-distroless
AUTHENTIK_OUTPOSTS__CONTAINER_IMAGE_BASE=ghcr.io/goauthentik/dev-%(type)s:gh-%(build_hash)s

Afterwards, run the upgrade commands from the latest release notes.

Instructions for Kubernetes

Add the following block to your values.yml file:

authentik:
    outposts:
        container_image_base: ghcr.io/goauthentik/dev-%(type)s:gh-%(build_hash)s
global:
    image:
        repository: ghcr.io/goauthentik/dev-server
        tag: gh-ec7b7972269bd8348563703112cc097ec11114f2
        # Distroless image: tag: gh-ec7b7972269bd8348563703112cc097ec11114f2-distroless

Afterwards, run the upgrade commands from the latest release notes.

@dewi-tik
dewi-tik marked this pull request as ready for review November 13, 2026 12:40
@dewi-tik
dewi-tik requested a review from a team as a code owner November 13, 2026 12:40
@jsachs jsachs self-assigned this Nov 13, 2026

@jsachs jsachs left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comments in line, I think we also might need a docs change w/r/t when the first purge happens (right after boot --> random point)

@@ -654,19 +659,20 @@ def _purge_locks(self) -> None:
def _auto_purge(self) -> None:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just noting that because this still runs inline from __next__, this is just going to shorten the stall rather than fully removing it. Getting the purge out of the consumer loop could be a good future TODO (when we don't need to worry about a backport).

for task in chunked_queryset(
self.logger.info("Task purge started")
start = time.monotonic()
count = delete_in_batches(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because of @receiver(pre_delete) with no sender, Django treats every model as having delete signals and can never use a plain SQL DELETE. For each batch of 1,000, Django therefore loads the Task rows plus every related TaskLog and TaskDependency into Python objects and fires pre_delete/post_delete per row.

Registering those receivers only for the models they handle (TasksModel subclasses, and Flow/FlowStageBinding/Stage) should let Django delete with plain SQL.

Maybe a good follow-up PR if we can benchmark and validate this idea.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah the main reason for that is that the django @receiver doesn't support inheritance, so we'd probably end up with a custom @receiver_inheritance that binds the signals to cls and all subclasses of cls

test-migrations-from-stable runs the suite against the migrated
authentik database, which already holds tasks queued at app startup.
Asserting on whole tables made the test fail there.
@jsachs

jsachs commented Nov 15, 2026

Copy link
Copy Markdown
Contributor

Benchmarking looks good:

Purge (head / main) Head tasks Head time Main tasks Main time Speed-up
1 (07:42 / 08:02) 28,028 20.2 s 31,251 425 s 21×
2 (13:42 / 14:09) 47,381 38.0 s 49,180 660 s 17×
3 (19:43 / 20:20) 47,560 38.0 s 49,313 670 s 18×
4 (01:43 / 02:31) 47,495 34.2 s 49,258 675 s 20×
5 (07:44 / 08:43) 71,027 49.9 s 79,637 1,086 s 22×
6 (13:45 / 15:01) 87,392 65.8 s 92,915 1,270 s 19×

@dewi-tik dewi-tik added this to the Release 2026.8.6 milestone Nov 17, 2026
Signed-off-by: Jens Langhammer <[email protected]>
Signed-off-by: Jens Langhammer <[email protected]>
@BeryJu
BeryJu merged commit e3c8395 into main Nov 19, 2026
172 checks passed
@BeryJu
BeryJu deleted the packages/django-dramatiq-postgres/batch-task-purge branch November 19, 2026 17:07
@authentik-cherry-pick

Copy link
Copy Markdown
Contributor

⚠️ Cherry-pick to version-2026.8 has conflicts: #27927

BeryJu added a commit that referenced this pull request Nov 20, 2026
… run (cherry-pick #27810 to version-2026.8) (#27927)

* Cherry-pick #27810 to version-2026.8 (with conflicts)

This cherry-pick has conflicts that need manual resolution.

Original PR: #27810
Original commit: e3c8395

* fix conflicts

Signed-off-by: Jens Langhammer <[email protected]>

---------

Signed-off-by: Jens Langhammer <[email protected]>
Co-authored-by: Dewi Roberts <[email protected]>
Co-authored-by: Jens Langhammer <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:backend backport/version-2026.8 Add this label to PRs to backport changes to version-2026.8

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants