django-simpletask5 0.1.3__tar.gz → 0.2.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {django_simpletask5-0.1.3 → django_simpletask5-0.2.0}/PKG-INFO +112 -1
- {django_simpletask5-0.1.3 → django_simpletask5-0.2.0}/README.md +109 -0
- {django_simpletask5-0.1.3 → django_simpletask5-0.2.0}/django_simpletask5/admin.py +125 -50
- {django_simpletask5-0.1.3 → django_simpletask5-0.2.0}/django_simpletask5/core/defaults.py +1 -2
- {django_simpletask5-0.1.3 → django_simpletask5-0.2.0}/django_simpletask5/core/publisher.py +27 -8
- {django_simpletask5-0.1.3 → django_simpletask5-0.2.0}/django_simpletask5/core/worker_registry.py +13 -11
- {django_simpletask5-0.1.3 → django_simpletask5-0.2.0}/django_simpletask5/executors/bash_script.py +11 -3
- {django_simpletask5-0.1.3 → django_simpletask5-0.2.0}/django_simpletask5/executors/python_script.py +10 -2
- django_simpletask5-0.2.0/django_simpletask5/executors/retry_timeout.py +47 -0
- {django_simpletask5-0.1.3 → django_simpletask5-0.2.0}/django_simpletask5/executors/simple_request.py +3 -0
- django_simpletask5-0.2.0/django_simpletask5/locale/zh_Hans/LC_MESSAGES/django.mo +0 -0
- django_simpletask5-0.2.0/django_simpletask5/locale/zh_Hans/LC_MESSAGES/django.po +418 -0
- {django_simpletask5-0.1.3 → django_simpletask5-0.2.0}/django_simpletask5/management/commands/django_simpletask_executor.py +18 -2
- django_simpletask5-0.2.0/django_simpletask5/migrations/0006_alter_cronjob_options_alter_taskexecution_created_at_and_more.py +67 -0
- django_simpletask5-0.2.0/django_simpletask5/migrations/0007_rename_task_model_to_executor_class_in_stat.py +34 -0
- {django_simpletask5-0.1.3 → django_simpletask5-0.2.0}/django_simpletask5/models.py +17 -8
- {django_simpletask5-0.1.3 → django_simpletask5-0.2.0}/django_simpletask5/services/archive.py +3 -3
- {django_simpletask5-0.1.3 → django_simpletask5-0.2.0}/django_simpletask5.egg-info/PKG-INFO +112 -1
- {django_simpletask5-0.1.3 → django_simpletask5-0.2.0}/django_simpletask5.egg-info/SOURCES.txt +4 -0
- {django_simpletask5-0.1.3 → django_simpletask5-0.2.0}/django_simpletask5.egg-info/requires.txt +2 -0
- {django_simpletask5-0.1.3 → django_simpletask5-0.2.0}/pyproject.toml +6 -1
- django_simpletask5-0.1.3/django_simpletask5/executors/retry_timeout.py +0 -32
- {django_simpletask5-0.1.3 → django_simpletask5-0.2.0}/LICENSE +0 -0
- {django_simpletask5-0.1.3 → django_simpletask5-0.2.0}/django_simpletask5/__init__.py +0 -0
- {django_simpletask5-0.1.3 → django_simpletask5-0.2.0}/django_simpletask5/apps.py +0 -0
- {django_simpletask5-0.1.3 → django_simpletask5-0.2.0}/django_simpletask5/core/__init__.py +0 -0
- {django_simpletask5-0.1.3 → django_simpletask5-0.2.0}/django_simpletask5/core/cronjob_registry.py +0 -0
- {django_simpletask5-0.1.3 → django_simpletask5-0.2.0}/django_simpletask5/core/executor_scanner.py +0 -0
- {django_simpletask5-0.1.3 → django_simpletask5-0.2.0}/django_simpletask5/core/lock.py +0 -0
- {django_simpletask5-0.1.3 → django_simpletask5-0.2.0}/django_simpletask5/core/message_queue.py +0 -0
- {django_simpletask5-0.1.3 → django_simpletask5-0.2.0}/django_simpletask5/core/signals.py +0 -0
- {django_simpletask5-0.1.3 → django_simpletask5-0.2.0}/django_simpletask5/dashboards.py +0 -0
- {django_simpletask5-0.1.3 → django_simpletask5-0.2.0}/django_simpletask5/executors/__init__.py +0 -0
- {django_simpletask5-0.1.3 → django_simpletask5-0.2.0}/django_simpletask5/executors/archive.py +0 -0
- {django_simpletask5-0.1.3 → django_simpletask5-0.2.0}/django_simpletask5/executors/base.py +0 -0
- {django_simpletask5-0.1.3 → django_simpletask5-0.2.0}/django_simpletask5/executors/loader.py +0 -0
- {django_simpletask5-0.1.3 → django_simpletask5-0.2.0}/django_simpletask5/executors/ping_pong.py +0 -0
- {django_simpletask5-0.1.3 → django_simpletask5-0.2.0}/django_simpletask5/executors/status_check.py +0 -0
- {django_simpletask5-0.1.3 → django_simpletask5-0.2.0}/django_simpletask5/management/__init__.py +0 -0
- {django_simpletask5-0.1.3 → django_simpletask5-0.2.0}/django_simpletask5/management/commands/__init__.py +0 -0
- {django_simpletask5-0.1.3 → django_simpletask5-0.2.0}/django_simpletask5/management/commands/django_simpletask_archive.py +0 -0
- {django_simpletask5-0.1.3 → django_simpletask5-0.2.0}/django_simpletask5/management/commands/django_simpletask_crontab.py +0 -0
- {django_simpletask5-0.1.3 → django_simpletask5-0.2.0}/django_simpletask5/management/commands/django_simpletask_sync_cronjobs.py +0 -0
- {django_simpletask5-0.1.3 → django_simpletask5-0.2.0}/django_simpletask5/migrations/0001_initial.py +0 -0
- {django_simpletask5-0.1.3 → django_simpletask5-0.2.0}/django_simpletask5/migrations/0002_taskexecution_done_tasks_count_and_more.py +0 -0
- {django_simpletask5-0.1.3 → django_simpletask5-0.2.0}/django_simpletask5/migrations/0003_alter_cronjob_options_remove_cronjob_name_and_more.py +0 -0
- {django_simpletask5-0.1.3 → django_simpletask5-0.2.0}/django_simpletask5/migrations/0004_alter_cronjob_is_modified_by_user.py +0 -0
- {django_simpletask5-0.1.3 → django_simpletask5-0.2.0}/django_simpletask5/migrations/0005_alter_cronjob_is_modified_by_user_help.py +0 -0
- {django_simpletask5-0.1.3 → django_simpletask5-0.2.0}/django_simpletask5/migrations/__init__.py +0 -0
- {django_simpletask5-0.1.3 → django_simpletask5-0.2.0}/django_simpletask5/services/__init__.py +0 -0
- {django_simpletask5-0.1.3 → django_simpletask5-0.2.0}/django_simpletask5.egg-info/dependency_links.txt +0 -0
- {django_simpletask5-0.1.3 → django_simpletask5-0.2.0}/django_simpletask5.egg-info/top_level.txt +0 -0
- {django_simpletask5-0.1.3 → django_simpletask5-0.2.0}/setup.cfg +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: django-simpletask5
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.2.0
|
|
4
4
|
Summary: Lightweight async task execution framework for Django
|
|
5
5
|
Author-email: rRR0VrFP <rrr0vrfp@qq.com>
|
|
6
6
|
Maintainer-email: rRR0VrFP <rrr0vrfp@qq.com>
|
|
@@ -27,6 +27,8 @@ Requires-Dist: globallock>=0.1.4
|
|
|
27
27
|
Requires-Dist: django-safe-fields>=0.2.3
|
|
28
28
|
Requires-Dist: django-app-requires>=0.3.6
|
|
29
29
|
Requires-Dist: django-admin-dashboards>=0.1.1
|
|
30
|
+
Requires-Dist: django-checkbox-normalize>=0.2.2
|
|
31
|
+
Requires-Dist: django-tabbed-changeform-admin>=0.1.9
|
|
30
32
|
Requires-Dist: cryptography>=48.0.0
|
|
31
33
|
Requires-Dist: croniter>=6.2.2
|
|
32
34
|
Requires-Dist: requests>=2.34.2
|
|
@@ -104,6 +106,92 @@ DJANGO_SIMPLETASK_LOCK_CONFIG = {
|
|
|
104
106
|
python manage.py migrate django_simpletask5
|
|
105
107
|
```
|
|
106
108
|
|
|
109
|
+
## 配置说明
|
|
110
|
+
|
|
111
|
+
所有配置项均为可选,设有合理的默认值。以下按功能分类说明。
|
|
112
|
+
|
|
113
|
+
### 消息队列(MQ)
|
|
114
|
+
|
|
115
|
+
默认使用内存队列(`memory://`),适合开发调试。生产环境建议切换为 RabbitMQ 或 Redis。
|
|
116
|
+
|
|
117
|
+
```python
|
|
118
|
+
# 方式一:直接指定完整 Broker URL(优先级最高)
|
|
119
|
+
DJANGO_SIMPLETASK_BROKER_URL = 'amqp://guest:guest@127.0.0.1:5672//'
|
|
120
|
+
|
|
121
|
+
# 方式二:分别指定类型和连接参数
|
|
122
|
+
DJANGO_SIMPLETASK_MESSAGE_QUEUE_TYPE = 'rabbitmq' # 'memory' | 'rabbitmq' | 'redis'
|
|
123
|
+
DJANGO_SIMPLETASK_RABBITMQ_HOST = '127.0.0.1'
|
|
124
|
+
DJANGO_SIMPLETASK_RABBITMQ_PORT = 5672
|
|
125
|
+
DJANGO_SIMPLETASK_RABBITMQ_USER = 'guest'
|
|
126
|
+
DJANGO_SIMPLETASK_RABBITMQ_PASSWORD = 'guest'
|
|
127
|
+
|
|
128
|
+
# Redis 作为 MQ 时:
|
|
129
|
+
# DJANGO_SIMPLETASK_MESSAGE_QUEUE_TYPE = 'redis'
|
|
130
|
+
# DJANGO_SIMPLETASK_REDIS_MQ_HOST = '127.0.0.1'
|
|
131
|
+
# DJANGO_SIMPLETASK_REDIS_MQ_PORT = 6379
|
|
132
|
+
# DJANGO_SIMPLETASK_REDIS_MQ_DB = 0
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
### 分布式锁
|
|
136
|
+
|
|
137
|
+
```python
|
|
138
|
+
DJANGO_SIMPLETASK_LOCK_CONFIG = {
|
|
139
|
+
'global_lock_engine_class': 'globallock.redis_global_lock.RedisGlobalLock',
|
|
140
|
+
'global_lock_engine_options': {
|
|
141
|
+
'host': '127.0.0.1',
|
|
142
|
+
'port': 6379,
|
|
143
|
+
'db': 0,
|
|
144
|
+
},
|
|
145
|
+
}
|
|
146
|
+
DJANGO_SIMPLETASK_LOCK_TIMEOUT = 600 # 锁超时时间(秒)
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
### 队列路由
|
|
150
|
+
|
|
151
|
+
```python
|
|
152
|
+
DJANGO_SIMPLETASK_DEFAULT_QUEUE = 'django_simpletask5.queue.default' # 默认队列
|
|
153
|
+
DJANGO_SIMPLETASK_HIGH_PRIORITY_QUEUE = 'django_simpletask5.queue.high_priority' # 高优先级队列
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
### 执行与重试
|
|
157
|
+
|
|
158
|
+
```python
|
|
159
|
+
DJANGO_SIMPLETASK_DEFAULT_MAX_RETRIES = 3 # 默认最大重试次数
|
|
160
|
+
DJANGO_SIMPLETASK_DEFAULT_TIMEOUT_SECONDS = 600 # 默认执行超时时间(秒)
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
### 归档
|
|
164
|
+
|
|
165
|
+
```python
|
|
166
|
+
DJANGO_SIMPLETASK_ARCHIVE_PATH = 'django_simpletask5_archives' # 归档文件存储子目录
|
|
167
|
+
DJANGO_SIMPLETASK_ARCHIVE_SALT = 'django-simpletask5-archive' # 归档加密盐值(结合 SECRET_KEY 派生 AES 密钥)
|
|
168
|
+
DJANGO_SIMPLETASK_ARCHIVE_RETENTION_DAYS = 7 # 归档保留天数
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
### Cron
|
|
172
|
+
|
|
173
|
+
```python
|
|
174
|
+
DJANGO_SIMPLETASK_CRONJOB_AUTO_SYNC = True # 是否自动将代码注册的 Cron 同步到数据库
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
### 安全
|
|
178
|
+
|
|
179
|
+
```python
|
|
180
|
+
# 是否启用 Python/Shell 脚本执行器(默认关闭,开启有安全风险)
|
|
181
|
+
DJANGO_SIMPLETASK_ENABLE_SCRIPT_EXECUTORS = False
|
|
182
|
+
|
|
183
|
+
# 脚本执行白名单(空列表表示不限制)
|
|
184
|
+
DJANGO_SIMPLETASK_SCRIPT_WHITELIST = ['/path/to/allowed/scripts']
|
|
185
|
+
|
|
186
|
+
# 自定义字段加密(默认使用 django-safe-fields 的默认加密)
|
|
187
|
+
DJANGO_SIMPLETASK_FIELD_CIPHER_CLASS = None # 自定义加密器类
|
|
188
|
+
DJANGO_SIMPLETASK_RESULT_PASSWORD = None # 执行结果加密密码
|
|
189
|
+
DJANGO_SIMPLETASK_ERROR_MSG_PASSWORD = None # 错误信息加密密码
|
|
190
|
+
DJANGO_SIMPLETASK_CONTEXT_PASSWORD = None # 上下文数据加密密码
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
> 注意:`DJANGO_SIMPLETASK_ENABLE_SCRIPT_EXECUTORS` 设为 `True` 后,还需为用户/组授予 `django_simpletask5 | Cron job | Can use script executors` 权限,非超级管理员无法创建 Python/Shell 脚本执行器类型的定时任务。
|
|
194
|
+
|
|
107
195
|
## 快速开始
|
|
108
196
|
|
|
109
197
|
### 1. 定义任务模型
|
|
@@ -232,6 +320,29 @@ Worker 消费消息 → 获取分布式锁 → 加载执行器 → 执行并保
|
|
|
232
320
|
|
|
233
321
|
## Releases
|
|
234
322
|
|
|
323
|
+
### 0.2.0
|
|
324
|
+
|
|
325
|
+
- **破坏性变更**: 移除 `Task` 模型上的自定义主键,`task_id` 不再是主键字段,改为 `unique=True` 的唯一标识字段。所有 `Task` 子模型将自动获得 Django 默认的自增 `id` 主键。已有数据库需要迁移处理。
|
|
326
|
+
- **统计维度调整**: `TaskExecutionStat` 的统计维度由 `task_model` 改为 `executor_class`,统一覆盖有模型任务和 Cron 定时任务两种场景。对应 API `_update_stats()` 按 `executor_class` 分组聚合。
|
|
327
|
+
- **Admin 优化**: CronJob 列表页移除 `executor_class` 列避免表格撑开;TaskExecutionStat 列表页新增 `executor_class` 列、`created_at` 设为只读修复详情页错误。
|
|
328
|
+
- **测试修复**: 修复 `TransactionTestCase` + `on_commit` 在 SQLite 共享内存连接下因 `TestCase` 残留原子块导致回调不执行的兼容性问题;Redis MQ / RabbitMQ E2E 集成测试全部通过。
|
|
329
|
+
|
|
330
|
+
### 0.1.5
|
|
331
|
+
|
|
332
|
+
- **安全加固** — 新增 `DJANGO_SIMPLETASK_ENABLE_SCRIPT_EXECUTORS` 配置项(默认关闭),启用后才可执行 Python/Shell 脚本;新增 `can_use_script_executors` 权限,仅有此权限或超级管理员的用户才能在 Admin 中创建脚本执行器类型的 CronJob
|
|
333
|
+
- **Bug 修复** — 修复 `RetryTimeoutExecutor` 中 lambda 晚绑定导致所有回调引用最后一个记录的问题
|
|
334
|
+
- **Admin 安全** — 操作按钮(立即执行、启用/禁用)从 GET 参数改为 POST 请求,附带 CSRF 保护
|
|
335
|
+
- **性能优化** — `RetryTimeoutExecutor` 改为批量 update + 按 ID 分发消息推送
|
|
336
|
+
- **Admin 增强** — `TaskExecutionAdmin` 新增 `executor_class` 过滤、`error_message` 搜索、批量重试失败任务、批量取消待处理任务
|
|
337
|
+
- **数据索引** — 添加 `(status, expire_time)` 和 `(is_active, next_run_time)` 复合索引;`trigger_event`、`executor_class`、`task_id`、`created_at` 添加单字段索引
|
|
338
|
+
- **代码规范** — `bash_script.py`、`python_script.py`、`simple_request.py` 补充缺失的 logger;移除废弃的 `allow_tags` 属性
|
|
339
|
+
|
|
340
|
+
### 0.1.4
|
|
341
|
+
|
|
342
|
+
- **修复打包** — `pyproject.toml` 添加 `package-data` 配置,打包时包含 locale po/mo 文件,修复 i18n 翻译不生效的问题
|
|
343
|
+
- **补充依赖** — `requirements.txt` 和 `pyproject.toml` 添加缺失的 `django-checkbox-normalize`、`django-tabbed-changeform-admin` 依赖
|
|
344
|
+
- **完善文档** — README 新增完整配置说明章节,涵盖消息队列(MQ)、分布式锁、队列路由、归档、Cron、安全等全部配置项
|
|
345
|
+
|
|
235
346
|
### 0.1.3
|
|
236
347
|
|
|
237
348
|
- **Admin 界面大升级** — 集成 Tabbed ChangeForm 分页签展示字段,CronJob 后台支持直接执行/启用/禁用操作;TaskExecution 列表优化,execution_id 显示前8字符并支持点击复制,重试次数合并显示,全局字段中文翻译
|
|
@@ -67,6 +67,92 @@ DJANGO_SIMPLETASK_LOCK_CONFIG = {
|
|
|
67
67
|
python manage.py migrate django_simpletask5
|
|
68
68
|
```
|
|
69
69
|
|
|
70
|
+
## 配置说明
|
|
71
|
+
|
|
72
|
+
所有配置项均为可选,设有合理的默认值。以下按功能分类说明。
|
|
73
|
+
|
|
74
|
+
### 消息队列(MQ)
|
|
75
|
+
|
|
76
|
+
默认使用内存队列(`memory://`),适合开发调试。生产环境建议切换为 RabbitMQ 或 Redis。
|
|
77
|
+
|
|
78
|
+
```python
|
|
79
|
+
# 方式一:直接指定完整 Broker URL(优先级最高)
|
|
80
|
+
DJANGO_SIMPLETASK_BROKER_URL = 'amqp://guest:guest@127.0.0.1:5672//'
|
|
81
|
+
|
|
82
|
+
# 方式二:分别指定类型和连接参数
|
|
83
|
+
DJANGO_SIMPLETASK_MESSAGE_QUEUE_TYPE = 'rabbitmq' # 'memory' | 'rabbitmq' | 'redis'
|
|
84
|
+
DJANGO_SIMPLETASK_RABBITMQ_HOST = '127.0.0.1'
|
|
85
|
+
DJANGO_SIMPLETASK_RABBITMQ_PORT = 5672
|
|
86
|
+
DJANGO_SIMPLETASK_RABBITMQ_USER = 'guest'
|
|
87
|
+
DJANGO_SIMPLETASK_RABBITMQ_PASSWORD = 'guest'
|
|
88
|
+
|
|
89
|
+
# Redis 作为 MQ 时:
|
|
90
|
+
# DJANGO_SIMPLETASK_MESSAGE_QUEUE_TYPE = 'redis'
|
|
91
|
+
# DJANGO_SIMPLETASK_REDIS_MQ_HOST = '127.0.0.1'
|
|
92
|
+
# DJANGO_SIMPLETASK_REDIS_MQ_PORT = 6379
|
|
93
|
+
# DJANGO_SIMPLETASK_REDIS_MQ_DB = 0
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
### 分布式锁
|
|
97
|
+
|
|
98
|
+
```python
|
|
99
|
+
DJANGO_SIMPLETASK_LOCK_CONFIG = {
|
|
100
|
+
'global_lock_engine_class': 'globallock.redis_global_lock.RedisGlobalLock',
|
|
101
|
+
'global_lock_engine_options': {
|
|
102
|
+
'host': '127.0.0.1',
|
|
103
|
+
'port': 6379,
|
|
104
|
+
'db': 0,
|
|
105
|
+
},
|
|
106
|
+
}
|
|
107
|
+
DJANGO_SIMPLETASK_LOCK_TIMEOUT = 600 # 锁超时时间(秒)
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
### 队列路由
|
|
111
|
+
|
|
112
|
+
```python
|
|
113
|
+
DJANGO_SIMPLETASK_DEFAULT_QUEUE = 'django_simpletask5.queue.default' # 默认队列
|
|
114
|
+
DJANGO_SIMPLETASK_HIGH_PRIORITY_QUEUE = 'django_simpletask5.queue.high_priority' # 高优先级队列
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
### 执行与重试
|
|
118
|
+
|
|
119
|
+
```python
|
|
120
|
+
DJANGO_SIMPLETASK_DEFAULT_MAX_RETRIES = 3 # 默认最大重试次数
|
|
121
|
+
DJANGO_SIMPLETASK_DEFAULT_TIMEOUT_SECONDS = 600 # 默认执行超时时间(秒)
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
### 归档
|
|
125
|
+
|
|
126
|
+
```python
|
|
127
|
+
DJANGO_SIMPLETASK_ARCHIVE_PATH = 'django_simpletask5_archives' # 归档文件存储子目录
|
|
128
|
+
DJANGO_SIMPLETASK_ARCHIVE_SALT = 'django-simpletask5-archive' # 归档加密盐值(结合 SECRET_KEY 派生 AES 密钥)
|
|
129
|
+
DJANGO_SIMPLETASK_ARCHIVE_RETENTION_DAYS = 7 # 归档保留天数
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
### Cron
|
|
133
|
+
|
|
134
|
+
```python
|
|
135
|
+
DJANGO_SIMPLETASK_CRONJOB_AUTO_SYNC = True # 是否自动将代码注册的 Cron 同步到数据库
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
### 安全
|
|
139
|
+
|
|
140
|
+
```python
|
|
141
|
+
# 是否启用 Python/Shell 脚本执行器(默认关闭,开启有安全风险)
|
|
142
|
+
DJANGO_SIMPLETASK_ENABLE_SCRIPT_EXECUTORS = False
|
|
143
|
+
|
|
144
|
+
# 脚本执行白名单(空列表表示不限制)
|
|
145
|
+
DJANGO_SIMPLETASK_SCRIPT_WHITELIST = ['/path/to/allowed/scripts']
|
|
146
|
+
|
|
147
|
+
# 自定义字段加密(默认使用 django-safe-fields 的默认加密)
|
|
148
|
+
DJANGO_SIMPLETASK_FIELD_CIPHER_CLASS = None # 自定义加密器类
|
|
149
|
+
DJANGO_SIMPLETASK_RESULT_PASSWORD = None # 执行结果加密密码
|
|
150
|
+
DJANGO_SIMPLETASK_ERROR_MSG_PASSWORD = None # 错误信息加密密码
|
|
151
|
+
DJANGO_SIMPLETASK_CONTEXT_PASSWORD = None # 上下文数据加密密码
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
> 注意:`DJANGO_SIMPLETASK_ENABLE_SCRIPT_EXECUTORS` 设为 `True` 后,还需为用户/组授予 `django_simpletask5 | Cron job | Can use script executors` 权限,非超级管理员无法创建 Python/Shell 脚本执行器类型的定时任务。
|
|
155
|
+
|
|
70
156
|
## 快速开始
|
|
71
157
|
|
|
72
158
|
### 1. 定义任务模型
|
|
@@ -195,6 +281,29 @@ Worker 消费消息 → 获取分布式锁 → 加载执行器 → 执行并保
|
|
|
195
281
|
|
|
196
282
|
## Releases
|
|
197
283
|
|
|
284
|
+
### 0.2.0
|
|
285
|
+
|
|
286
|
+
- **破坏性变更**: 移除 `Task` 模型上的自定义主键,`task_id` 不再是主键字段,改为 `unique=True` 的唯一标识字段。所有 `Task` 子模型将自动获得 Django 默认的自增 `id` 主键。已有数据库需要迁移处理。
|
|
287
|
+
- **统计维度调整**: `TaskExecutionStat` 的统计维度由 `task_model` 改为 `executor_class`,统一覆盖有模型任务和 Cron 定时任务两种场景。对应 API `_update_stats()` 按 `executor_class` 分组聚合。
|
|
288
|
+
- **Admin 优化**: CronJob 列表页移除 `executor_class` 列避免表格撑开;TaskExecutionStat 列表页新增 `executor_class` 列、`created_at` 设为只读修复详情页错误。
|
|
289
|
+
- **测试修复**: 修复 `TransactionTestCase` + `on_commit` 在 SQLite 共享内存连接下因 `TestCase` 残留原子块导致回调不执行的兼容性问题;Redis MQ / RabbitMQ E2E 集成测试全部通过。
|
|
290
|
+
|
|
291
|
+
### 0.1.5
|
|
292
|
+
|
|
293
|
+
- **安全加固** — 新增 `DJANGO_SIMPLETASK_ENABLE_SCRIPT_EXECUTORS` 配置项(默认关闭),启用后才可执行 Python/Shell 脚本;新增 `can_use_script_executors` 权限,仅有此权限或超级管理员的用户才能在 Admin 中创建脚本执行器类型的 CronJob
|
|
294
|
+
- **Bug 修复** — 修复 `RetryTimeoutExecutor` 中 lambda 晚绑定导致所有回调引用最后一个记录的问题
|
|
295
|
+
- **Admin 安全** — 操作按钮(立即执行、启用/禁用)从 GET 参数改为 POST 请求,附带 CSRF 保护
|
|
296
|
+
- **性能优化** — `RetryTimeoutExecutor` 改为批量 update + 按 ID 分发消息推送
|
|
297
|
+
- **Admin 增强** — `TaskExecutionAdmin` 新增 `executor_class` 过滤、`error_message` 搜索、批量重试失败任务、批量取消待处理任务
|
|
298
|
+
- **数据索引** — 添加 `(status, expire_time)` 和 `(is_active, next_run_time)` 复合索引;`trigger_event`、`executor_class`、`task_id`、`created_at` 添加单字段索引
|
|
299
|
+
- **代码规范** — `bash_script.py`、`python_script.py`、`simple_request.py` 补充缺失的 logger;移除废弃的 `allow_tags` 属性
|
|
300
|
+
|
|
301
|
+
### 0.1.4
|
|
302
|
+
|
|
303
|
+
- **修复打包** — `pyproject.toml` 添加 `package-data` 配置,打包时包含 locale po/mo 文件,修复 i18n 翻译不生效的问题
|
|
304
|
+
- **补充依赖** — `requirements.txt` 和 `pyproject.toml` 添加缺失的 `django-checkbox-normalize`、`django-tabbed-changeform-admin` 依赖
|
|
305
|
+
- **完善文档** — README 新增完整配置说明章节,涵盖消息队列(MQ)、分布式锁、队列路由、归档、Cron、安全等全部配置项
|
|
306
|
+
|
|
198
307
|
### 0.1.3
|
|
199
308
|
|
|
200
309
|
- **Admin 界面大升级** — 集成 Tabbed ChangeForm 分页签展示字段,CronJob 后台支持直接执行/启用/禁用操作;TaskExecution 列表优化,execution_id 显示前8字符并支持点击复制,重试次数合并显示,全局字段中文翻译
|
|
@@ -2,8 +2,9 @@ import json
|
|
|
2
2
|
|
|
3
3
|
from django import forms
|
|
4
4
|
from django.contrib import admin
|
|
5
|
-
from django.http import HttpResponseRedirect
|
|
6
|
-
from django.urls import reverse
|
|
5
|
+
from django.http import HttpResponseRedirect, Http404
|
|
6
|
+
from django.urls import path, reverse
|
|
7
|
+
from django.utils.html import format_html, mark_safe
|
|
7
8
|
from django.utils.translation import gettext_lazy as _
|
|
8
9
|
from django_checkbox_normalize.admin import DjangoCheckboxNormalizeAdmin
|
|
9
10
|
from django_tabbed_changeform_admin.admin import DjangoTabbedChangeformAdmin
|
|
@@ -15,6 +16,11 @@ from django_simpletask5.core.executor_scanner import (
|
|
|
15
16
|
get_executor_schema_map,
|
|
16
17
|
)
|
|
17
18
|
|
|
19
|
+
SCRIPT_EXECUTOR_CLASSES = {
|
|
20
|
+
'django_simpletask5.executors.bash_script.BashScriptExecutor',
|
|
21
|
+
'django_simpletask5.executors.python_script.PythonScriptExecutor',
|
|
22
|
+
}
|
|
23
|
+
|
|
18
24
|
|
|
19
25
|
class ExecutorClassWidget(forms.Select):
|
|
20
26
|
def __init__(self, attrs=None):
|
|
@@ -58,6 +64,7 @@ class CronJobForm(forms.ModelForm):
|
|
|
58
64
|
)
|
|
59
65
|
|
|
60
66
|
def __init__(self, *args, **kwargs):
|
|
67
|
+
self.request = kwargs.pop('request', None)
|
|
61
68
|
super().__init__(*args, **kwargs)
|
|
62
69
|
if self.instance and self.instance.pk:
|
|
63
70
|
self.fields['uid'].disabled = True
|
|
@@ -75,6 +82,16 @@ class CronJobForm(forms.ModelForm):
|
|
|
75
82
|
except Exception:
|
|
76
83
|
pass
|
|
77
84
|
|
|
85
|
+
def clean_executor_class(self):
|
|
86
|
+
executor_class = self.cleaned_data.get("executor_class", "")
|
|
87
|
+
if executor_class in SCRIPT_EXECUTOR_CLASSES:
|
|
88
|
+
if self.request and not (self.request.user.is_superuser or self.request.user.has_perm('django_simpletask5.can_use_script_executors')):
|
|
89
|
+
raise forms.ValidationError(
|
|
90
|
+
_('You do not have permission to use script executors (Python/Shell). '
|
|
91
|
+
'Only superusers and users with the "can_use_script_executors" permission can do so.')
|
|
92
|
+
)
|
|
93
|
+
return executor_class
|
|
94
|
+
|
|
78
95
|
def clean_context(self):
|
|
79
96
|
raw = self.cleaned_data.get("context")
|
|
80
97
|
if not raw:
|
|
@@ -125,7 +142,6 @@ class TaskExecutionAdmin(DjangoCheckboxNormalizeAdmin, DjangoTabbedChangeformAdm
|
|
|
125
142
|
|
|
126
143
|
@admin.display(description=_("Execution ID"))
|
|
127
144
|
def short_execution_id(self, obj):
|
|
128
|
-
from django.utils.html import format_html
|
|
129
145
|
full = str(obj.execution_id)
|
|
130
146
|
short = full[:8]
|
|
131
147
|
copied_text = _("Copied!")
|
|
@@ -136,8 +152,8 @@ class TaskExecutionAdmin(DjangoCheckboxNormalizeAdmin, DjangoTabbedChangeformAdm
|
|
|
136
152
|
'setTimeout(function(){{t.textContent=o;}},800);}}.bind(this)).catch(function(){{}})">{}</span>',
|
|
137
153
|
full, full, copied_text, short
|
|
138
154
|
)
|
|
139
|
-
list_filter = ["status", "trigger_event", "created_at"]
|
|
140
|
-
search_fields = ["execution_id", "task_id", "executor_class"]
|
|
155
|
+
list_filter = ["status", "trigger_event", "executor_class", "created_at"]
|
|
156
|
+
search_fields = ["execution_id", "task_id", "executor_class", "error_message"]
|
|
141
157
|
readonly_fields = [
|
|
142
158
|
"execution_id", "task_id", "task_model", "trigger_event", "executor_class", "status",
|
|
143
159
|
"result", "error_message", "context", "total_tasks_count", "done_tasks_count",
|
|
@@ -145,6 +161,7 @@ class TaskExecutionAdmin(DjangoCheckboxNormalizeAdmin, DjangoTabbedChangeformAdm
|
|
|
145
161
|
"started_at", "finished_at", "created_at", "updated_at",
|
|
146
162
|
]
|
|
147
163
|
ordering = ["-created_at"]
|
|
164
|
+
actions = ["retry_selected", "cancel_selected"]
|
|
148
165
|
|
|
149
166
|
def has_add_permission(self, request):
|
|
150
167
|
return False
|
|
@@ -185,6 +202,23 @@ class TaskExecutionAdmin(DjangoCheckboxNormalizeAdmin, DjangoTabbedChangeformAdm
|
|
|
185
202
|
def retry_info(self, obj):
|
|
186
203
|
return f"{obj.retry_count}/{obj.max_retries}"
|
|
187
204
|
|
|
205
|
+
@admin.action(description=_("Retry selected failed tasks"))
|
|
206
|
+
def retry_selected(self, request, queryset):
|
|
207
|
+
from django_simpletask5.core.publisher import _publish_message
|
|
208
|
+
count = 0
|
|
209
|
+
for exec_record in queryset.filter(status__in=['failed', 'timeout']):
|
|
210
|
+
exec_record.status = 'retry'
|
|
211
|
+
exec_record.retry_count += 1
|
|
212
|
+
exec_record.save(update_fields=['status', 'retry_count', 'updated_at'])
|
|
213
|
+
_publish_message(exec_record)
|
|
214
|
+
count += 1
|
|
215
|
+
self.message_user(request, _("%d execution(s) have been re-queued.") % count)
|
|
216
|
+
|
|
217
|
+
@admin.action(description=_("Cancel selected pending tasks"))
|
|
218
|
+
def cancel_selected(self, request, queryset):
|
|
219
|
+
updated = queryset.filter(status='pending').update(status='failed')
|
|
220
|
+
self.message_user(request, _("%d execution(s) have been cancelled.") % updated)
|
|
221
|
+
|
|
188
222
|
|
|
189
223
|
@admin.register(CronJob)
|
|
190
224
|
class CronJobAdmin(DjangoCheckboxNormalizeAdmin, DjangoTabbedChangeformAdmin):
|
|
@@ -192,7 +226,6 @@ class CronJobAdmin(DjangoCheckboxNormalizeAdmin, DjangoTabbedChangeformAdmin):
|
|
|
192
226
|
list_display = [
|
|
193
227
|
"display_name",
|
|
194
228
|
"cron_expression",
|
|
195
|
-
"executor_class",
|
|
196
229
|
"is_active",
|
|
197
230
|
"is_modified_by_user",
|
|
198
231
|
"updated_at",
|
|
@@ -228,62 +261,102 @@ class CronJobAdmin(DjangoCheckboxNormalizeAdmin, DjangoTabbedChangeformAdmin):
|
|
|
228
261
|
def get_queryset(self, request):
|
|
229
262
|
return super().get_queryset(request)
|
|
230
263
|
|
|
231
|
-
def
|
|
232
|
-
|
|
233
|
-
|
|
264
|
+
def get_urls(self):
|
|
265
|
+
urls = super().get_urls()
|
|
266
|
+
custom_urls = [
|
|
267
|
+
path(
|
|
268
|
+
'<path:object_id>/run/',
|
|
269
|
+
self.admin_site.admin_view(self._run_cronjob_view),
|
|
270
|
+
name='django_simpletask5_cronjob_run',
|
|
271
|
+
),
|
|
272
|
+
path(
|
|
273
|
+
'<path:object_id>/toggle-active/',
|
|
274
|
+
self.admin_site.admin_view(self._toggle_active_view),
|
|
275
|
+
name='django_simpletask5_cronjob_toggle_active',
|
|
276
|
+
),
|
|
277
|
+
]
|
|
278
|
+
return custom_urls + urls
|
|
234
279
|
|
|
235
|
-
|
|
280
|
+
def _run_cronjob_view(self, request, object_id):
|
|
281
|
+
if request.method != 'POST':
|
|
282
|
+
return HttpResponseRedirect(
|
|
283
|
+
reverse('admin:django_simpletask5_cronjob_changelist')
|
|
284
|
+
)
|
|
285
|
+
try:
|
|
286
|
+
cronjob = CronJob.objects.get(pk=object_id)
|
|
287
|
+
from django_simpletask5.management.commands.django_simpletask_crontab import Command as CrontabCommand
|
|
288
|
+
cmd = CrontabCommand()
|
|
289
|
+
cmd._trigger_cronjob(cronjob)
|
|
290
|
+
self.message_user(request, _('Cronjob "%s" triggered.') % cronjob.display_name)
|
|
291
|
+
except CronJob.DoesNotExist:
|
|
292
|
+
raise Http404()
|
|
293
|
+
return HttpResponseRedirect(
|
|
294
|
+
reverse('admin:django_simpletask5_cronjob_changelist')
|
|
295
|
+
)
|
|
296
|
+
|
|
297
|
+
def _toggle_active_view(self, request, object_id):
|
|
298
|
+
if request.method != 'POST':
|
|
299
|
+
return HttpResponseRedirect(
|
|
300
|
+
reverse('admin:django_simpletask5_cronjob_changelist')
|
|
301
|
+
)
|
|
302
|
+
try:
|
|
303
|
+
cronjob = CronJob.objects.get(pk=object_id)
|
|
304
|
+
cronjob.is_active = not cronjob.is_active
|
|
305
|
+
cronjob.save(update_fields=['is_active'])
|
|
306
|
+
status = _('activated') if cronjob.is_active else _('deactivated')
|
|
307
|
+
self.message_user(request, _('Cronjob "%s" %s.') % (cronjob.display_name, status))
|
|
308
|
+
except CronJob.DoesNotExist:
|
|
309
|
+
raise Http404()
|
|
310
|
+
return HttpResponseRedirect(
|
|
311
|
+
reverse('admin:django_simpletask5_cronjob_changelist')
|
|
312
|
+
)
|
|
313
|
+
|
|
314
|
+
def action_buttons(self, obj):
|
|
315
|
+
run_url = reverse('admin:django_simpletask5_cronjob_run', args=[obj.pk])
|
|
316
|
+
toggle_url = reverse('admin:django_simpletask5_cronjob_toggle_active', args=[obj.pk])
|
|
317
|
+
csrf_token = '"{{ csrf_token }}"'
|
|
236
318
|
|
|
237
319
|
buttons = []
|
|
238
320
|
buttons.append(
|
|
239
|
-
f'<
|
|
240
|
-
f'
|
|
241
|
-
f'<
|
|
321
|
+
f'<form style="display:inline" method="post" action="{run_url}">'
|
|
322
|
+
f'<input type="hidden" name="csrfmiddlewaretoken" value={csrf_token}>'
|
|
323
|
+
f'<button type="submit" style="background:#17a2b8;color:#fff;padding:3px 8px;border-radius:3px;border:none;cursor:pointer;margin-right:4px;font-size:12px" title="{_("Execute now")}">'
|
|
324
|
+
f'<i class="ri-play-line"></i> {_("Run")}</button></form>'
|
|
242
325
|
)
|
|
243
326
|
if obj.is_active:
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
)
|
|
327
|
+
btn_style = "background:#ffc107;color:#000"
|
|
328
|
+
btn_title = _("Deactivate")
|
|
329
|
+
btn_icon = "ri-pause-line"
|
|
330
|
+
btn_label = _("Deactivate")
|
|
249
331
|
else:
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
332
|
+
btn_style = "background:#28a745;color:#fff"
|
|
333
|
+
btn_title = _("Activate")
|
|
334
|
+
btn_icon = "ri-play-circle-line"
|
|
335
|
+
btn_label = _("Activate")
|
|
336
|
+
buttons.append(
|
|
337
|
+
f'<form style="display:inline" method="post" action="{toggle_url}">'
|
|
338
|
+
f'<input type="hidden" name="csrfmiddlewaretoken" value={csrf_token}>'
|
|
339
|
+
f'<button type="submit" style="{btn_style};padding:3px 8px;border-radius:3px;border:none;cursor:pointer;margin-right:4px;font-size:12px" title="{btn_title}">'
|
|
340
|
+
f'<i class="{btn_icon}"></i> {btn_label}</button></form>'
|
|
341
|
+
)
|
|
255
342
|
delete_url = reverse('admin:django_simpletask5_cronjob_delete', args=[obj.pk])
|
|
256
343
|
buttons.append(
|
|
257
344
|
f'<a class="button" style="background:#dc3545;color:#fff;padding:3px 8px;border-radius:3px;text-decoration:none;margin-right:4px;font-size:12px" '
|
|
258
|
-
f'href="{delete_url}" title="
|
|
259
|
-
f'<i class="ri-delete-bin-line"></i>
|
|
345
|
+
f'href="{delete_url}" title="{_("Delete")}">'
|
|
346
|
+
f'<i class="ri-delete-bin-line"></i> {_("Delete")}</a>'
|
|
260
347
|
)
|
|
261
|
-
return
|
|
348
|
+
return mark_safe(''.join(buttons))
|
|
262
349
|
|
|
263
|
-
action_buttons.short_description = _("
|
|
264
|
-
action_buttons.allow_tags = True
|
|
350
|
+
action_buttons.short_description = _("Actions")
|
|
265
351
|
|
|
266
|
-
def
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
from django_simpletask5.management.commands.django_simpletask_crontab import Command as CrontabCommand
|
|
275
|
-
cmd = CrontabCommand()
|
|
276
|
-
cmd._trigger_cronjob(cronjob)
|
|
277
|
-
self.message_user(request, f'Cronjob "{cronjob.display_name}" triggered.')
|
|
278
|
-
elif action == 'toggle-active':
|
|
279
|
-
cronjob.is_active = not cronjob.is_active
|
|
280
|
-
cronjob.save(update_fields=['is_active'])
|
|
281
|
-
status = 'activated' if cronjob.is_active else 'deactivated'
|
|
282
|
-
self.message_user(request, f'Cronjob "{cronjob.display_name}" {status}.')
|
|
283
|
-
except CronJob.DoesNotExist:
|
|
284
|
-
self.message_user(request, f'Cronjob not found.', level='ERROR')
|
|
285
|
-
return HttpResponseRedirect(request.path)
|
|
286
|
-
return super().changelist_view(request, extra_context=extra_context)
|
|
352
|
+
def get_form(self, request, obj=None, change=False, **kwargs):
|
|
353
|
+
form_class = super().get_form(request, obj=obj, change=change, **kwargs)
|
|
354
|
+
|
|
355
|
+
class _CronJobFormWithRequest(form_class):
|
|
356
|
+
def __init__(self, *args, **kwargs):
|
|
357
|
+
kwargs['request'] = request
|
|
358
|
+
super().__init__(*args, **kwargs)
|
|
359
|
+
return _CronJobFormWithRequest
|
|
287
360
|
|
|
288
361
|
def save_model(self, request, obj, form, change):
|
|
289
362
|
if change:
|
|
@@ -344,6 +417,7 @@ class CronJobAdmin(DjangoCheckboxNormalizeAdmin, DjangoTabbedChangeformAdmin):
|
|
|
344
417
|
class TaskExecutionStatAdmin(DjangoCheckboxNormalizeAdmin, DjangoTabbedChangeformAdmin):
|
|
345
418
|
list_display = [
|
|
346
419
|
"date",
|
|
420
|
+
"executor_class",
|
|
347
421
|
"trigger_event",
|
|
348
422
|
"total_count",
|
|
349
423
|
"success_count",
|
|
@@ -351,11 +425,12 @@ class TaskExecutionStatAdmin(DjangoCheckboxNormalizeAdmin, DjangoTabbedChangefor
|
|
|
351
425
|
"avg_duration_seconds",
|
|
352
426
|
]
|
|
353
427
|
list_filter = ["date", "trigger_event"]
|
|
428
|
+
readonly_fields = ["created_at"]
|
|
354
429
|
ordering = ["-date"]
|
|
355
430
|
|
|
356
431
|
fieldsets = [
|
|
357
432
|
(None, {
|
|
358
|
-
"fields": ["date", "
|
|
433
|
+
"fields": ["date", "executor_class", "trigger_event", "created_at"],
|
|
359
434
|
"classes": ["tab-basic"],
|
|
360
435
|
}),
|
|
361
436
|
(None, {
|
|
@@ -5,8 +5,6 @@ DJANGO_SIMPLETASK_LOCK_TIMEOUT = getattr(settings, 'DJANGO_SIMPLETASK_LOCK_TIMEO
|
|
|
5
5
|
DJANGO_SIMPLETASK_DEFAULT_MAX_RETRIES = getattr(settings, 'DJANGO_SIMPLETASK_DEFAULT_MAX_RETRIES', 3)
|
|
6
6
|
DJANGO_SIMPLETASK_DEFAULT_TIMEOUT_SECONDS = getattr(settings, 'DJANGO_SIMPLETASK_DEFAULT_TIMEOUT_SECONDS', 600)
|
|
7
7
|
|
|
8
|
-
DJANGO_SIMPLETASK_UPDATE_FILTER_POLICY = getattr(settings, 'DJANGO_SIMPLETASK_UPDATE_FILTER_POLICY', 'whitelist')
|
|
9
|
-
|
|
10
8
|
DJANGO_SIMPLETASK_ARCHIVE_PATH = getattr(settings, 'DJANGO_SIMPLETASK_ARCHIVE_PATH', 'django_simpletask5_archives')
|
|
11
9
|
DJANGO_SIMPLETASK_ARCHIVE_SALT = getattr(settings, 'DJANGO_SIMPLETASK_ARCHIVE_SALT', 'django-simpletask5-archive')
|
|
12
10
|
DJANGO_SIMPLETASK_ARCHIVE_RETENTION_DAYS = getattr(settings, 'DJANGO_SIMPLETASK_ARCHIVE_RETENTION_DAYS', 7)
|
|
@@ -16,6 +14,7 @@ DJANGO_SIMPLETASK_CRONJOB_AUTO_SYNC = getattr(settings, 'DJANGO_SIMPLETASK_CRONJ
|
|
|
16
14
|
DJANGO_SIMPLETASK_DEFAULT_QUEUE = getattr(settings, 'DJANGO_SIMPLETASK_DEFAULT_QUEUE', 'django_simpletask5.queue.default')
|
|
17
15
|
DJANGO_SIMPLETASK_HIGH_PRIORITY_QUEUE = getattr(settings, 'DJANGO_SIMPLETASK_HIGH_PRIORITY_QUEUE', 'django_simpletask5.queue.high_priority')
|
|
18
16
|
|
|
17
|
+
DJANGO_SIMPLETASK_ENABLE_SCRIPT_EXECUTORS = getattr(settings, 'DJANGO_SIMPLETASK_ENABLE_SCRIPT_EXECUTORS', False)
|
|
19
18
|
DJANGO_SIMPLETASK_SCRIPT_WHITELIST = getattr(settings, 'DJANGO_SIMPLETASK_SCRIPT_WHITELIST', [])
|
|
20
19
|
|
|
21
20
|
DJANGO_SIMPLETASK_FIELD_CIPHER_CLASS = getattr(settings, 'DJANGO_SIMPLETASK_FIELD_CIPHER_CLASS', None)
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import json
|
|
2
2
|
import logging
|
|
3
|
+
import time
|
|
3
4
|
|
|
4
5
|
from django.db import transaction
|
|
6
|
+
from django.utils import timezone
|
|
5
7
|
|
|
6
8
|
from django_simpletask5.models import TaskExecution
|
|
7
9
|
from django_simpletask5.core.defaults import DJANGO_SIMPLETASK_DEFAULT_QUEUE, DJANGO_SIMPLETASK_DEFAULT_TIMEOUT_SECONDS
|
|
@@ -58,16 +60,33 @@ def _publish_message(execution, routing_key=None):
|
|
|
58
60
|
'executor_class': execution.executor_class,
|
|
59
61
|
}
|
|
60
62
|
|
|
63
|
+
from django_simpletask5.core.message_queue import get_message_queue
|
|
64
|
+
|
|
65
|
+
for attempt in range(1, 4):
|
|
66
|
+
try:
|
|
67
|
+
mq = get_message_queue()
|
|
68
|
+
mq.publish(
|
|
69
|
+
routing_key=routing_key or DJANGO_SIMPLETASK_DEFAULT_QUEUE,
|
|
70
|
+
message=message,
|
|
71
|
+
)
|
|
72
|
+
logger.debug('Published message for execution %s', execution.execution_id)
|
|
73
|
+
return
|
|
74
|
+
except Exception:
|
|
75
|
+
if attempt < 3:
|
|
76
|
+
logger.warning('Publish attempt %d/3 failed for execution %s, retrying...', attempt, execution.execution_id)
|
|
77
|
+
time.sleep(1)
|
|
78
|
+
else:
|
|
79
|
+
logger.exception('Failed to publish message for execution %s after 3 attempts', execution.execution_id)
|
|
80
|
+
|
|
61
81
|
try:
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
logger.debug('Published message for execution %s', execution.execution_id)
|
|
82
|
+
execution.refresh_from_db()
|
|
83
|
+
if execution.status not in ('success', 'failed', 'timeout'):
|
|
84
|
+
execution.status = 'failed'
|
|
85
|
+
execution.error_message = 'Failed to publish message to queue after 3 attempts'
|
|
86
|
+
execution.finished_at = timezone.now()
|
|
87
|
+
execution.save(update_fields=['status', 'error_message', 'finished_at', 'updated_at'])
|
|
69
88
|
except Exception:
|
|
70
|
-
logger.exception('Failed to
|
|
89
|
+
logger.exception('Failed to mark execution %s as failed after publish failure', execution.execution_id)
|
|
71
90
|
|
|
72
91
|
|
|
73
92
|
def _serialize_context(context):
|
{django_simpletask5-0.1.3 → django_simpletask5-0.2.0}/django_simpletask5/core/worker_registry.py
RENAMED
|
@@ -12,6 +12,7 @@ logger = logging.getLogger(__name__)
|
|
|
12
12
|
WORKER_TTL = 300
|
|
13
13
|
|
|
14
14
|
_REDIS_UNAVAILABLE = False
|
|
15
|
+
_redis_lock = threading.Lock()
|
|
15
16
|
|
|
16
17
|
|
|
17
18
|
def _get_redis_client():
|
|
@@ -30,17 +31,18 @@ def _get_redis_client():
|
|
|
30
31
|
|
|
31
32
|
def _safe_redis_call(fn, default=None, log_level=logging.WARNING):
|
|
32
33
|
global _REDIS_UNAVAILABLE
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
_REDIS_UNAVAILABLE
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
34
|
+
with _redis_lock:
|
|
35
|
+
try:
|
|
36
|
+
result = fn()
|
|
37
|
+
if _REDIS_UNAVAILABLE:
|
|
38
|
+
_REDIS_UNAVAILABLE = False
|
|
39
|
+
logger.info('Redis connection restored')
|
|
40
|
+
return result
|
|
41
|
+
except Exception as e:
|
|
42
|
+
if not _REDIS_UNAVAILABLE:
|
|
43
|
+
_REDIS_UNAVAILABLE = True
|
|
44
|
+
logger.log(log_level, 'Redis operation failed: %s', e)
|
|
45
|
+
return default
|
|
44
46
|
|
|
45
47
|
|
|
46
48
|
def _worker_key(pid, tid):
|