mirror of
https://github.com/discourse/discourse.git
synced 2024-11-23 09:26:54 -06:00
BUGFIX: work correctly if process forks
This commit is contained in:
parent
90139efc6f
commit
798b8444cf
@ -3,11 +3,10 @@ module Scheduler
|
||||
def initialize
|
||||
@async = Rails.env != "test"
|
||||
@queue = Queue.new
|
||||
@thread = Thread.new {
|
||||
while true
|
||||
do_work
|
||||
end
|
||||
}
|
||||
@mutex = Mutex.new
|
||||
@thread = nil
|
||||
start_thread
|
||||
|
||||
end
|
||||
|
||||
# for test
|
||||
@ -17,6 +16,7 @@ module Scheduler
|
||||
|
||||
def later(&blk)
|
||||
if @async
|
||||
start_thread unless @thread.alive?
|
||||
@queue << [RailsMultisite::ConnectionManagement.current_db, blk]
|
||||
else
|
||||
blk.call
|
||||
@ -27,8 +27,24 @@ module Scheduler
|
||||
@thread.kill
|
||||
end
|
||||
|
||||
# test only
|
||||
def stopped?
|
||||
!@thread.alive?
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def start_thread
|
||||
@mutex.synchronize do
|
||||
return if @thread && @thread.alive?
|
||||
@thread = Thread.new {
|
||||
while true
|
||||
do_work
|
||||
end
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
def do_work
|
||||
db, job = @queue.deq
|
||||
RailsMultisite::ConnectionManagement.establish_connection(db: db)
|
||||
|
@ -23,6 +23,26 @@ describe Scheduler::Defer do
|
||||
@defer.stop!
|
||||
end
|
||||
|
||||
it "recovers from a crash / fork" do
|
||||
s = nil
|
||||
@defer.stop!
|
||||
wait_for(10) do
|
||||
@defer.stopped?
|
||||
end
|
||||
# hack allow thread to die
|
||||
sleep 0.005
|
||||
|
||||
@defer.later do
|
||||
s = "good"
|
||||
end
|
||||
|
||||
wait_for(10) do
|
||||
s == "good"
|
||||
end
|
||||
|
||||
s.should == "good"
|
||||
end
|
||||
|
||||
it "can queue jobs properly" do
|
||||
s = nil
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user