This is my Flask CLI script. It awaits for messages from rabbitmq
and prints them out.
import pika
import click
from app import app_queue
from app import RABBITMQ_USER
from app import RABBITMQ_HOST
from app import RABBITMQ_PASSWORD
from app import RABBITMQ_PASSWORD
from app import RABBITMQ_PORT
from app import RABBITMQ_VHOST
from flask import current_app
from flask.cli import with_appcontext
from threading import Event
credentials = pika.PlainCredentials(RABBITMQ_USER, RABBITMQ_PASSWORD)
parameters = pika.ConnectionParameters(RABBITMQ_HOST, RABBITMQ_PORT, RABBITMQ_VHOST, credentials)
connection = pika.BlockingConnection(parameters)
channel = connection.channel()
@app_queue.command("listen", help="Listen to default queue.")
@click.argument("queue", required=False)
@with_appcontext
def consume_message(queue):
queue = queue if queue else 'results'
print(f'Command is listening for "{queue}" queue.', flush=True)
channel.queue_declare(queue, durable=True, exclusive=False)
channel.basic_consume(queue=queue, on_message_callback=process_results)
print(f'[*] Waiting for messages. from "{queue}" queue. To exit press CTRL+C', flush=True)
channel.start_consuming()
Event().wait()
def process_results(ch, method, properties, body):
print(" [x] Received %r" % body, flush=True)
ch.basic_ack(delivery_tag = method.delivery_tag)
The script is executed by supervisord
which triggers flask queue listen
command.
[program:app_rabbitmq]
user=root
numprocs=1
directory=/src
autostart=true
autorestart=true
process_name=%(program_name)s
redirect_stderr=true
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
command=flask queue listen
How can I redirect output of Flask CLI command to stdout so I can see it with docker logs <my_container>
command?
I found solution. Here are three things that I did.
Instead redirecting my log to /dev/stdout
I redirected it to /proc/1/fd/1
I started supervisord in nodaemon mode supervisord -n
I set these two environment variables in my docker image export PYTHONUNBUFFERED=True
and export PYTHONIOENCODING=UTF-8
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.