Skip to content

Commit

Permalink
修改celery可以选择配置mq
Browse files Browse the repository at this point in the history
  • Loading branch information
woshiyanghai committed Sep 18, 2024
1 parent c509fe8 commit 2f0d0f4
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions archery/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,12 @@

environ.Env.read_env(os.path.join(BASE_DIR, ".env"))
# 定义 Redis 基本连接字符串
REDIS_BASE_URL = 'redis://127.0.0.1:6379/'
env = environ.Env(
DEBUG=(bool, False),
ALLOWED_HOSTS=(list, ["*"]),
SECRET_KEY=(str, "hfusaf2m4ot#7)fkw#di2bu6(cv0@opwmafx5n#6=3d%x^hpl6"),
DATABASE_URL=(str, "mysql://root:@127.0.0.1:3306/archery"),
CACHE_URL=(str, REDIS_BASE_URL + '0'), # 使用 Redis 数据库 0 作为缓存
CACHE_URL=(str, 'redis://127.0.0.1:6379/0'), # 使用 Redis 数据库 0 作为缓存
# 系统外部认证目前支持LDAP、OIDC、DINGDING三种,认证方式只能启用其中一种,如果启用多个,实际生效的只有一个,优先级LDAP > DINGDING > OIDC
ENABLE_LDAP=(bool, False),
ENABLE_OIDC=(bool, False),
Expand Down Expand Up @@ -225,7 +224,9 @@
**env.db(),
**{
"DEFAULT_CHARSET": "utf8mb4",
"CONN_MAX_AGE": 50,
# CONN_MAX_AGE设置为0,可以避免celery 报错mysql has gone away 问题
# https://github.com/celery/celery/pull/4292
"CONN_MAX_AGE": 0,
"OPTIONS": {
"init_command": "SET sql_mode='STRICT_TRANS_TABLES'",
"charset": "utf8mb4",
Expand Down Expand Up @@ -262,9 +263,14 @@
DEFAULT_AUTO_FIELD = "django.db.models.AutoField"

# Celery Configuration
CELERY_RESULT_BACKEND = REDIS_BASE_URL + '1' # 使用 Redis 数据库 1 存储任务结果
CELERY_RESULT_BACKEND = 'django-db'
CELERY_BROKER_URL = REDIS_BASE_URL + '2' # 使用 Redis 数据库 2 作为消息代理
'''
Celery 默认已支持 Redis、RabbitMQ、MongoDB、Django ORM、SQLAlchemy 等方式,
Redis:没有相应的机制保证消息的消费,当消费者消费失败的时候,消息体丢失,需要手动处理
RabbitMQ:具有消息消费确认,即使消费者消费失败,也会自动使消息体返回原队列,同时可全程持久化,保证消息体被正确消费
'''
CELERY_RESULT_BACKEND = 'redis://127.0.0.1:6379/1'
#CELERY_RESULT_BACKEND = 'django-db'
CELERY_BROKER_URL = 'redis://127.0.0.1:6379/2'
CELERY_BEAT_SCHEDULER = 'django_celery_beat.schedulers:DatabaseScheduler'
CELERYD_CONCURRENCY = 4
CELERY_TIMEZONE = 'UTC'
Expand Down

0 comments on commit 2f0d0f4

Please sign in to comment.