I have a base resource class
../baselayer/baseresource/resource.py
class Resource:
def __init__(self, *args, service_name: str, client_name: str = "Resource", log: Logger = None,
error_notifier: ErrorNotifier = None, **kwargs):
super().__init__(*args, **kwargs)
self.service_name: str = service_name
self.client_name: str = client_name
self.log: Logger = log
self.error_notifier: ErrorNotifier = error_notifier
And I have a utility class inherting the above class
../baselayer/kafka/producer.py
from confluent_kafka import Producer
class CustomKafkaProducer(Resource, Producer):
#custom functions
@classmethod
def get_client(cls, *, service_name, logger, error_notifier, config: KafkaCred):
config = {
"bootstrap.servers": config.BROKER,
"security.protocol": "SASL_SSL",
"sasl.mechanisms": "PLAIN",
"sasl.username": config.USERNAME,
"sasl.password": config.PASSWORD,
"client.id": f"{service_name}-{uuid4().hex}"
}
client = cls(config, log=logger, service_name=service_name, error_notifier=error_notifier)
return client
This is part of a fastapi project and the CustomKafkaProducer.get_client
is called during app setup
../tests/app.py
from dotenv import load_dotenv
print("ENV load status test", load_dotenv(".env"))
from src.app.deployment import http_server, kafka_client
gunicorn tests.app:http_server --workers 4 --worker-class uvicorn.workers.UvicornWorker --bind 0.0.0.0:3000 --log-file - --access-logfile -
With the above command I am able to run the server with Python 3.10.4
I am building the image for the code with the following docker file
FROM python:3.10.4-slim-bullseye as base
RUN apt-get update
RUN apt-get install build-essential -y
RUN python -m pip install --upgrade pip
COPY ./requirements.txt /tmp/requirements.txt
WORKDIR /myapp
RUN pip install -r /tmp/requirements.txt
COPY ./src /myapp/src
COPY ./baselayer /myapp/baselayer
EXPOSE 3000
FROM base AS http
CMD ["ddtrace-run", "gunicorn", "src.app.deployment:http_server","--workers", "4", "--worker-class", "uvicorn.workers.UvicornWorker", "--bind", "0.0.0.0:3000", "--log-file" , "-", "--access-logfile", "-"]
When running the container I get the following error
File "/myapp/src/app/deployment/ddtrace_deployment.py", line 15, in <module>
http_server = create_http_app()
File "/myapp/src/app/app.py", line 80, in create_http_app
return create_app(config=config, cls=HTTPServer)
File "/myapp/src/app/app.py", line 75, in create_app
app = cls.get_app(config, aws_session=Session())
File "/myapp/baselayer/app.py", line 104, in get_app
app.setup_app()
File "/myapp/src/app/app.py", line 49, in setup_app
super().setup_app()
File "/myapp/src/app/app.py", line 34, in setup_app
self.kafka_producer = CustomKafkaProducer.get_client(service_name=self.config.SERVICE_NAME,
File "/myapp/baselayer/kafka/producer.py", line 62, in get_client
client = cls(config, log=logger, service_name=service_name, error_notifier=error_notifier)
File "/myapp/baselayer/baseresource/resource.py", line 11, in __init__
self.service_name: str = service_name
AttributeError: 'cimpl.Producer' object has no attribute 'service_name'