How to tell python consumer to wait for client to bootup

207 views Asked by At

I have a fundamental problem in a docker container where when I try to start create and start two images where the second image (python and some scripts) is dependent on the first image.

This causes the second image to error out and stop. How can I adopt my python script to consume on the client, to wait for the client boot-up?

I don't think this problem is necessarily an Apache Pulsar problem, but here's some documentation for those interested

Apache Pulsar standalone

python api docs

Consumer on Client

import pulsar

def initialize_consumer():

    client = pulsar.Client('pulsar://localhost:6650')

    consumer = client.subscribe('my-topic', 'my-subscription')

    while True:
        msg = consumer.receive()
        try:
            output_string = f"Received message {msg.data()} id={msg.message_id()}"
            print(output_string)
            with open('./output.txt', 'a') as f:
                f.write(output_string + '\n')
            # Acknowledge successful processing of the message
            consumer.acknowledge(msg)
        except:
            # Message failed to be processed
            consumer.negative_acknowledge(msg)

    client.close()
1

There are 1 answers

0
Hofbr On

This thread helped me since I had a Docker specific problem: Docker Compose wait for container X before starting Y

I essentially added a healthcheck for my standalone image and then used the restart: on-failure for my conprod image and that seems to work.

I'd still be interested in a python centered solution in the actualy consumer function above.

docker-compose.yaml file ends up looking like this:

version: '3.8'
services:
  standalone:
    hostname: standalone
    container_name: standalone
    image: apachepulsar/pulsar:2.8.1
    ports:
      - 8080:8080
      - 6650:6650
    command: bin/pulsar standalone
    healthcheck:
      test: ["CMD", "nc", "-vz", "localhost", "6650"]
      interval: 20s
      timeout: 5s
      retries: 5
    networks:
      - conprod
  conprod:
    hostname: conprod
    container_name: conprod
    build:
      context: .
      dockerfile: ./Dockerfile
    ports:
      - 80:80
    restart: on-failure
    depends_on:
      - standalone
    networks:
      - conprod
networks:
  conprod: