Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
name: CI

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]

jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Install uv
uses: astral-sh/setup-uv@v5

- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: "3.12"

- name: Run Ruff
run: |
uv pip install --system ruff
ruff check .
ruff format --check .

test:
needs: lint
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.12", "3.13", "3.14"]
django-version: ["6.0"]

steps:
- uses: actions/checkout@v4

- name: Install uv
uses: astral-sh/setup-uv@v5
with:
enable-cache: true

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}

- name: Install dependencies
run: |
uv pip install --system "django==${{ matrix.django-version }}"
uv pip install --system -e ".[test]" || uv pip install --system -e . pytest pytest-django huey

- name: Run Tests
run: PYTHONPATH=. pytest
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ Log and monitor [Huey](https://huey.readthedocs.io/en/latest/) task attempts dir

1. Install the package using [uv](https://docs.astral.sh/uv/):
```bash
uv add django-huey-log
uv add git+https://github.com/PythonUnited/django-huey-log.git
```

2. Add `huey_log` to your `INSTALLED_APPS` in `settings.py`:
```python
INSTALLED_APPS = [
# ...
"huey_log",
"django_huey_log",
# ...
]
```
Expand Down
7 changes: 4 additions & 3 deletions example/manage.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""

import os
import sys


def main():
"""Run administrative tasks."""
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'settings')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings")
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
Expand All @@ -18,5 +19,5 @@ def main():
execute_from_command_line(sys.argv)


if __name__ == '__main__':
main()
if __name__ == "__main__":
main()
9 changes: 4 additions & 5 deletions example/settings.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import os
from pathlib import Path

BASE_DIR = Path(__file__).resolve().parent.parent
Expand Down Expand Up @@ -55,7 +54,7 @@
STATIC_URL = "static/"

HUEY = {
'huey_class': 'huey.SqliteHuey',
'name': 'test-huey',
'filename': BASE_DIR / 'huey_db.sqlite3',
}
"huey_class": "huey.SqliteHuey",
"name": "test-huey",
"filename": BASE_DIR / "huey_db.sqlite3",
}
7 changes: 5 additions & 2 deletions example/tasks.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
from huey.contrib.djhuey import task, db_task
import time

from huey.contrib.djhuey import task


@task()
def success_task(name):
print(f"Hello {name}!")
time.sleep(1)
return f"Done {name}"


@task()
def failure_task():
time.sleep(0.5)
raise ValueError("This task was designed to fail!")
raise ValueError("This task was designed to fail!")
2 changes: 1 addition & 1 deletion example/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@

urlpatterns = [
path("admin/", admin.site.urls),
]
]
33 changes: 31 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ description = "Log and monitor Huey task attempts in the Django admin"
readme = "README.md"
requires-python = ">=3.10"
dependencies = [
"django>=4.2",
"django>=5.0",
"huey>=2.5.0",
]

Expand All @@ -17,4 +17,33 @@ build-backend = "hatchling.build"
packages = ["src/django_huey_log"]

[tool.hatch.build.targets.wheel.sources]
"src" = "django_huey_log"
"src" = "django_huey_log"

[project.optional-dependencies]
test = [
"pytest>=7.0",
"pytest-django>=4.5",
]
dev = [
"ruff==0.14.13",
]

[tool.pytest.ini_options]
DJANGO_SETTINGS_MODULE = "example.settings"
python_files = ["tests.py", "test_*.py"]
pythonpath = ["."]

[tool.ruff]
# Target version for the generated code
target-version = "py310"
line-length = 88

[tool.ruff.lint]
# Enable Pyflakes (F) and pycodestyle (E, W) codes by default.
# Also common: I (isort), B (flake8-bugbear), UP (pyupgrade)
select = ["E", "F", "W", "I", "B", "UP"]
ignore = []

[tool.ruff.format]
quote-style = "double"
indent-style = "space"
1 change: 1 addition & 0 deletions src/django_huey_log/admin.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django.contrib import admin

from .models import HueyTaskAttempt


Expand Down
1 change: 0 additions & 1 deletion src/django_huey_log/migrations/0001_initial.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@


class Migration(migrations.Migration):

initial = True

dependencies = []
Expand Down
6 changes: 4 additions & 2 deletions src/django_huey_log/signals.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from __future__ import annotations

import traceback as tb
from datetime import datetime, timezone as dt_timezone
from datetime import datetime
from datetime import timezone as dt_timezone

from django.utils import timezone
from huey.contrib.djhuey import HUEY
Expand Down Expand Up @@ -48,7 +49,8 @@ def _task_eta(task):

def _upsert_attempt(task, status: str, **fields):
# Single-row “current attempt” strategy keyed by task_id + started_at bucket:
# For simplicity: just create new rows for EXECUTING, then update the latest row for completion/error.
# For simplicity: just create new rows for EXECUTING, then update the latest
# row for completion/error.
task_id = str(getattr(task, "id", "") or getattr(task, "task_id", ""))

if status == HueyTaskAttempt.Status.EXECUTING:
Expand Down
Empty file.
42 changes: 42 additions & 0 deletions src/django_huey_log/tests/test_log_tasks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import pytest

from example.tasks import failure_task, success_task

from ..models import HueyTaskAttempt


@pytest.mark.django_db
def test_task_logging_success():
# Trigger the task (calling .task_id usually triggers huey logic in testing)
success_task("test-user")

# Check if an attempt was logged
attempt = HueyTaskAttempt.objects.first()
assert attempt is not None
assert attempt.task_name == "success_task"


@pytest.mark.django_db
def test_task_logging_failure():
# Trigger a failing task.
# We don't necessarily need pytest.raises if Huey handles the exception internally,
# we just need to verify that the SIGNAL_ERROR was caught and logged.
failure_task()

# Check if a failure attempt was logged
attempt = HueyTaskAttempt.objects.filter(task_name="failure_task").first()
assert attempt is not None
assert attempt.status == HueyTaskAttempt.Status.ERROR
assert attempt.exc_type == "ValueError"
assert "This task was designed to fail!" in attempt.exc_message
assert len(attempt.traceback) > 0


@pytest.mark.django_db
def test_task_logging_arguments():
# Test complex argument capturing.
# Note: 'name' is a keyword argument here, so it goes into kwargs_repr.
success_task(name={"complex": [1, 2, 3]})

attempt = HueyTaskAttempt.objects.filter(task_name="success_task").first()
assert "{'complex': [1, 2, 3]}" in attempt.kwargs_repr
Loading