简体   繁体   中英

Heroku / Redis Connection error on Python/Django Project

I'm trying to set up django so it send automatic email when a certain date in my models i reached. However i setup a Heroku-Redis server and am trying to connect to it. I created a simple task to test out if celery is working but it always returns the following error:

Error while reading from socket: (10054, 'An existing connection was forcibly closed by the remote host', None, 10054, None)

I setup celery according to the website:

celery.py:

import os

from celery import Celery

# Set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'incFleet.settings')

app = Celery('incFleet')

# Using a string here means the worker doesn't have to serialize
# the configuration object to child processes.
# - namespace='CELERY' means all celery-related configuration keys
#   should have a `CELERY_` prefix.
app.config_from_object('django.conf:settings', namespace='CELERY')

# Load task modules from all registered Django apps.
app.autodiscover_tasks()


@app.task(bind=True)
def debug_task(self):
    print(f'Request: {self.request!r}')

Tasks.py

import datetime    
from celery import shared_task, task
from time import sleep
from .models import trucks
from datetime import datetime


@shared_task()
def sleepy(duration):
    sleep(duration)
    return 0

#@shared_task()
#def send_warning():

My views:

def index(request):
    sleepy.delay(5)
    return render(request, 'Inventory/index.html')

And my settings.py

# Celery Broker - Redis
CELERY_BROKER_URL = 'redis://:p0445df1196b44ba70a9bd0c84545315fec8a5dcbd77c8e8c4bd22ff4cd0a2ff4@ec2-54-167-58-171.compute-1.amazonaws.com:11900/'
    CELERY_RESULT_BACKEND = 'redis://:p0445df1196b44ba70a9bd0c84545315fec8a5dcbd77c8e8c4bd22ff4cd0a2ff4@ec2-54-167-58-171.compute-1.amazonaws.com:11900/'
    CELERY_ACCEPT_CONTENT = ['json']
    CELERY_TASK_SERIALIZER = 'json'

Heroku recommends using encryption and thus a rediss:// URL instead of redis://

Source - https://stackoverflow.com/a/68730097/14018892

After a while i realized the issue was with the network in my office. The port i was trying to access was closed. As soon as i activated redis on the actual server it started working fine.

heroku config | grep REDIS

check the actual url using this code

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM