lib/ost.rb in ost-0.0.3 vs lib/ost.rb in ost-0.1.0

- old
+ new

@@ -1,47 +1,43 @@ require "nest" module Ost - VERSION = "0.0.3" - TIMEOUT = ENV["OST_TIMEOUT"] || 2 + VERSION = "0.1.0" + TIMEOUT = ENV["OST_TIMEOUT"] || 0 class Queue - attr :ns + attr :key + attr :backup def initialize(name) - @ns = Nest.new(:ost)[name] + @key = Nest.new(:ost)[name] + @backup = @key[Socket.gethostname][Process.pid] end def push(value) - redis.lpush(ns, value) + key.lpush(value) end def each(&block) @stopping = false loop do break if @stopping - _, item = redis.brpop(ns, TIMEOUT) - next if item.nil? or item.empty? + item = @key.brpoplpush(@backup, TIMEOUT) - begin - block.call(item) - rescue Exception => e - error = "#{Time.now} #{ns[item]} => #{e.inspect}" + block.call(item) - redis.rpush ns[:errors], error - redis.publish ns[:errors], error - end + @backup.del end end - def errors - redis.lrange ns[:errors], 0, -1 - end - def stop @stopping = true + end + + def items + key.lrange(0, -1) end alias << push alias pop each