# -*- 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. 声明延迟交换机（插件必须启用）
channel.exchange_declare(
    exchange='delayed_exchange',
    exchange_type='x-delayed-message',
    arguments={'x-delayed-type': 'direct'}  # 交换机内部用直连
)

# 2. 从命令行读取延迟时间（毫秒）和消息
delay_ms = int(sys.argv[1]) if len(sys.argv) > 1 else 5000
message    = ' '.join(sys.argv[2:]) or f'Hello after {delay_ms} ms'

channel.basic_publish(
    exchange='delayed_exchange',
    routing_key='delayed',
    body=message,
    properties=pika.BasicProperties(
        headers={'x-delay': delay_ms}  # 关键：延迟投递时间
    )
)
print(f" [x] Sent message to be delayed {delay_ms} ms: {message}")
connection.close()