Powershell脚本为所有vhost删除所有RabbitMQ队列

wko9yo5t  于 5个月前  发布在  RabbitMQ
关注(0)|答案(1)|浏览(77)

我正在运行测试,它会用消息填充RabbitMQ队列。我有多个vhost,每个主机都有多个队列,其中包含大量消息。我需要一个Powershell脚本,它可以简单地清除所有vhost的所有队列中的所有消息。已经有一个script for Python that purges all queues for a single vhost,但我想使用powershell,我想清除所有vhost的所有队列。
我不想删除队列或vhost,我只想清除队列中的所有消息。我使用的是RabbitMQ版本3.12.10。
有人有这样的剧本吗?

vulvrdjw

vulvrdjw1#

我创建了一个基于on the 'rabbitmqctl' documentation.的powershell脚本该脚本使用3个rabbitmqctl命令:

  1. rabbitmqctl list_vhosts > vhosts.txt获取所有vhost的列表并将列表存储到文件中。
  2. rabbitmqctl list_queues -p $vhost > queues.txt获取给定vhost的所有队列的列表,并将队列名称存储到文件中。
  3. rabbitmqctl purge_queue -p $vhost $queueName它为给定的vhost清除给定的队列。
    记得将RabbitMQ bin目录添加到您的环境Path变量,以便在powershell终端中启用rabbitmqctl。该脚本会过滤掉一些通用的JavaScript输出,因此根据您使用的RabbitMQ版本和您的环境,您可能需要修改该脚本才能为您工作。
    下面是脚本:
rabbitmqctl list_vhosts > vhosts.txt
foreach($vhost in Get-Content .\vhosts.txt) {
    if ($vhost.StartsWith("Listing vhosts") -or $vhost.StartsWith("name")) {    # skipping the generic program output
        continue;
    }
    Write-Host "Checking vhost $($vhost) for queues"
    rabbitmqctl list_queues -p $vhost > queues.txt

    foreach ($queueLine in Get-Content .\queues.txt) {
        if ($queueLine.StartsWith("Timeout: ") -or $queueLine.StartsWith("Listing queues") -or $queueLine.StartsWith("name")) { # skipping the generic program output
            continue;
        }
        $split = $queueLine -split "\s+"    # splitting string on both space and tab
        $queueName = $split[0]
        $messageCount = $split[1]
        if ($messageCount -eq 0 ) {
            continue;
        }
        rabbitmqctl purge_queue -p $vhost $queueName
    }
}
Remove-Item "vhosts.txt"
Remove-Item "queues.txt"

字符串

相关问题