如需进一步参考,可以按照以下步骤归档 Argus9 的请求:
1) 你可以控制你的工人的选择是:
class YourWorker
include Sneakers::Worker
from_queue "your_queue",
:env => nil,
:ack => true,
:workers => 1, #Number of per-cpu processes to run
:prefetch => 1, #This param will define that single message will be fetched per time
:threads => 1, #This will define that you have single thread running
:heartbeat => 2,
:share_threads => true,
:timeout_job_after => 3600,
:exchange => 'your_exchange'
def work(args={})
#... your steps here
end
end
2)你需要注意你在你的sneakers.rb中指定的初始参数(因为它被Sneakers::Runner在worker初始化时使用),所以一定要在那里有正确的参数,比如:
Sneakers.configure :amqp => url,
:daemonize => true,
:ack => true,
:prefetch => 1,
:threads => 1,
:start_worker_delay => 0.1,
:workers => 1,
:exchange => "your_exchange",
:exchange_type => :direct,
:log => "log/sneakers.log"
Sneakers.logger.level = Logger::DEBUG
您还可以使用 RabbitMQ API 构建一些额外的控件,这将使您能够检查诸如是否已经有一些消息正在处理?...等等使用 bunny 归档并不容易等等。使用非常简单的代码,例如:
def queue_info
queues_infos = {}
rabbitmqctl_url = "http://127.0.0.1:15672"
rabbitmqctl_user = "your_user"
rabbitmqctl_password = "your_password"
uri = URI.parse("#{rabbitmqctl_url}/api/queues")
request = Net::HTTP::Get.new(uri)
request.basic_auth(rabbitmqctl_user, rabbitmqctl_password)
req_options = { use_ssl: uri.scheme == 'https' }
response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
http.request(request)
end
queue_details = JSON.parse(response.body)
queue_details.each do |queue|
queues_infos[queue['name'].to_s] = { name: queue['name'],
msg_total: queue['messages'],
msg_ready: queue['messages_ready'],
msg_unacknowlged: queue['messages_unacknowledged'],
state: queue['state'],
consumers: queue['consumers'] }
end
return queues_infos
end