我有一个 Django 项目,我在其中使用 Redis/Celery 来调用一些 API。我已经设置了 settings.py 文件来连接到 Postgres 数据库,并且在本地运行 Django 项目(在本地运行 Redis/Celery)时,数据存储在数据库中没有问题。当我将应用程序推送到 Heroku 时,数据不会保存到数据库中。
我不确定这听起来像是代码本身的问题,还是导致此问题的服务器设置(Heroku 上的 Redis/Celery)。
我正在使用 Heroku Postgres 数据库。
设置:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': '',
'USER': '',
'PASSWORD': '',
'HOST': '',
'PORT': 5432,
}
}
db_from_env = dj_database_url.config(conn_max_age=600)
DATABASES['default'].update(db_from_env)
# The URL of the broker for celery to use
CELERY_BROKER_URL = os.getenv('REDIS_URL', 'redis://localhost:6379/0')
# The URL of the backend you want to use in case you wish to store the result of a task
CELERY_RESULT_BACKEND = os.getenv('REDIS_URL', 'redis://localhost:6379/0')
# The tasks you need to schedule
CELERY_BEAT_SCHEDULE = {
'add_data': {
'task': 'comparasion.tasks.add_data_util', # Name of task
'schedule': 60.0 # Time in seconds
}
}
还有 procfile:
release: python manage.py migrate
web: gunicorn Soccer.wsgi --log-file -
worker: celery -A Soccer worker --loglevel=info
beat: celery -A Soccer beat --loglevel=info
不确定提供的代码是否足以帮助解决此问题。
我已经在本地进行了测试,一切正常,但是一旦你推送到heroku,就不会保存数据。
提供的设置示例代码片段展示了 localhost 的配置,因此,一种解决方案可能是在设置中添加一个 boolean flag 以自动在 localhost 和 Production 实例之间切换。
使用 Heroku 站点中的示例代码片段,下面是 django 应用程序的演示,该应用程序在本地执行和在 Heroku 实例上运行时切换其数据库实例和主机。
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/5.0/howto/deployment/checklist/
# The `DYNO` env var is set on Heroku CI, but it's not a real Heroku app, so we
# have to also explicitly exclude CI:
# https://devcenter.heroku.com/articles/heroku-ci#immutable-environment-
# variables
IS_HEROKU_APP = "DYNO" in os.environ and not "CI" in os.environ
# SECURITY WARNING: don't run with debug turned on in production!
if not IS_HEROKU_APP:
DEBUG = True
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = os.environ.get(
"DJANGO_SECRET_KEY",
default=secrets.token_urlsafe(nbytes=64),
)
# SECURITY WARNING: don't run with debug turned on in production!
# On Heroku, it's safe to use a wildcard for `ALLOWED_HOSTS``, since the Heroku
# router performs validation of the Host header in the incoming HTTP request.
# On other platforms you may need to list the expected hostnames explicitly in
# production to prevent HTTP Host header attacks. See:
# https://docs.djangoproject.com/en/5.0/ref/settings/#std-setting-ALLOWED_HOSTS
if IS_HEROKU_APP:
ALLOWED_HOSTS = ["*"]
else:
ALLOWED_HOSTS = [".localhost", "127.0.0.1", "[::1]", "0.0.0.0"]
# Database
# https://docs.djangoproject.com/en/5.0/ref/settings/#databases
if IS_HEROKU_APP:
# In production on Heroku the database configuration is derived from the
# `DATABASE_URL`
# environment variable by the dj-database-url package. `DATABASE_URL` will
# be set
# automatically by Heroku when a database addon is attached to your Heroku
# app. See:
# https://devcenter.heroku.com/articles/provisioning-heroku postgres
# application-config-vars
# https://github.com/jazzband/dj-database-url
DATABASES = {
"default": dj_database_url.config(
env=os.environ.get("DATABASE_URL"),
conn_max_age=600,
conn_health_checks=True,
ssl_require=True,
),
}
else:
# When running locally in development or in CI, a sqlite database file will
# be used instead
# to simplify initial setup. Longer term it's recommended to use Postgres
# locally too.
DATABASES = {
"default": env.dj_db_url("DATABASE_URL_LOCAL")
}