Commit c31a5c33c947df50fcaee9152f3bb1f1b9f80d73
Exists in
master
and in
11 other branches
Merge remote-tracking branch 'origin/master'
Showing
4 changed files
with
74 additions
and
72 deletions
Show diff stats
config/initializers/unicorn.rb
lib/noosfero/scheduler/defer.rb
1 | 1 | # based on https://github.com/discourse/discourse/blob/master/lib/scheduler/defer.rb |
2 | 2 | |
3 | -module Scheduler | |
4 | - module Deferrable | |
5 | - def initialize | |
6 | - # FIXME: do some other way when not using Unicorn | |
7 | - @async = (not Rails.env.test?) and defined? Unicorn | |
8 | - @queue = Queue.new | |
9 | - @mutex = Mutex.new | |
10 | - @paused = false | |
11 | - @thread = nil | |
12 | - end | |
3 | +module Noosfero | |
4 | + module Scheduler | |
5 | + module Deferrable | |
6 | + def initialize | |
7 | + # FIXME: do some other way when not using Unicorn | |
8 | + @async = (not Rails.env.test?) and defined? Unicorn | |
9 | + @queue = Queue.new | |
10 | + @mutex = Mutex.new | |
11 | + @paused = false | |
12 | + @thread = nil | |
13 | + end | |
13 | 14 | |
14 | - def pause | |
15 | - stop! | |
16 | - @paused = true | |
17 | - end | |
15 | + def pause | |
16 | + stop! | |
17 | + @paused = true | |
18 | + end | |
18 | 19 | |
19 | - def resume | |
20 | - @paused = false | |
21 | - end | |
20 | + def resume | |
21 | + @paused = false | |
22 | + end | |
22 | 23 | |
23 | - # for test | |
24 | - def async= val | |
25 | - @async = val | |
26 | - end | |
24 | + # for test | |
25 | + def async= val | |
26 | + @async = val | |
27 | + end | |
27 | 28 | |
28 | - def later desc = nil, &blk | |
29 | - if @async | |
30 | - start_thread unless (@thread && @thread.alive?) || @paused | |
31 | - @queue << [blk, desc] | |
32 | - else | |
33 | - blk.call | |
29 | + def later desc = nil, &blk | |
30 | + if @async | |
31 | + start_thread unless (@thread && @thread.alive?) || @paused | |
32 | + @queue << [blk, desc] | |
33 | + else | |
34 | + blk.call | |
35 | + end | |
34 | 36 | end |
35 | - end | |
36 | 37 | |
37 | - def stop! | |
38 | - @thread.kill if @thread and @thread.alive? | |
39 | - @thread = nil | |
40 | - end | |
38 | + def stop! | |
39 | + @thread.kill if @thread and @thread.alive? | |
40 | + @thread = nil | |
41 | + end | |
41 | 42 | |
42 | - # test only | |
43 | - def stopped? | |
44 | - !(@thread and @thread.alive?) | |
45 | - end | |
43 | + # test only | |
44 | + def stopped? | |
45 | + !(@thread and @thread.alive?) | |
46 | + end | |
46 | 47 | |
47 | - def do_all_work | |
48 | - while !@queue.empty? | |
49 | - do_work _non_block=true | |
48 | + def do_all_work | |
49 | + while !@queue.empty? | |
50 | + do_work _non_block=true | |
51 | + end | |
50 | 52 | end |
51 | - end | |
52 | 53 | |
53 | - private | |
54 | + private | |
54 | 55 | |
55 | - def start_thread | |
56 | - @mutex.synchronize do | |
57 | - return if @thread && @thread.alive? | |
58 | - @thread = Thread.new do | |
59 | - while true | |
60 | - do_work | |
56 | + def start_thread | |
57 | + @mutex.synchronize do | |
58 | + return if @thread && @thread.alive? | |
59 | + @thread = Thread.new do | |
60 | + while true | |
61 | + do_work | |
62 | + end | |
61 | 63 | end |
64 | + @thread.priority = -2 | |
62 | 65 | end |
63 | - @thread.priority = -2 | |
64 | 66 | end |
65 | - end | |
66 | 67 | |
67 | - # using non_block to match Ruby #deq | |
68 | - def do_work non_block=false | |
69 | - job, desc = @queue.deq non_block | |
70 | - begin | |
71 | - job.call | |
68 | + # using non_block to match Ruby #deq | |
69 | + def do_work non_block=false | |
70 | + job, desc = @queue.deq non_block | |
71 | + begin | |
72 | + job.call | |
73 | + rescue => ex | |
74 | + ExceptionNotifier.notify_exception ex, message: "Running deferred code '#{desc}'" | |
75 | + end | |
72 | 76 | rescue => ex |
73 | - ExceptionNotifier.notify_exception ex, message: "Running deferred code '#{desc}'" | |
77 | + ExceptionNotifier.notify_exception ex, message: "Processing deferred code queue" | |
74 | 78 | end |
75 | - rescue => ex | |
76 | - ExceptionNotifier.notify_exception ex, message: "Processing deferred code queue" | |
77 | 79 | end |
78 | - end | |
79 | 80 | |
80 | - class Defer | |
81 | + class Defer | |
81 | 82 | |
82 | - module Unicorn | |
83 | - def process_client client | |
84 | - ::Scheduler::Defer.pause | |
85 | - super client | |
86 | - ::Scheduler::Defer.do_all_work | |
87 | - ::Scheduler::Defer.resume | |
83 | + module Unicorn | |
84 | + def process_client client | |
85 | + ::Noosfero::Scheduler::Defer.pause | |
86 | + super client | |
87 | + ::Noosfero::Scheduler::Defer.do_all_work | |
88 | + ::Noosfero::Scheduler::Defer.resume | |
89 | + end | |
88 | 90 | end |
91 | + | |
92 | + extend Deferrable | |
93 | + initialize | |
89 | 94 | end |
90 | 95 | |
91 | - extend Deferrable | |
92 | - initialize | |
93 | 96 | end |
94 | - | |
95 | 97 | end | ... | ... |
plugins/analytics/controllers/profile/analytics_plugin/time_on_page_controller.rb
... | ... | @@ -4,7 +4,7 @@ class AnalyticsPlugin::TimeOnPageController < ProfileController |
4 | 4 | |
5 | 5 | def page_load |
6 | 6 | # to avoid concurrency problems with the original deferred request, also defer this |
7 | - Scheduler::Defer.later do | |
7 | + Noosfero::Scheduler::Defer.later do | |
8 | 8 | page_view = profile.page_views.where(request_id: params[:id]).first |
9 | 9 | page_view.request = request |
10 | 10 | page_view.page_load! | ... | ... |
plugins/analytics/lib/analytics_plugin/base.rb
... | ... | @@ -22,7 +22,7 @@ class AnalyticsPlugin::Base < Noosfero::Plugin |
22 | 22 | return if @analytics_skip_page_view |
23 | 23 | return unless profile and profile.analytics_enabled? |
24 | 24 | |
25 | - Scheduler::Defer.later 'analytics: register page view' do | |
25 | + Noosfero::Scheduler::Defer.later 'analytics: register page view' do | |
26 | 26 | page_view = profile.page_views.build request: request, profile_id: profile, |
27 | 27 | request_started_at: request_started_at, request_finished_at: request_finished_at |
28 | 28 | ... | ... |