Skip to main content

Lightweight async task execution framework for Django

Project description

django-simpletask5

本项目由 opencode + deepseek-v4-flash 生成

一个轻量级的 Django 异步任务执行框架,提供声明式的任务模型、信号驱动的自动发布、Worker 进程异步执行,以及内置的 Cron 定时调度。

特性

  • 声明式任务模型 — 继承 Task 模型即可定义任务,自动处理创建/更新/删除事件
  • 信号驱动 — Django 信号自动拦截模型变更,发布 TaskExecution 到消息队列
  • 自定义事件 — 通过 task.trigger('event_name') 触发任意事件
  • 灵活的执行器映射 — 不同事件可绑定不同的执行器类
  • 队列路由 — 不同事件可路由到不同优先级队列
  • 重试与超时 — 失败自动重试(指数退避),支持超时检测
  • 加密字段 — 敏感数据自动加密存储
  • Cron 调度 — 内置 crontab 守护进程,支持代码注册与数据库覆盖
  • 归档统计 — 已完成执行记录自动归档为加密 JSONL,并生成日统计
  • Worker 注册中心 — 基于 Redis 的 Worker 心跳与状态追踪
  • Django Admin 集成 — 完整的后台管理界面

依赖

  • Python >= 3.8
  • Django >= 3.2(兼容至 5.2.x)
  • Kombu >= 5.3.0(消息队列,支持 RabbitMQ/Redis/内存)
  • Redis >= 4.0.0(分布式锁、Worker 注册中心)
  • 详见 pyproject.toml

安装

pip install django-simpletask5

django-simpletask5 使用 django-app-requires 自动管理依赖的 app(如 django_safe_fields),无需手动添加到 INSTALLED_APPS。只需在 settings.py 中调用一次 patch:

from django_app_requires import patch_all as django_app_requires_patch_all
django_app_requires_patch_all()

然后在 INSTALLED_APPS 中添加:

INSTALLED_APPS = [
    ...
    'django_simpletask5',
]

配置分布式锁:

DJANGO_SIMPLETASK_LOCK_CONFIG = {
    'global_lock_engine_class': 'globallock.redis_global_lock.RedisGlobalLock',
    'global_lock_engine_options': {
        'host': '127.0.0.1',
        'port': 6379,
        'db': 0,
    },
}

运行迁移:

python manage.py migrate django_simpletask5

配置说明

所有配置项均为可选,设有合理的默认值。以下按功能分类说明。

消息队列(MQ)

默认使用内存队列(memory://),适合开发调试。生产环境建议切换为 RabbitMQ 或 Redis。

# 方式一:直接指定完整 Broker URL(优先级最高)
DJANGO_SIMPLETASK_BROKER_URL = 'amqp://guest:guest@127.0.0.1:5672//'

# 方式二:分别指定类型和连接参数
DJANGO_SIMPLETASK_MESSAGE_QUEUE_TYPE = 'rabbitmq'  # 'memory' | 'rabbitmq' | 'redis'
DJANGO_SIMPLETASK_RABBITMQ_HOST = '127.0.0.1'
DJANGO_SIMPLETASK_RABBITMQ_PORT = 5672
DJANGO_SIMPLETASK_RABBITMQ_USER = 'guest'
DJANGO_SIMPLETASK_RABBITMQ_PASSWORD = 'guest'

# Redis 作为 MQ 时:
# DJANGO_SIMPLETASK_MESSAGE_QUEUE_TYPE = 'redis'
# DJANGO_SIMPLETASK_REDIS_MQ_HOST = '127.0.0.1'
# DJANGO_SIMPLETASK_REDIS_MQ_PORT = 6379
# DJANGO_SIMPLETASK_REDIS_MQ_DB = 0

分布式锁

DJANGO_SIMPLETASK_LOCK_CONFIG = {
    'global_lock_engine_class': 'globallock.redis_global_lock.RedisGlobalLock',
    'global_lock_engine_options': {
        'host': '127.0.0.1',
        'port': 6379,
        'db': 0,
    },
}
DJANGO_SIMPLETASK_LOCK_TIMEOUT = 600  # 锁超时时间(秒)

队列路由

DJANGO_SIMPLETASK_DEFAULT_QUEUE = 'django_simpletask5.queue.default'          # 默认队列
DJANGO_SIMPLETASK_HIGH_PRIORITY_QUEUE = 'django_simpletask5.queue.high_priority'  # 高优先级队列

执行与重试

DJANGO_SIMPLETASK_DEFAULT_MAX_RETRIES = 3        # 默认最大重试次数
DJANGO_SIMPLETASK_DEFAULT_TIMEOUT_SECONDS = 600  # 默认执行超时时间(秒)

归档

DJANGO_SIMPLETASK_ARCHIVE_PATH = 'django_simpletask5_archives'  # 归档文件存储子目录
DJANGO_SIMPLETASK_ARCHIVE_SALT = 'django-simpletask5-archive'   # 归档加密盐值(结合 SECRET_KEY 派生 AES 密钥)
DJANGO_SIMPLETASK_ARCHIVE_RETENTION_DAYS = 7                    # 归档保留天数

Cron

DJANGO_SIMPLETASK_CRONJOB_AUTO_SYNC = True  # 是否自动将代码注册的 Cron 同步到数据库

安全

# 脚本执行白名单(空列表表示不限制)
DJANGO_SIMPLETASK_SCRIPT_WHITELIST = ['/path/to/allowed/scripts']

# 自定义字段加密(默认使用 django-safe-fields 的默认加密)
DJANGO_SIMPLETASK_FIELD_CIPHER_CLASS = None                              # 自定义加密器类
DJANGO_SIMPLETASK_RESULT_PASSWORD = None     # 执行结果加密密码
DJANGO_SIMPLETASK_ERROR_MSG_PASSWORD = None  # 错误信息加密密码
DJANGO_SIMPLETASK_CONTEXT_PASSWORD = None    # 上下文数据加密密码

快速开始

1. 定义任务模型

from django.db import models
from django_simpletask5.models import Task

class OrderTask(Task):
    order_id = models.CharField(max_length=64, unique=True)
    customer_name = models.CharField(max_length=128)
    amount = models.DecimalField(max_digits=10, decimal_places=2)
    status = models.CharField(max_length=32, default='pending')

    executor_class = {
        'create': 'myapp.executors.OrderCreateExecutor',
        'update': 'myapp.executors.OrderUpdateExecutor',
        'delete': 'myapp.executors.OrderDeleteExecutor',
    }

    simpletask_queue = {
        'create': 'django_simpletask5.queue.high_priority',
        'update': 'django_simpletask5.queue.default',
        'delete': 'django_simpletask5.queue.default',
    }

    trigger_update_fields = ['customer_name', 'amount', 'status']

2. 编写执行器

# myapp/executors.py
from django_simpletask5.executors.base import BaseExecutor
from django_simpletask5.models import TaskExecution

class OrderCreateExecutor(BaseExecutor):
    def execute(self, execution: TaskExecution) -> str | None:
        context = execution.get_context_dict()
        # 业务逻辑...
        return 'ok'

3. 启动 Worker

默认只监听 default 队列,high_priority 队列需要单独启动 Worker 处理。

# 启动 4 个 Worker 处理 default 队列
python manage.py django_simpletask_executor --workers 4

# 单独启动 Worker 处理 high_priority 队列
python manage.py django_simpletask_executor --queue django_simpletask5.queue.high_priority

# 指定执行器
python manage.py django_simpletask_executor --workers 2 --service myapp.executors.OrderCreateExecutor

4. 启动 Cron 调度

python manage.py django_simpletask_crontab

5. 触发自定义事件

order = OrderTask.objects.get(order_id='ORD-001')
order.trigger('refund', extra_context={'refund_amount': '50.00'})

Cron 任务

在代码中定义 Cron 任务

在任意 app 的 cronjobs.py 文件中使用 register_cronjob 注册:

# myapp/cronjobs.py
from django_simpletask5.cronjob_registry import register_cronjob

register_cronjob(
    name='health_check',
    cron_expression='*/5 * * * *',
    executor_class='django_simpletask5.executors.simple_request.SimpleRequestExecutor',
    context={
        'url': 'https://example.com/health',
        'method': 'GET',
        'timeout': 10,
    },
    description='定期健康检查',
)

从代码同步到数据库

注册的 Cron 任务需要同步到数据库才会生效。框架默认在 django_simpletask_crontab 启动时自动同步(可通过 DJANGO_SIMPLETASK_CRONJOB_AUTO_SYNC = False 关闭),也可以手动执行:

python manage.py django_simpletask_sync_cronjobs

同步后,用户可以在 Django Admin 中查看和修改 Cron 任务,被手动修改过的任务不会在后续同步中被覆盖(is_modified_by_user 标记保护)。

内置执行器

执行器 说明
PingPongExecutor 健康检查,返回 'pong'
BashScriptExecutor 执行 Shell 脚本
PythonScriptExecutor 执行 Python 代码
SimpleRequestExecutor 发起 HTTP 请求
StatusCheckExecutor 检测卡住的执行并标记超时(每 5 分钟)
RetryTimeoutExecutor 重试超时的执行(每 10 分钟)
ArchiveExecutor 归档已完成执行并生成统计(每天凌晨 2 点)

架构

Task 模型变更 → Django 信号 → 创建 TaskExecution 并发布到消息队列
                                                    ↓
Worker 消费消息 → 获取分布式锁 → 加载执行器 → 执行并保存结果
                                                    ↓
失败时自动重试,完成后归档

Releases

0.1.4

  • 修复打包pyproject.toml 添加 package-data 配置,打包时包含 locale po/mo 文件,修复 i18n 翻译不生效的问题
  • 补充依赖requirements.txtpyproject.toml 添加缺失的 django-checkbox-normalizedjango-tabbed-changeform-admin 依赖
  • 完善文档 — README 新增完整配置说明章节,涵盖消息队列(MQ)、分布式锁、队列路由、归档、Cron、安全等全部配置项

0.1.3

  • Admin 界面大升级 — 集成 Tabbed ChangeForm 分页签展示字段,CronJob 后台支持直接执行/启用/禁用操作;TaskExecution 列表优化,execution_id 显示前8字符并支持点击复制,重试次数合并显示,全局字段中文翻译
  • 归档管理增强 — 新增 django_simpletask_archive 管理命令,支持手动触发归档与过期清理;PurgeExpiredExecutor 定期自动清理过期记录;保留天数可配置;采用流式 AES-GCM 加密,大文件归档更高效
  • Dashboard 修复 — 卡片链接使用 reverse 正确跳转,无执行数据时成功率显示 -
  • 超时检测优化 — 模型新增 expire_time 字段,超时判定更准确高效

0.1.2

  • 修复: 修复 Transaction-Message 排序竞态条件,使用 transaction.on_commit 确保消息在事务提交后才发送到队列

0.1.1

  • 修复: 修复 DjangoSimpletask5Config 缺少 ready() 方法导致信号监听未注册的问题

0.1.0

这是 django-simpletask5 的首个正式版本。核心功能包括:

  • 声明式任务模型 — 继承 Task 模型即可定义任务,自动处理创建/更新/删除事件
  • 信号驱动自动发布 — Django 信号自动拦截模型变更,发布 TaskExecution 到消息队列
  • Worker 异步执行 — 多 Worker 进程消费消息队列,支持队列路由和优先级
  • Cron 定时调度 — 内置 crontab 守护进程,支持代码注册与数据库覆盖
  • 重试与超时 — 失败自动重试(指数退避),支持超时检测
  • 加密字段 — 敏感数据自动加密存储
  • 归档统计 — 已完成执行记录自动归档为加密 JSONL,并生成日统计
  • Worker 注册中心 — 基于 Redis 的 Worker 心跳与状态追踪
  • Django Admin 集成 — 完整的后台管理界面与仪表盘

许可证

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_simpletask5-0.1.4.tar.gz (43.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_simpletask5-0.1.4-py3-none-any.whl (52.4 kB view details)

Uploaded Python 3

File details

Details for the file django_simpletask5-0.1.4.tar.gz.

File metadata

  • Download URL: django_simpletask5-0.1.4.tar.gz
  • Upload date:
  • Size: 43.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.9

File hashes

Hashes for django_simpletask5-0.1.4.tar.gz
Algorithm Hash digest
SHA256 ad0fcb30a56b7da66d8405f4d559ce1767b3f8e6901e5de456d712ca2b308a5b
MD5 2e8c190e861f8c599acc02907e12d97d
BLAKE2b-256 c55873c39580d0f979cc23276e9bf26768ee0981521579d42ef540e5e0a85280

See more details on using hashes here.

File details

Details for the file django_simpletask5-0.1.4-py3-none-any.whl.

File metadata

File hashes

Hashes for django_simpletask5-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 fb9989246ce77e208318001e733f5f687caf74216f06661b2a0debad6c05d80e
MD5 96bcd0e684139c1b169ae52b165b6fc2
BLAKE2b-256 4bdec376becda79dde23584654623562235ada588cd7cdfac79b131bc0c7e790

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page