Django BaseCommand在本地工作但不适用于Amazon AWS EB

时间:2017-08-17 18:04:44

标签: python django amazon-web-services elastic-beanstalk amazon-rds

因此,我正在使用Amazon AWS EB + RDS数据库部署Django网站。

除了我负责以编程方式填充数据库的脚本外,一切正常。 我设法让它在本地运行这个非常好的简短教程: http://eli.thegreenplace.net/2014/02/15/programmatically-populating-a-django-database 所以在本地它按预期工作。

当我在我的亚马逊AWS EB上运行此脚本时,我的问题就出现了(无论是使用cron还是手动激活我的env)。

我收到此错误消息:

'''
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?
'''

如果我手动将psycopg2代码与sql语句一起使用,脚本运行正常(在AWS EB上),但是无法使用我认为我在我链接的教程之后添加应用的Django设置。

我真的希望能够为这个脚本使用django命令。

有关如何调试此问题的任何想法? 谷歌搜索目前还没有给我太多帮助。

更新 - &gt;我非常肯定我的#34;正常&#34; python脚本没有指向正确的DB。事实上,我现在已经尝试打印它指向的数据库,我是对的。它没有指向我的亚马逊RDS数据库(但寻找本地的),而我的Django应用程序的其余部分。我已经尝试过在stackoverflow上找到的解决方案。 我应该在哪里开始调试呢? ;)

谢谢! 维托里奥

1 个答案:

答案 0 :(得分:0)

所以几个月后我又回到了这个位置并设法对它进行排序。 我是一个变数问题。 经过一些研究并尝试了一些解决方案后,这对我有用:

我在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

希望这可以提供帮助。