PERF: stop running background work between requests

Use a dedicated thread to run Scheduler::Defer

This avoids blocking of a worker during operations that require waiting.

In particular uploads risked blocking a unicorn.

This also add a queue "length" that discourse prometheus consumes.
This commit is contained in:
Sam 2017-11-23 15:48:47 +11:00
parent d43a54e83a
commit 2b8d4508e5
4 changed files with 7 additions and 17 deletions

View File

@ -0,0 +1 @@
require_dependency 'scheduler/defer'

View File

@ -1,7 +0,0 @@
require_dependency 'scheduler/defer'
if defined? Unicorn::HttpServer
ObjectSpace.each_object(Unicorn::HttpServer) do |s|
s.extend(Scheduler::Defer::Unicorn)
end
end

View File

@ -8,6 +8,10 @@ module Scheduler
@thread = nil
end
def length
@queue.length
end
def pause
stop!
@paused = true
@ -80,16 +84,6 @@ module Scheduler
end
class Defer
module Unicorn
def process_client(client)
Defer.pause
super(client)
Defer.do_all_work
Defer.resume
end
end
extend Deferrable
initialize
end

View File

@ -31,6 +31,8 @@ describe Scheduler::Defer do
x = 2
end
expect(@defer.length).to eq(1)
@defer.do_all_work
expect(x).to eq(2)