# -*- coding: utf-8 -*-
import pika
import sys

user_info = pika.PlainCredentials('user', 'J70e6K7BRrxrU1dO')
connection = pika.BlockingConnection(
    pika.ConnectionParameters('rabbitmq.rabbitmq.svc.cluster.local', 5672, '/', user_info)
)
channel = connection.channel()

# 1. 声明优先级队列，最多支持 10 个优先级
channel.queue_declare(
    queue='priority_queue',
    durable=True,
    arguments={'x-max-priority': 10}
)

# 2. 从命令行读取优先级和消息
priority = int(sys.argv[1]) if len(sys.argv) > 1 else 0
message  = ' '.join(sys.argv[2:]) or f'Message with priority {priority}'

channel.basic_publish(
    exchange='',
    routing_key='priority_queue',
    body=message,
    properties=pika.BasicProperties(
        priority=priority,   # 关键
        delivery_mode=2      # 持久化
    )
)
print(f" [x] Sent priority={priority}: {message}")
connection.close()