Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Use WeakRefs rather than ObjectSpace to play nice with JRuby

  • Loading branch information...
commit a0fbcfb55b9d8e584e080266ec72e017ef2eb618 1 parent 83bf1d2
@mperham authored
Showing with 20 additions and 13 deletions.
  1. +1 −0  History.md
  2. +18 −13 lib/girl_friday.rb
  3. +1 −0  lib/girl_friday/work_queue.rb
View
1  History.md
@@ -4,6 +4,7 @@ Changes
HEAD
---------
+* Use WeakRefs instead of ObjectSpace, as that plays better on JRuby.
* Can now pass a [connection\_pool](https://github.com/mperham/connection_pool) in as a Redis instance.
* Switch Redis.new to Redis.connect so a :url option can be passed in.
Nice for using on Heroku with Redis To Go.
View
31 lib/girl_friday.rb
@@ -1,3 +1,4 @@
+require 'weakref'
require 'thread'
begin
# Rubinius
@@ -16,12 +17,22 @@
module GirlFriday
+ @@lock = Mutex.new
+ @@queues = []
+
+ def self.add_queue(ref)
+ @@lock.synchronize do
+ @@queues = @@queues.keep_if { |q| q.weakref_alive? }
+ @@queues << ref
+ end
+ end
+
def self.queues
- ObjectSpace.each_object(GirlFriday::WorkQueue).to_a
+ @@queues
end
def self.status
- queues.inject({}) { |memo, queue| memo.merge(queue.status) }
+ queues.keep_if { |q| q.weakref_alive? }.inject({}) { |memo, queue| queue.weakref_alive? ? memo.merge(queue.__getobj__.status) : memo }
end
##
@@ -33,7 +44,7 @@ def self.status
# Note that shutdown! just works with existing queues. If you create a
# new queue, it will act as normal.
def self.shutdown!(timeout=30)
- qs = queues
+ qs = queues.delete_if { |q| !q.weakref_alive? }
count = qs.size
if count > 0
@@ -41,7 +52,8 @@ def self.shutdown!(timeout=30)
var = ConditionVariable.new
qs.each do |q|
- q.shutdown do |queue|
+ next if !q.weakref_alive?
+ q.__getobj__.shutdown do |queue|
m.synchronize do
count -= 1
var.signal if count == 0
@@ -52,19 +64,12 @@ def self.shutdown!(timeout=30)
m.synchronize do
var.wait(m, timeout)
end
- #puts "girl_friday shutdown complete"
end
count
end
end
-begin
- ObjectSpace.each_object(GirlFriday).to_a
- at_exit do
- GirlFriday.shutdown!
- end
-rescue RuntimeError
- $stderr.puts "[warn] girl_friday will not shut down cleanly, pass -X+O to JRuby to enable ObjectSpace"
+at_exit do
+ GirlFriday.shutdown!
end
-
View
1  lib/girl_friday/work_queue.rb
@@ -19,6 +19,7 @@ def initialize(name, options={}, &block)
@total_processed = @total_errors = @total_queued = 0
@persister = (options[:store] || Store::InMemory).new(name, (options[:store_config] || []))
start
+ GirlFriday.add_queue WeakRef.new(self)
end
def self.immediate!
Please sign in to comment.
Something went wrong with that request. Please try again.