3

I'm currently trying to setup a Google app engine flex using a django framework with django-channels. for my current project i need a websocket, so i'm trying to reconstruct the tutorial offered on the website by Django-channels: https://channels.readthedocs.io/en/latest/tutorial/

Currently I'm stuck on adding redis to my google-app-flex instance. I followed the google documentation on setting up a redis connection - unfortunatly the example is in Flask: google doc I assume my error is trivial, and i just need to connect django CHANNEL_LAYERS to redis proporly.

executing sudo gcloud redis instances describe <redisname> --region=us-central1 gives me following responce:

Image: "Redis Describtion" Description Redis

executing sudo gcloud app describe, this responce:

Description App

I configured my app.yaml as follows:

# app.yaml
# [START runtime]
runtime: python
env: flex
entrypoint: daphne django_channels_heroku.asgi:application --port $PORT --bind 0.0.0.0

runtime_config:
  python_version: 3
automatic_scaling:
  min_num_instances: 1
  max_num_instances: 7

# Update with Redis instance IP and port
env_variables:
  REDISHOST: '<the ip in "host" from "Redis Describtion" image above>'
  REDISPORT: '6379'

# Update with Redis instance network name
network:
  name: default

# [END runtime]

..and in my settings.py i added this as the redis connection (which feels really wrong btw):

#settings.py
import redis

#settings.py stuff...


#connect to redis
redis_host = os.environ.get('REDISHOST', '127.0.0.1')
redis_port = int(os.environ.get('REDISPORT', 6379))
redis_client = redis.StrictRedis(host=redis_host, port=redis_port)

# Channels
ASGI_APPLICATION = "django_channels_heroku.routing.application"
CHANNEL_LAYERS = {
    'default': {
        'BACKEND': 'channels_redis.core.RedisChannelLayer',
        'CONFIG': {
            "hosts": [('127.0.0.1', 6379)],
        },
    },
}

what am i doing wrong. how do i connect to Redis using Django correctly?

here are some Links:

https://cloud.google.com/memorystore/docs/redis/connect-redis-instance-flex

Django, Redis: Where to put connection-code

Deploying Django channels app on google flex engine

How to connect to Redis instance (memorystore) from Google's Standard App Engine (Python 3.7)

https://cloud.google.com/memorystore/docs/redis/connect-redis-instance-flex

https://cloud.google.com/memorystore/docs/redis/quickstart-gcloud

  • Apologies on asking the question here, but were you ever able to get this fully deployed and working? I'm having trouble myself doing a similar setup of Django/Channels/Redis/Flex, and can never get the actual app to start up. – Shane Aug 15 '20 at 04:47
  • Awesome detailed question – Aseem Oct 06 '20 at 02:11
  • 1
    @Shane yes i did get is working...sorry didnt see your comment. did you solve it? – Andy Parzyszek Dec 17 '20 at 15:56
  • No worries. Indeed I did. I can't recall what my issue was, but all is running fine now. – Shane Jan 11 '21 at 19:52

1 Answers1

3

My mistake is in the settings.py:

Correct version:

#settings.py

#settings stuff...

redis_host = os.environ.get('REDISHOST', '127.0.0.1')
redis_port = int(os.environ.get('REDISPORT', 6379))
#redis_client = redis.StrictRedis(host=redis_host, port=redis_port)  #this is not needed

# Channels
ASGI_APPLICATION = "django_channels_heroku.routing.application"
CHANNEL_LAYERS = {
    'default': {
        'BACKEND': 'channels_redis.core.RedisChannelLayer',
        'CONFIG': {
            "hosts": [(redis_host, redis_port)],
        },
    },
}