Skip to content

Leaner task workers with automatic memory cleanup#1

Open
d3banjan wants to merge 14 commits intoshubhamdipt:masterfrom
d3banjan:master
Open

Leaner task workers with automatic memory cleanup#1
d3banjan wants to merge 14 commits intoshubhamdipt:masterfrom
d3banjan:master

Conversation

@d3banjan
Copy link
Copy Markdown

This PR adds the following features to the django-simple-queue library --

  1. The heartbeat log now shows approximate RAM Usage (RSS: overestimates actual memory usage by double counting shared libraries)
  2. The process_task function now runs in a subprocess and cleans up after itself, by terminating the subprocess. This should ideally reload code changes without restarts and keep RAM usage from accumulating over time.
❯ ./manage.py task_worker --settings contentai.task_worker_settings
2025-03-28 16:05:29.216631+00:00: [RAM Usage: 46.08 MB] Heartbeat..
2025-03-28 16:05:38.239792+00:00: [RAM Usage: 48.65 MB] Heartbeat..
2025-03-28 16:05:47.242454+00:00: [RAM Usage: 48.65 MB] Heartbeat..
2025-03-28 16:05:56.245035+00:00: [RAM Usage: 48.65 MB] Heartbeat..
2025-03-28 16:06:05.247989+00:00: [RAM Usage: 48.65 MB] Heartbeat..
2025-03-28 16:06:14.250499+00:00: [RAM Usage: 48.65 MB] Heartbeat..
2025-03-28 16:06:23.253134+00:00: [RAM Usage: 48.65 MB] Heartbeat..
Initiating task id: cd609667-7e48-4076-97a4-9a005a4f451d
Finished task id: cd609667-7e48-4076-97a4-9a005a4f451d
2025-03-28 16:06:40.049689+00:00: [RAM Usage: 48.65 MB] Heartbeat..
2025-03-28 16:06:49.054867+00:00: [RAM Usage: 48.65 MB] Heartbeat..
2025-03-28 16:06:58.057543+00:00: [RAM Usage: 48.65 MB] Heartbeat..
2025-03-28 16:07:07.059874+00:00: [RAM Usage: 48.65 MB] Heartbeat..
Initiating task id: 012321d3-ca5c-4b58-9bb5-9e9a7d5fb8bc
Finished task id: 012321d3-ca5c-4b58-9bb5-9e9a7d5fb8bc
2025-03-28 16:07:23.364137+00:00: [RAM Usage: 48.9 MB] Heartbeat..
2025-03-28 16:07:32.368120+00:00: [RAM Usage: 48.9 MB] Heartbeat..
2025-03-28 16:07:41.370409+00:00: [RAM Usage: 49.03 MB] Heartbeat..
2025-03-28 16:07:50.372618+00:00: [RAM Usage: 49.03 MB] Heartbeat..
2025-03-28 16:07:59.375190+00:00: [RAM Usage: 49.03 MB] Heartbeat..
2025-03-28 16:08:08.379942+00:00: [RAM Usage: 49.03 MB] Heartbeat..
2025-03-28 16:08:17.382780+00:00: [RAM Usage: 49.03 MB] Heartbeat..
2025-03-28 16:08:26.385380+00:00: [RAM Usage: 49.03 MB] Heartbeat..
2025-03-28 16:08:35.388132+00:00: [RAM Usage: 49.03 MB] Heartbeat..
Initiating task id: 75a2ef65-0e87-469d-a1d4-3564373cb3df
Finished task id: 75a2ef65-0e87-469d-a1d4-3564373cb3df
2025-03-28 16:08:51.554354+00:00: [RAM Usage: 49.03 MB] Heartbeat..
2025-03-28 16:09:00.557616+00:00: [RAM Usage: 49.03 MB] Heartbeat..
2025-03-28 16:09:09.559932+00:00: [RAM Usage: 49.03 MB] Heartbeat..
  1. Details the setting up of a custom settings module for the task workers that decreases RAM Usage from 200-400MB (from systemd-cgtop)/450-550 MB ( from psutil RSS) to 20-40MB (from systemd-cgtop)/30-50 MB ( from psutil RSS) in the README.

Debanjan Basu added 3 commits March 28, 2025 17:11
Note: the parent's DB connections are closed before forking to avoid corrupting the db connections if the child process writes to them.
Debanjan Basu and others added 11 commits September 4, 2025 13:02
- Add psutil to requirements.txt (used in task_worker.py for memory
  logging but was never declared as a dependency)
- Fix TypeError in error handler when task.output is None: if an
  exception occurs before output is initialized (e.g. during module
  import), the except block crashed on `None += str`, leaving the
  task stuck in "In Progress" forever with no error output

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Set up runtests.py with in-memory SQLite and test_tasks.py with helper
callables so subsequent commits can include tests.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Move ManagedEventLoop and process_task (renamed execute_task) into
worker.py. Add signals.py with 5 lifecycle signals (stubs). Add
monitor.py with orphan detection stubs. Slim task_worker.py to
orchestration only. Include tests for execute_task.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Add three new fields to Task model: worker_pid for tracking which
process owns a task, error for storing tracebacks separately from
output, and log for captured process output. Update execute_task to
write errors to task.error instead of task.output.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Set worker_pid on task claim, clear after subprocess completes. Implement
detect_orphaned_tasks to find PROGRESS tasks with dead PIDs and mark
them FAILED. Add handle_subprocess_exit to catch non-zero exit codes.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Emit before_job, on_success, on_failure, before_loop, and after_loop
signals at appropriate points in the task execution flow. Generator
tasks fire before_loop/after_loop per iteration.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Capture stdout, stderr, and Python logging output from the child process
via os.pipe(). Parent creates pipe before fork, drains it in a background
thread during p.join(), then stores the captured log in task.log.

The worker.py execute_task() redirects sys.stdout, sys.stderr, and root
logger to the pipe fd when log_fd is provided.

Also update runtests.py to use file-based SQLite with matching TEST name
so subprocess tests can access the same database.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
- Fix XSS: replace inline HTML with Django template (auto-escaping)
- Fix bare except clauses with specific exception types
- Replace mark_safe with format_html in admin
- Add optional task allowlist (DJANGO_SIMPLE_QUEUE_ALLOWED_TASKS)
- Add task execution timeout (DJANGO_SIMPLE_QUEUE_TASK_TIMEOUT)
- Add conf.py settings module
- Add security and timeout tests

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Set up MkDocs Material with mkdocstrings to generate documentation from
Google-style docstrings. Adds comprehensive docstrings to all modules
(models, signals, monitor, views, worker, admin), 17 documentation pages
covering getting started, guides, API reference, and advanced topics,
plus a GitHub Actions workflow for automatic GitHub Pages deployment.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant