django - Celery - Django - '“结果”列中的空值违反非空约束'
问题描述
我正在使用 celery 对不同的任务进行排队的 django 项目。大多数情况下,我们使用它来发送邮件,并且我们目前有很多任务正在使用相同的方法。问题是我在生产时遇到了错误,因为突然 celery 没有在 Task 模型的结果字段上保存任何值。希望这些信息足够,如果没有,请询问。这是回溯:
Task users.tasks.send_reset_password_task with id cf6ddab4-4407-477e-80e5-a5e50fbe4a90 raised exception:
'IntegrityError(\'null value in column "result" violates not-null constraint\\nDETAIL: Failing row contains (524, 2018-09-10 13:26:13.528034+00, {"errors": [{"error_code": 0, "error_data": {"user_id": 3298, "e..., 3298, 59, null, null).\\n\',)'
Task was called with args: [272, [3298]] kwargs: {u'_schema_name': u'tgs'}.
The contents of the full traceback was:
Traceback (most recent call last):
File "/app/.heroku/python/lib/python2.7/site-packages/celery/app/trace.py", line 240, in trace_task
R = retval = fun(*args, **kwargs)
File "/app/.heroku/python/lib/python2.7/site-packages/celery/app/trace.py", line 438, in __protected_call__
return self.run(*args, **kwargs)
File "/app/users/tasks.py", line 618, in send_reset_password_task
log_event(user=user, event_type=EventLogTypes.RESET_PASSWORD_EMAIL_SENT, success=success, extra={'errors': errors})
File "/app/lmsplatform/services_eventlog.py", line 937, in log_event
campaign_id=campaign_id,
File "/app/.heroku/python/lib/python2.7/site-packages/django/db/models/manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/app/.heroku/python/lib/python2.7/site-packages/django/db/models/query.py", line 399, in create
obj.save(force_insert=True, using=self.db)
File "/app/.heroku/python/lib/python2.7/site-packages/django/db/models/base.py", line 796, in save
force_update=force_update, update_fields=update_fields)
File "/app/.heroku/python/lib/python2.7/site-packages/django/db/models/base.py", line 824, in save_base
updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File "/app/.heroku/python/lib/python2.7/site-packages/django/db/models/base.py", line 908, in _save_table
result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
File "/app/.heroku/python/lib/python2.7/site-packages/django/db/models/base.py", line 947, in _do_insert
using=using, raw=raw)
File "/app/.heroku/python/lib/python2.7/site-packages/django/db/models/manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/app/.heroku/python/lib/python2.7/site-packages/django/db/models/query.py", line 1045, in _insert
return query.get_compiler(using=using).execute_sql(return_id)
File "/app/.heroku/python/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 1054, in execute_sql
cursor.execute(sql, params)
File "/app/.heroku/python/lib/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/app/.heroku/python/lib/python2.7/site-packages/django/db/utils.py", line 94, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/app/.heroku/python/lib/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
IntegrityError: null value in column "result" violates not-null constraint
DETAIL: Failing row contains (524, 2018-09-10 13:26:13.528034+00, {"errors": [{"error_code": 0, "error_data": {"user_id": 3298, "e..., 3298, 59, null, null).
运行此任务时:
@app.task
def send_reset_password_task(tenant_id, users):
tenant = Service.get_tenant_by_id(tenant_id)
tenant_config = Service.get_tenant_config(tenant)
message_list = []
errors = []
success = None
mail_connection = Service.create_mail_connection()
with schema_context(tenant.schema_name):
for user_id in users:
user = User.objects.get(id=user_id)
# Only if authentication method is PLATFORM or not PLATFORM and user is staff
if tenant_config.users_authentication_method == 'PLATFORM' or (tenant_config.users_authentication_method != 'PLATFORM' and user.is_staff):
tenant_domain = Service.get_tenant_domain(tenant.schema_name, protocol='https')
uid = urlsafe_base64_encode(force_bytes(user.pk))
token = default_token_generator.make_token(user)
# Activate the language for this thread only
translation.activate(user.userprofile.get_default_language())
# Email subject *must not* contain newlines
from_email = '%s <%s>' % (tenant.name, 'info@email.com')
subject = _(u'%(first_name)s Recupera tu contraseña') % {'first_name': user.first_name}
content_html = SimpleTemplateResponse("emails/reset_password.html", {
'user_name': user.username,
'user_first_name': user.first_name,
'tenant_domain': tenant_domain,
'uid': uid,
'token': token,
'tenant': tenant,
'lang': user.userprofile.get_default_language(),
}, content_type="text/html", charset="utf-8").render().content
msg = mail.EmailMessage(subject, content_html, from_email, [user.email], connection=mail_connection)
msg.content_subtype = 'html'
message_list.append(msg)
try:
mail_connection.open()
success = mail_connection.send_messages([msg])
except Exception as e:
error = {'error_code': ErrorCodes.GENERAL_ERROR, 'error_data': {'user_id': user.id, 'error_detail': e.message}}
errors.append(error)
# Log mail result
with schema_context(tenant.schema_name):
log_event(user=user, event_type=EventLogTypes.RESET_PASSWORD_EMAIL_SENT, success=success, extra={'errors': errors})
此任务正常运行,现在我在运行此任务时遇到此错误。我找不到错误在哪里,我知道我没有在任务模型上设置结果字段,但它应该由 celery 本身自动生成。
解决方案
推荐阅读
- blazor - 如何在 Microsoft Blazor 应用中使用 Google Cast API?
- javascript - 在 Raspberry pi2 上启动 Electron 时出错
- r - R/dplyr 获取运行最大值
- visual-studio - VS创建的Nuget包不更新版本
- java - 在 Docker 容器(OS-Mac M1)中安装 Keyclock 时出现错误
- php - 必需的属性在codeigniter php中不起作用
- flutter - 颤振错误:(动态)=> Null 不是(字符串,动态)的子类型
- java - gradle 项目(由 IntelliJ IDEA 2021 导入)无法从 webapp 文件夹中识别 jar
- bash - 使用 bash 删除 CSF“请勿删除”IP 条目
- javascript - 从 iOS 应用程序调用函数时,Firebase HTTP 云函数给出 401 错误