lib/bluth.rb in bluth-0.6.1 vs lib/bluth.rb in bluth-0.6.7

- old
+ new

@@ -33,23 +33,41 @@ class Maeby < Familia::Problem; end # A shutdown request. We burn down the banana stand. class Shutdown < Familia::Problem; end @db = 0 @env = :dev - @poptimeout = 60 #.seconds + @queuetimeout = 60 #.seconds @handlers = [] @locks = [] @sysinfo = nil @priority = [] @scheduler = nil class << self attr_reader :handlers, :db, :conf, :locks - attr_accessor :redis, :uri, :priority, :scheduler, :poptimeout, :env + attr_accessor :redis, :uri, :priority, :scheduler, :queuetimeout, :env def sysinfo @sysinfo ||= SysInfo.new.freeze @sysinfo end + # A block to be called before a worker starts. + # + # e.g. + # Bluth.onconnect do + # config = YourProject.load_config + # Familia.uri = config[:redis_uri] + # end + # + # Note: + # this block can be called multiple times so do not + # put anything with side effects in here. + def onconnect &blk + @onconnect = blk unless blk.nil? + @onconnect + end + def connect + instance_eval &onconnect unless onconnect.nil? + end end def Bluth.clear_locks @locks.each { |lock| Familia.info "Removing lock #{lock}" @@ -96,11 +114,11 @@ # Set default priority Bluth.priority = [:critical, :high, :low] end # Workers use a blocking pop and will wait for up to - # Bluth.poptimeout (seconds) before returnning nil. + # Bluth.queuetimeout (seconds) before returnning nil. # Note that the queues are still processed in order. # If all queues are empty, the first one to return a # value is use. See: # # http://code.google.com/p/redis/wiki/BlpopCommand @@ -117,10 +135,10 @@ # +meth+ is either :blpop or :brpop def Bluth.blocking_queue_handler meth gob = nil begin order = Bluth::Queue.entry_queues.collect(&:rediskey) - order << Bluth.poptimeout # We do it this way to support Ruby 1.8 + order << Bluth.queuetimeout # We do it this way to support Ruby 1.8 queue, gobid = *(Bluth::Queue.redis.send(meth, *order) || []) unless queue.nil? Familia.ld "FOUND #{gobid} id #{queue}" gob = Gob.from_redis gobid raise Bluth::Buster, "No such gob object: #{gobid}" if gob.nil?