Django BaseCommand 在本地工作但不在 Amazon AWS EB 上工作
Django BaseCommand works locally but does not work on Amazon AWS EB
所以,我正在使用 Amazon AWS EB + RDS 数据库部署 Django 网站。
除了我负责以编程方式填充数据库的脚本外,一切正常。
按照这个非常简短的教程,我设法在本地 运行ning 获得了它:
http://eli.thegreenplace.net/2014/02/15/programmatically-populating-a-django-database
所以在本地它按预期工作。
当我在我的亚马逊 AWS EB 上 运行 这个脚本时,我的问题就来了(无论是使用 cron 还是手动激活我的环境)。
我收到此错误消息:
'''
Traceback (most recent call last):
File "/opt/python/run/venv/local/lib/python3.4/site-packages/django/db/backends/base/base.py", line 213, in ensure_connection
self.connect()
File "/opt/python/run/venv/local/lib/python3.4/site-packages/django/db/backends/base/base.py", line 189, in connect
self.connection = self.get_new_connection(conn_params)
File "/opt/python/run/venv/local/lib/python3.4/site-packages/django/db/backends/postgresql/base.py", line 176, in get_new_connection
connection = Database.connect(**conn_params)
File "/opt/python/run/venv/local/lib64/python3.4/site-packages/psycopg2/__init__.py", line 130, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError: could not connect to server: Connection refused
Is the server running on host "localhost" (127.0.0.1) and accepting
TCP/IP connections on port 5432?
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/opt/python/current/app/BetzCenter/manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "/opt/python/run/venv/local/lib/python3.4/site-packages/django/core/management/__init__.py", line 363, in execute_from_command_line
utility.execute()
File "/opt/python/run/venv/local/lib/python3.4/site-packages/django/core/management/__init__.py", line 355, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/opt/python/run/venv/local/lib/python3.4/site-packages/django/core/management/base.py", line 283, in run_from_argv
self.execute(*args, **cmd_options)
File "/opt/python/run/venv/local/lib/python3.4/site-packages/django/core/management/base.py", line 330, in execute
output = self.handle(*args, **options)
File "/opt/python/bundle/18/app/BetzCenter/gamestream/management/commands/odds.py", line 444, in handle
self.create_gamepages(self.all_fixtures())
File "/opt/python/bundle/18/app/BetzCenter/gamestream/management/commands/odds.py", line 433, in create_gamepages
for fixture in fixtures:
File "/opt/python/run/venv/local/lib/python3.4/site-packages/django/db/models/query.py", line 250, in __iter__
self._fetch_all()
File "/opt/python/run/venv/local/lib/python3.4/site-packages/django/db/models/query.py", line 1118, in _fetch_all
self._result_cache = list(self._iterable_class(self))
File "/opt/python/run/venv/local/lib/python3.4/site-packages/django/db/models/query.py", line 53, in __iter__
results = compiler.execute_sql(chunked_fetch=self.chunked_fetch)
File "/opt/python/run/venv/local/lib/python3.4/site-packages/django/db/models/sql/compiler.py", line 882, in execute_sql
cursor = self.connection.cursor()
File "/opt/python/run/venv/local/lib/python3.4/site-packages/django/db/backends/base/base.py", line 254, in cursor
return self._cursor()
File "/opt/python/run/venv/local/lib/python3.4/site-packages/django/db/backends/base/base.py", line 229, in _cursor
self.ensure_connection()
File "/opt/python/run/venv/local/lib/python3.4/site-packages/django/db/backends/base/base.py", line 213, in ensure_connection
self.connect()
File "/opt/python/run/venv/local/lib/python3.4/site-packages/django/db/utils.py", line 94, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/opt/python/run/venv/local/lib/python3.4/site-packages/django/utils/six.py", line 685, in reraise
raise value.with_traceback(tb)
File "/opt/python/run/venv/local/lib/python3.4/site-packages/django/db/backends/base/base.py", line 213, in ensure_connection
self.connect()
File "/opt/python/run/venv/local/lib/python3.4/site-packages/django/db/backends/base/base.py", line 189, in connect
self.connection = self.get_new_connection(conn_params)
File "/opt/python/run/venv/local/lib/python3.4/site-packages/django/db/backends/postgresql/base.py", line 176, in get_new_connection
connection = Database.connect(**conn_params)
File "/opt/python/run/venv/local/lib64/python3.4/site-packages/psycopg2/__init__.py", line 130, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
django.db.utils.OperationalError: could not connect to server: Connection refused
Is the server running on host "localhost" (127.0.0.1) and accepting
TCP/IP connections on port 5432?
'''
如果我手动使用带有 sql 语句的 psycopg2 代码,脚本 运行 很好(在 AWS EB 上),但未能使用我认为我添加的 Django 设置已链接。
虽然我真的希望能够为这个脚本使用 django 命令。
关于如何调试它有什么想法吗?
谷歌搜索目前对我帮助不大。
更新 --> 我很确定我的 "normal" python 脚本没有指向正确的数据库。事实上,我现在已经尝试打印它指向的数据库,我是对的。它不指向我的亚马逊 RDS 数据库(但寻找本地数据库),而我的 Django 应用程序的其余部分指向它。我已经尝试了在 Whosebug 上到处找到的解决方案。
我应该从哪里开始调试呢? ;)
谢谢!
维托里奥
所以几个月后我又回到了这里并设法对其进行了排序。
我是个变数。
经过一些研究并尝试了一些解决方案后,这对我有用:
我在 crontab 文件中添加了这个,我在其中调用命令(在激活虚拟环境之后和 运行 命令本身之前:
&& source /opt/python/current/env
如果我理解正确,这是特定于 AWS elastick beanstalk 的,并且传递了所有正确的变量,这样我的命令就不会失败。
整个 crontab 行现在看起来像:
0,30 * * * * source /opt/python/run/venv/bin/activate && source /opt/python/current/env && python /opt/python/current/app/my_site/manage.py my_command
希望对您有所帮助。
所以,我正在使用 Amazon AWS EB + RDS 数据库部署 Django 网站。
除了我负责以编程方式填充数据库的脚本外,一切正常。 按照这个非常简短的教程,我设法在本地 运行ning 获得了它: http://eli.thegreenplace.net/2014/02/15/programmatically-populating-a-django-database 所以在本地它按预期工作。
当我在我的亚马逊 AWS EB 上 运行 这个脚本时,我的问题就来了(无论是使用 cron 还是手动激活我的环境)。
我收到此错误消息:
'''
Traceback (most recent call last):
File "/opt/python/run/venv/local/lib/python3.4/site-packages/django/db/backends/base/base.py", line 213, in ensure_connection
self.connect()
File "/opt/python/run/venv/local/lib/python3.4/site-packages/django/db/backends/base/base.py", line 189, in connect
self.connection = self.get_new_connection(conn_params)
File "/opt/python/run/venv/local/lib/python3.4/site-packages/django/db/backends/postgresql/base.py", line 176, in get_new_connection
connection = Database.connect(**conn_params)
File "/opt/python/run/venv/local/lib64/python3.4/site-packages/psycopg2/__init__.py", line 130, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError: could not connect to server: Connection refused
Is the server running on host "localhost" (127.0.0.1) and accepting
TCP/IP connections on port 5432?
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/opt/python/current/app/BetzCenter/manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "/opt/python/run/venv/local/lib/python3.4/site-packages/django/core/management/__init__.py", line 363, in execute_from_command_line
utility.execute()
File "/opt/python/run/venv/local/lib/python3.4/site-packages/django/core/management/__init__.py", line 355, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/opt/python/run/venv/local/lib/python3.4/site-packages/django/core/management/base.py", line 283, in run_from_argv
self.execute(*args, **cmd_options)
File "/opt/python/run/venv/local/lib/python3.4/site-packages/django/core/management/base.py", line 330, in execute
output = self.handle(*args, **options)
File "/opt/python/bundle/18/app/BetzCenter/gamestream/management/commands/odds.py", line 444, in handle
self.create_gamepages(self.all_fixtures())
File "/opt/python/bundle/18/app/BetzCenter/gamestream/management/commands/odds.py", line 433, in create_gamepages
for fixture in fixtures:
File "/opt/python/run/venv/local/lib/python3.4/site-packages/django/db/models/query.py", line 250, in __iter__
self._fetch_all()
File "/opt/python/run/venv/local/lib/python3.4/site-packages/django/db/models/query.py", line 1118, in _fetch_all
self._result_cache = list(self._iterable_class(self))
File "/opt/python/run/venv/local/lib/python3.4/site-packages/django/db/models/query.py", line 53, in __iter__
results = compiler.execute_sql(chunked_fetch=self.chunked_fetch)
File "/opt/python/run/venv/local/lib/python3.4/site-packages/django/db/models/sql/compiler.py", line 882, in execute_sql
cursor = self.connection.cursor()
File "/opt/python/run/venv/local/lib/python3.4/site-packages/django/db/backends/base/base.py", line 254, in cursor
return self._cursor()
File "/opt/python/run/venv/local/lib/python3.4/site-packages/django/db/backends/base/base.py", line 229, in _cursor
self.ensure_connection()
File "/opt/python/run/venv/local/lib/python3.4/site-packages/django/db/backends/base/base.py", line 213, in ensure_connection
self.connect()
File "/opt/python/run/venv/local/lib/python3.4/site-packages/django/db/utils.py", line 94, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/opt/python/run/venv/local/lib/python3.4/site-packages/django/utils/six.py", line 685, in reraise
raise value.with_traceback(tb)
File "/opt/python/run/venv/local/lib/python3.4/site-packages/django/db/backends/base/base.py", line 213, in ensure_connection
self.connect()
File "/opt/python/run/venv/local/lib/python3.4/site-packages/django/db/backends/base/base.py", line 189, in connect
self.connection = self.get_new_connection(conn_params)
File "/opt/python/run/venv/local/lib/python3.4/site-packages/django/db/backends/postgresql/base.py", line 176, in get_new_connection
connection = Database.connect(**conn_params)
File "/opt/python/run/venv/local/lib64/python3.4/site-packages/psycopg2/__init__.py", line 130, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
django.db.utils.OperationalError: could not connect to server: Connection refused
Is the server running on host "localhost" (127.0.0.1) and accepting
TCP/IP connections on port 5432?
'''
如果我手动使用带有 sql 语句的 psycopg2 代码,脚本 运行 很好(在 AWS EB 上),但未能使用我认为我添加的 Django 设置已链接。
虽然我真的希望能够为这个脚本使用 django 命令。
关于如何调试它有什么想法吗? 谷歌搜索目前对我帮助不大。
更新 --> 我很确定我的 "normal" python 脚本没有指向正确的数据库。事实上,我现在已经尝试打印它指向的数据库,我是对的。它不指向我的亚马逊 RDS 数据库(但寻找本地数据库),而我的 Django 应用程序的其余部分指向它。我已经尝试了在 Whosebug 上到处找到的解决方案。 我应该从哪里开始调试呢? ;)
谢谢! 维托里奥
所以几个月后我又回到了这里并设法对其进行了排序。 我是个变数。 经过一些研究并尝试了一些解决方案后,这对我有用:
我在 crontab 文件中添加了这个,我在其中调用命令(在激活虚拟环境之后和 运行 命令本身之前:
&& source /opt/python/current/env
如果我理解正确,这是特定于 AWS elastick beanstalk 的,并且传递了所有正确的变量,这样我的命令就不会失败。 整个 crontab 行现在看起来像:
0,30 * * * * source /opt/python/run/venv/bin/activate && source /opt/python/current/env && python /opt/python/current/app/my_site/manage.py my_command
希望对您有所帮助。