Permalink
Browse files

Use ObjectSpace instead of class variables and weakrefs. It's just too

easy to lead to a memory leak.
  • Loading branch information...
1 parent 369a496 commit 10486aaac7ec2f44926c4d6f66f28923579b430e @mperham committed Jul 14, 2011
Showing with 7 additions and 8 deletions.
  1. +6 −7 lib/girl_friday.rb
  2. +1 −1 lib/girl_friday/work_queue.rb
View
@@ -1,5 +1,4 @@
require 'thread'
-require 'weakref'
begin
# Rubinius
require 'actor'
@@ -13,16 +12,16 @@
require 'girl_friday/work_queue'
require 'girl_friday/error_handler'
require 'girl_friday/persistence'
+require 'girl_friday/batch'
module GirlFriday
- @@queues = []
def self.queues
- @@queues
+ ObjectSpace.each_object(GirlFriday::WorkQueue).to_a
end
def self.status
- queues.delete_if { |q| !q.weakref_alive? }.inject({}) { |memo, queue| queue.weakref_alive? ? memo.merge(queue.status) : memo }
+ queues.inject({}) { |memo, queue| memo.merge(queue.status) }
end
##
@@ -36,14 +35,14 @@ def self.status
#
# WeakRefs make this method full of race conditions with GC. :-(
def self.shutdown!(timeout=30)
- queues.delete_if { |q| !q.weakref_alive? }
- count = queues.size
+ qs = queues
+ count = qs.size
if count > 0
m = Mutex.new
var = ConditionVariable.new
- queues.each do |q|
+ qs.each do |q|
q.shutdown do |queue|
m.synchronize do
count -= 1
@@ -7,6 +7,7 @@ class WorkQueue
attr_reader :name
def initialize(name, options={}, &block)
+ raise ArgumentError, "#{self.class.name} requires a block" unless block_given?
@name = name.to_s
@size = options[:size] || 5
@processor = block
@@ -18,7 +19,6 @@ def initialize(name, options={}, &block)
@total_processed = @total_errors = @total_queued = 0
@persister = (options[:store] || Store::InMemory).new(name, (options[:store_config] || []))
start
- GirlFriday.queues << WeakRef.new(self)
end
if defined?(Rails) && Rails.env.development?

0 comments on commit 10486aa

Please sign in to comment.