sneaker 很好的把使用 bunny 和 rabbitmq 封装了起来, 而且提供了灵活的进程管理机制. 成为 rabbitmq 官方的推荐版本. 下面来看看它到底做了些什么.
首先来看看 Demo:
require 'sneakers'
require 'redis'
require 'json'
# $redis = Redis.new
class Processor
include Sneakers::Worker
from_queue :logs
def work(msg)
err = JSON.parse(msg)
if err["type"] == "error"
#$redis.incr "processor:#{err["error"]}"
puts err["error"]
end
ack!
end
end
然后通过
─> sneakers work Processor --require boot.rb
就可以启动当前这个Processor
作为一个worker
来处理 message.
然后默认会创建4个进程来监听代码里设定的queue logs
, 默认在对于 queue 的处理上,当没有对应的 queue 的时候它会负责创建.
- Sneakers::Publisher
It uses a hybrid process-thread model where many processes are spawned (like Unicorn) and many threads are used per process (like Puma), so all your cores max out and you have best of both worlds.
这个信息量有点大啊. 采用超轻量的进程-线程模式.
- 像
Unicorn
那样,复制很多的进程. - 然后像
Puma
那样,每一个进程都使用了多线程.
所有这样可以最大使用CPU;
尼玛最后跑题了啊, 跑到了进程线程哪里去了!!!
link: