Creating model in task leads cannot connect to postgres database - tries to use socket instead of network port

Creating model in task leads cannot connect to postgres database - tries to use socket instead of network port



I am at wits end. I researched for sometime, and maybe I don't know the correct keywords, but I couldn't find a solution that worked.



I have dockerized a Django app (custom link shortener) with a Postgres backend that uses Celery, RabbitMQ for a single task - to create and save an object to the database when a GET request is made.



The idea is this: when a shortlink is requested (GET request), the app simply redirects the request to another URL, but also creates a task using Celery. That task is supposed to add an analytics record to the Postgres database. The redirect works fine. I can create new shortlinks just fine. The django app connects to the postgres container fine. But Celery, in a different container tries to connect upon creating and saving the new object to the postgres container via a socket. This is the main error:


celery | django.db.utils.OperationalError: could not connect to server: No such file or directory
celery | Is the server running locally and accepting
celery | connections on Unix domain socket "/tmp/.s.PGSQL.5432"?



The following are what I think is the relevant, code, but if I didn't include something, please let me know. Thank you.



docker-compose.yml:


version: '3.6'

services:

pgdb:
container_name: postgres
restart: unless-stopped
environment:
- POSTGRES_DB=postgres
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=password
image: postgres:10-alpine
ports:
- 5432:5432
volumes:
- postgres_data:/var/lib/postgres/data
networks:
- net1

rabbitmq:
container_name: rabbitmq
restart: always
image: rabbitmq:3.7-alpine
environment:
- RABBITMQ_DEFAULT_USER=slinky
- RABBITMQ_DEFAULT_PASS=pwd
depends_on:
- pgdb
networks:
- net1
ports:
- 5672:5672

django:
container_name: django
restart: unless-stopped
environment:
- DB_NAME=postgres
- DB_USER=postgres
- DB_PASSWORD=password
- DB_HOST=pgdb
- DB_PORT=5432
build:
context: .
dockerfile: docker/django.dockerfile
command: sh -c "/usr/local/bin/gunicorn slinky.wsgi -w 2 -b 0.0.0.0:8000 --log-file gunicorn.log"
volumes:
- .:/code
- /tmp
networks:
- net1
ports:
- 8000:8000
depends_on:
- pgdb
- rabbitmq

celery:
container_name: celery
restart: unless-stopped
build:
context: .
dockerfile: docker/django.dockerfile
command: celery -A slinky worker -l info
volumes:
- .:/code
depends_on:
- rabbitmq
networks:
- net1

nginx:
container_name: nginx
restart: unless-stopped
build:
context: .
dockerfile: docker/nginx.dockerfile
volumes:
- .:/code
ports:
- 80:80
networks:
- net1
depends_on:
- django

volumes:
postgres_data:

networks:
net1:
driver: bridge
name: net1



tasks.py:


from __future__ import absolute_import, unicode_literals
from celery import shared_task
from django.conf import settings
from django.db import models
from .utils import ip2country


@shared_task
def redirect_event(url, slink, label, ip):
country = "Switzerland" # ip2country(ip)
print("Saving redirect event object: ", country)
obj = RedirectEvent(url=url, slink=slink, label=label, country=country)
obj.save()



utils.py:


from django.conf import settings
from django.db import models

import random
import string
import requests

SLINK_LENGTH = getattr(settings, "SLINK_LENGTH", 6)

def code_gen(size=SLINK_LENGTH, chars = string.ascii_lowercase + string.digits):
return ''.join(random.choice(chars) for _ in range(size))

def create_code(instance, size=SLINK_LENGTH):
new_code = code_gen(size=size)
Klass = instance.__class__
qs_exists = Klass.objects.filter(shortcode=new_code).exists()
if qs_exists:
return create_code(size=size)
return new_code

def ip2country(ip):
url = "http://api.ipaddress.com/iptocountry?format=json&ip=" + ip
print("IP2 country API call: ", url)
r = requests.get(url)
data = r.json()
return data['country_name']



views.py:


from django.conf import settings
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render, get_object_or_404
from django.views import View
from .models import Slink, SlinkBaseDomain, RedirectEvent
from shortener.tasks import redirect_event

SLINK_BASE = getattr(settings, "SLINK_BASE", "www.meydan.tv")

class SlinkRedirectView(View):
def get(self, request, slink=None, *args, **kwargs):
obj = get_object_or_404(Slink, shortcode=slink)
latest_domain_obj = SlinkBaseDomain.objects.latest()
latest_domain = latest_domain_obj.domain
url = latest_domain + obj.url
ip = request.META['REMOTE_ADDR']

# celery task (delayed job)
redirect_event.delay(obj.url, slink, obj.label, ip)

print("================= Redirecting user to external URL: ", url)
return HttpResponseRedirect(url)



from app models.py:


class RedirectEvent(models.Model):
url = models.CharField(max_length=255)
slink = models.CharField(max_length=SLINK_LENGTH)
country = models.CharField(max_length=255, blank=True)
label = models.CharField(max_length=25, blank=True)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)

def __str__(self):
return str(self.slink)

class Meta:
verbose_name = "Redirect Analytic"






Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Some of your past answers have not been well-received, and you're in danger of being blocked from answering.



Please pay close attention to the following guidance:



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)