使用Ruby PubNub异步订阅通道示例实现无限循环的正确方法是什么?

使用Ruby PubNub异步订阅通道示例实现无限循环的正确方法是什么?,ruby,pubnub,fluentd,Ruby,Pubnub,Fluentd,我有一些Ruby代码订阅PubNub,并将从通道读取的内容转发到Fluentd。我周期性地看到一些重复的消息以Fluentd的形式出现。我怀疑在下面的代码中使用无限循环+睡眠有问题。实现这一目标更正确的方法是什么?非常感谢 #!/usr/bin/ruby require 'pubnub' require 'fluent-logger' class Subscribe # spin loop delay DELAY = 0.5 def initialize(subkey)

我有一些Ruby代码订阅PubNub,并将从通道读取的内容转发到Fluentd。我周期性地看到一些重复的消息以Fluentd的形式出现。我怀疑在下面的代码中使用无限循环+睡眠有问题。实现这一目标更正确的方法是什么?非常感谢

#!/usr/bin/ruby

require 'pubnub'
require 'fluent-logger'

class Subscribe
  # spin loop delay
  DELAY = 0.5
  def initialize(subkey)
    @subkey = subkey
    # setup logger
    @log = Fluent::Logger::FluentLogger.new(nil, :host=>'localhost', :port=>24224)
  end
  def to_channel(channel)
    pubnub = Pubnub.new(
      subscribe_key: @subkey
    )
    pubnub.subscribe(channel: channel) do |envelope|
      #puts envelope.message
      @log.post("some-tag", envelope.message)
    end
    # never end
    loop do
      sleep(DELAY)
    end
  end
end

args = Hash[ ARGV.join(' ').scan(/--?([^=\s]+)(?:=(\S+))?/) ]
channel = args['channel']

if channel and channel != ""
  sub  = Subscribe.new('sub-key-here')
  sub.to_channel(channel)
else
  puts "usage: #{$PROGRAM_NAME} --channel=<channel name>"
end
#/usr/bin/ruby
需要“pubnub”
需要“流畅的记录器”
班级订阅
#自旋环延迟
延迟=0.5
def初始化(子键)
@子键=子键
#设置记录器
@log=Fluent::Logger::FluentLogger.new(nil,:host=>localhost',:port=>24224)
结束
def至_通道(通道)
pubnub=pubnub.new(
订阅密钥:@子密钥
)
pubnub.subscribe(频道:频道)do |信封|
#把信封放进去
@log.post(“一些标签”,信封.消息)
结束
#永无止境
环道
睡眠(延迟)
结束
结束
结束
args=Hash[ARGV.join(“”).scan(/-?([^=\s]+)(?:=(\s+)))/)
通道=args['channel']
如果频道和频道!=""
sub=Subscribe.new('sub-key-here')
sub.to_通道(通道)
其他的
放置“用法:#{$PROGRAM_NAME}--channel=”
结束

看起来你的循环除了睡眠什么都不做。这就是你的循环应该做的吗?是的,但我想知道这是否与PubNub代码使用的EventMachine配合得很好。看起来你的循环除了睡眠什么都不做。这就是您的循环应该做的所有事情吗?是的,但我想知道这是否与PubNub代码使用的EventMachine配合得很好。