How to link Env Groups with Cron. (Blueprint)

Hi.
Cron fails.
It seems to be failing on the DB connection.

I am running Django’s Command.
I manually linked the Environment but it failed.
Please let me know how to resolve this issue.

Thanks.

render.yaml

databases:
  - name: "Slack Bot DB"
    databaseName: slack_bot_db
    user: slack_bot_user
    region: singapore

services:
  - type: web
    region: singapore
    name: "Slack Bot Service"
    env: python
    buildCommand: "./scripts/build.sh"
    startCommand: "gunicorn config.wsgi:application"
    envVars:
      - key: DATABASE_URL
        fromDatabase:
          name: "Slack Bot DB"
          property: connectionString
      - key: SECRET_KEY
        generateValue: true
      - key: WEB_CONCURRENCY
        value: 4
  - type: cron
    name: post_questions_command
    env: python
    schedule: "0 1 * * *"
    buildCommand: "poetry install"
    startCommand: "poetry run python manage.py post_questions_command"
    region: singapore

error log

Traceback (most recent call last):
Oct 1 07:01:47 PM    File "/opt/render/project/src/.venv/lib/python3.9/site-packages/django/db/backends/base/base.py", line 282, in ensure_connection
Oct 1 07:01:47 PM      self.connect()
Oct 1 07:01:47 PM    File "/opt/render/project/src/.venv/lib/python3.9/site-packages/django/utils/asyncio.py", line 26, in inner
Oct 1 07:01:47 PM      return func(*args, **kwargs)
Oct 1 07:01:47 PM    File "/opt/render/project/src/.venv/lib/python3.9/site-packages/django/db/backends/base/base.py", line 263, in connect
Oct 1 07:01:47 PM      self.connection = self.get_new_connection(conn_params)
Oct 1 07:01:47 PM    File "/opt/render/project/src/.venv/lib/python3.9/site-packages/django/utils/asyncio.py", line 26, in inner
Oct 1 07:01:47 PM      return func(*args, **kwargs)
Oct 1 07:01:47 PM    File "/opt/render/project/src/.venv/lib/python3.9/site-packages/django/db/backends/postgresql/base.py", line 215, in get_new_connection
Oct 1 07:01:47 PM      connection = Database.connect(**conn_params)
Oct 1 07:01:47 PM    File "/opt/render/project/src/.venv/lib/python3.9/site-packages/psycopg2/__init__.py", line 122, in connect
Oct 1 07:01:47 PM      conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
Oct 1 07:01:47 PM  psycopg2.OperationalError: could not translate host name "dpg-cc8lbm9gp3jhesr2rjf0-a" to address: Name or service not known
Oct 1 07:01:47 PM  
Oct 1 07:01:47 PM  
Oct 1 07:01:47 PM  The above exception was the direct cause of the following exception:
Oct 1 07:01:47 PM  
Oct 1 07:01:47 PM  Traceback (most recent call last):
Oct 1 07:01:47 PM    File "/opt/render/project/src/manage.py", line 22, in <module>
Oct 1 07:01:47 PM      main()
Oct 1 07:01:47 PM    File "/opt/render/project/src/manage.py", line 18, in main
Oct 1 07:01:47 PM      execute_from_command_line(sys.argv)
Oct 1 07:01:47 PM    File "/opt/render/project/src/.venv/lib/python3.9/site-packages/django/core/management/__init__.py", line 446, in execute_from_command_line
Oct 1 07:01:47 PM      utility.execute()
Oct 1 07:01:47 PM    File "/opt/render/project/src/.venv/lib/python3.9/site-packages/django/core/management/__init__.py", line 440, in execute
Oct 1 07:01:47 PM      self.fetch_command(subcommand).run_from_argv(self.argv)
Oct 1 07:01:47 PM    File "/opt/render/project/src/.venv/lib/python3.9/site-packages/django/core/management/base.py", line 402, in run_from_argv
Oct 1 07:01:47 PM      self.execute(*args, **cmd_options)
Oct 1 07:01:47 PM    File "/opt/render/project/src/.venv/lib/python3.9/site-packages/django/core/management/base.py", line 448, in execute
Oct 1 07:01:47 PM      output = self.handle(*args, **options)
Oct 1 07:01:47 PM    File "/opt/render/project/src/slack_bot/management/commands/post_questions_command.py", line 25, in handle
Oct 1 07:01:47 PM      for team in teams:
Oct 1 07:01:47 PM    File "/opt/render/project/src/.venv/lib/python3.9/site-packages/django/db/models/query.py", line 394, in __iter__
Oct 1 07:01:47 PM      self._fetch_all()
Oct 1 07:01:47 PM    File "/opt/render/project/src/.venv/lib/python3.9/site-packages/django/db/models/query.py", line 1866, in _fetch_all
Oct 1 07:01:47 PM      self._result_cache = list(self._iterable_class(self))
Oct 1 07:01:47 PM    File "/opt/render/project/src/.venv/lib/python3.9/site-packages/django/db/models/query.py", line 87, in __iter__
Oct 1 07:01:47 PM      results = compiler.execute_sql(
Oct 1 07:01:47 PM    File "/opt/render/project/src/.venv/lib/python3.9/site-packages/django/db/models/sql/compiler.py", line 1393, in execute_sql
Oct 1 07:01:47 PM      cursor = self.connection.cursor()
Oct 1 07:01:47 PM    File "/opt/render/project/src/.venv/lib/python3.9/site-packages/django/utils/asyncio.py", line 26, in inner
Oct 1 07:01:47 PM      return func(*args, **kwargs)
Oct 1 07:01:47 PM    File "/opt/render/project/src/.venv/lib/python3.9/site-packages/django/db/backends/base/base.py", line 323, in cursor
Oct 1 07:01:47 PM      return self._cursor()
Oct 1 07:01:47 PM    File "/opt/render/project/src/.venv/lib/python3.9/site-packages/django/db/backends/base/base.py", line 299, in _cursor
Oct 1 07:01:47 PM      self.ensure_connection()
Oct 1 07:01:47 PM    File "/opt/render/project/src/.venv/lib/python3.9/site-packages/django/utils/asyncio.py", line 26, in inner
Oct 1 07:01:47 PM      return func(*args, **kwargs)
Oct 1 07:01:47 PM    File "/opt/render/project/src/.venv/lib/python3.9/site-packages/django/db/backends/base/base.py", line 282, in ensure_connection
Oct 1 07:01:47 PM      self.connect()
Oct 1 07:01:47 PM    File "/opt/render/project/src/.venv/lib/python3.9/site-packages/django/db/utils.py", line 91, in __exit__
Oct 1 07:01:47 PM      raise dj_exc_value.with_traceback(traceback) from exc_value
Oct 1 07:01:47 PM    File "/opt/render/project/src/.venv/lib/python3.9/site-packages/django/db/backends/base/base.py", line 282, in ensure_connection
Oct 1 07:01:47 PM      self.connect()
Oct 1 07:01:47 PM    File "/opt/render/project/src/.venv/lib/python3.9/site-packages/django/utils/asyncio.py", line 26, in inner
Oct 1 07:01:47 PM      return func(*args, **kwargs)
Oct 1 07:01:47 PM    File "/opt/render/project/src/.venv/lib/python3.9/site-packages/django/db/backends/base/base.py", line 263, in connect
Oct 1 07:01:47 PM      self.connection = self.get_new_connection(conn_params)
Oct 1 07:01:47 PM    File "/opt/render/project/src/.venv/lib/python3.9/site-packages/django/utils/asyncio.py", line 26, in inner
Oct 1 07:01:47 PM      return func(*args, **kwargs)
Oct 1 07:01:47 PM    File "/opt/render/project/src/.venv/lib/python3.9/site-packages/django/db/backends/postgresql/base.py", line 215, in get_new_connection
Oct 1 07:01:47 PM      connection = Database.connect(**conn_params)
Oct 1 07:01:47 PM    File "/opt/render/project/src/.venv/lib/python3.9/site-packages/psycopg2/__init__.py", line 122, in connect
Oct 1 07:01:47 PM      conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
Oct 1 07:01:47 PM  django.db.utils.OperationalError: could not translate host name "dpg-cc8lbm9gp3jhesr2rjf0-a" to address: Name or service not known

Problem solved.
It seems I needed to use the same region as the web service.
I also found the same Issue.

databases:
  - name: "Slack Bot DB"
    databaseName: slack_bot_db
    user: slack_bot_user
    region: singapore

services:
  - type: web
    region: singapore
    name: "Slack Bot Service"
    env: python
    buildCommand: "./scripts/build.sh"
    startCommand: "gunicorn config.wsgi:application"
    envVars:
      - key: DATABASE_URL
        fromDatabase:
          name: "Slack Bot DB"
          property: connectionString
      - key: SECRET_KEY
        generateValue: true
      - key: WEB_CONCURRENCY
        value: 4
  - type: cron
    region: singapore # <- required
    name: post_questions_command
    env: python
    schedule: "0 1 * * *"
    buildCommand: "poetry install"
    startCommand: "poetry run python manage.py post_questions_command"

But it looks like Env Groups needs to be configured manually.
It would be useful to be able to configure it in yaml.

That’s right - services can’t communicate across regions and if a region is not specific it would be created in our default region.

John B

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.