[Git][noosfero/noosfero][master] scheduler: scope into Noosfero

Bráulio Bhavamitra gitlab at gitlab.com
Fri Jul 24 18:04:55 BRT 2015


Bráulio Bhavamitra pushed to branch master at Noosfero / noosfero


Commits:
e65ff0a5 by Braulio Bhavamitra at 2015-07-24T17:54:46Z
scheduler: scope into Noosfero

- - - - -


4 changed files:

- config/initializers/unicorn.rb
- lib/noosfero/scheduler/defer.rb
- plugins/analytics/controllers/profile/analytics_plugin/time_on_page_controller.rb
- plugins/analytics/lib/analytics_plugin/base.rb


Changes:

=====================================
config/initializers/unicorn.rb
=====================================
--- a/config/initializers/unicorn.rb
+++ b/config/initializers/unicorn.rb
@@ -1,8 +1,8 @@
-require_dependency 'scheduler/defer'
+require_dependency 'noosfero/scheduler/defer'
 
 if defined? Unicorn
   ObjectSpace.each_object Unicorn::HttpServer do |s|
-    s.extend Scheduler::Defer::Unicorn
+    s.extend Noosfero::Scheduler::Defer::Unicorn
   end
 end
 


=====================================
lib/noosfero/scheduler/defer.rb
=====================================
--- a/lib/noosfero/scheduler/defer.rb
+++ b/lib/noosfero/scheduler/defer.rb
@@ -1,95 +1,97 @@
 # based on https://github.com/discourse/discourse/blob/master/lib/scheduler/defer.rb
 
-module Scheduler
-  module Deferrable
-    def initialize
-      # FIXME: do some other way when not using Unicorn
-      @async = (not Rails.env.test?) and defined? Unicorn
-      @queue = Queue.new
-      @mutex = Mutex.new
-      @paused = false
-      @thread = nil
-    end
+module Noosfero
+  module Scheduler
+    module Deferrable
+      def initialize
+        # FIXME: do some other way when not using Unicorn
+        @async = (not Rails.env.test?) and defined? Unicorn
+        @queue = Queue.new
+        @mutex = Mutex.new
+        @paused = false
+        @thread = nil
+      end
 
-    def pause
-      stop!
-      @paused = true
-    end
+      def pause
+        stop!
+        @paused = true
+      end
 
-    def resume
-      @paused = false
-    end
+      def resume
+        @paused = false
+      end
 
-    # for test
-    def async= val
-      @async = val
-    end
+      # for test
+      def async= val
+        @async = val
+      end
 
-    def later desc = nil, &blk
-      if @async
-        start_thread unless (@thread && @thread.alive?) || @paused
-        @queue << [blk, desc]
-      else
-        blk.call
+      def later desc = nil, &blk
+        if @async
+          start_thread unless (@thread && @thread.alive?) || @paused
+          @queue << [blk, desc]
+        else
+          blk.call
+        end
       end
-    end
 
-    def stop!
-      @thread.kill if @thread and @thread.alive?
-      @thread = nil
-    end
+      def stop!
+        @thread.kill if @thread and @thread.alive?
+        @thread = nil
+      end
 
-    # test only
-    def stopped?
-      !(@thread and @thread.alive?)
-    end
+      # test only
+      def stopped?
+        !(@thread and @thread.alive?)
+      end
 
-    def do_all_work
-      while !@queue.empty?
-        do_work _non_block=true
+      def do_all_work
+        while !@queue.empty?
+          do_work _non_block=true
+        end
       end
-    end
 
-    private
+      private
 
-    def start_thread
-      @mutex.synchronize do
-        return if @thread && @thread.alive?
-        @thread = Thread.new do
-          while true
-            do_work
+      def start_thread
+        @mutex.synchronize do
+          return if @thread && @thread.alive?
+          @thread = Thread.new do
+            while true
+              do_work
+            end
           end
+          @thread.priority = -2
         end
-        @thread.priority = -2
       end
-    end
 
-    # using non_block to match Ruby #deq
-    def do_work non_block=false
-      job, desc = @queue.deq non_block
-      begin
-        job.call
+      # using non_block to match Ruby #deq
+      def do_work non_block=false
+        job, desc = @queue.deq non_block
+        begin
+          job.call
+        rescue => ex
+          ExceptionNotifier.notify_exception ex, message: "Running deferred code '#{desc}'"
+        end
       rescue => ex
-        ExceptionNotifier.notify_exception ex, message: "Running deferred code '#{desc}'"
+        ExceptionNotifier.notify_exception ex, message: "Processing deferred code queue"
       end
-    rescue => ex
-      ExceptionNotifier.notify_exception ex, message: "Processing deferred code queue"
     end
-  end
 
-  class Defer
+    class Defer
 
-    module Unicorn
-      def process_client client
-        ::Scheduler::Defer.pause
-        super client
-        ::Scheduler::Defer.do_all_work
-        ::Scheduler::Defer.resume
+      module Unicorn
+        def process_client client
+          ::Noosfero::Scheduler::Defer.pause
+          super client
+          ::Noosfero::Scheduler::Defer.do_all_work
+          ::Noosfero::Scheduler::Defer.resume
+        end
       end
+
+      extend Deferrable
+      initialize
     end
 
-    extend Deferrable
-    initialize
   end
-
 end


=====================================
plugins/analytics/controllers/profile/analytics_plugin/time_on_page_controller.rb
=====================================
--- a/plugins/analytics/controllers/profile/analytics_plugin/time_on_page_controller.rb
+++ b/plugins/analytics/controllers/profile/analytics_plugin/time_on_page_controller.rb
@@ -4,7 +4,7 @@ class AnalyticsPlugin::TimeOnPageController < ProfileController
 
   def page_load
     # to avoid concurrency problems with the original deferred request, also defer this
-    Scheduler::Defer.later do
+    Noosfero::Scheduler::Defer.later do
       page_view = profile.page_views.where(request_id: params[:id]).first
       page_view.request = request
       page_view.page_load!


=====================================
plugins/analytics/lib/analytics_plugin/base.rb
=====================================
--- a/plugins/analytics/lib/analytics_plugin/base.rb
+++ b/plugins/analytics/lib/analytics_plugin/base.rb
@@ -22,7 +22,7 @@ class AnalyticsPlugin::Base < Noosfero::Plugin
         return if @analytics_skip_page_view
         return unless profile and profile.analytics_enabled?
 
-        Scheduler::Defer.later 'analytics: register page view' do
+        Noosfero::Scheduler::Defer.later 'analytics: register page view' do
           page_view = profile.page_views.build request: request, profile_id: profile,
             request_started_at: request_started_at, request_finished_at: request_finished_at
 



View it on GitLab: https://gitlab.com/noosfero/noosfero/commit/e65ff0a5a193d6459ebfc878cd3b8d10fffba78b
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://listas.softwarelivre.org/pipermail/noosfero-dev/attachments/20150724/13b6d92b/attachment.html>


More information about the Noosfero-dev mailing list