Skip to content

Commit

Permalink
Merge pull request #438 from rollbar/plugins
Browse files Browse the repository at this point in the history
Add plugins architecture
  • Loading branch information
jondeandres committed Apr 25, 2016
2 parents b651923 + 3412c9a commit c85d1b7
Show file tree
Hide file tree
Showing 47 changed files with 736 additions and 346 deletions.
56 changes: 8 additions & 48 deletions lib/rollbar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@
end

require 'rollbar/version'
require 'rollbar/plugins'
require 'rollbar/json'
require 'rollbar/js'
require 'rollbar/configuration'
require 'rollbar/encoding'
require 'rollbar/logger_proxy'
require 'rollbar/exception_reporter'
require 'rollbar/util'
require 'rollbar/railtie' if defined?(Rails::VERSION) && Rails::VERSION::MAJOR >= 3
require 'rollbar/delay/girl_friday' if defined?(GirlFriday)
require 'rollbar/delay/thread'
require 'rollbar/truncation'
Expand Down Expand Up @@ -812,6 +812,8 @@ class << self

def_delegators :notifier, *PUBLIC_NOTIFIER_METHODS

attr_writer :plugins

# Similar to configure below, but used only internally within the gem
# to configure it without initializing any of the third party hooks
def preconfigure
Expand All @@ -826,7 +828,7 @@ def configure

yield(configuration)

prepare
plugins.load!
reset_notifier!
end

Expand Down Expand Up @@ -854,52 +856,8 @@ def safely?
configuration.safely?
end

def prepare
prepare_js
require_hooks
require_core_extensions
end

def prepare_js
::Rollbar::Js.prepare if configuration.js_enabled
end

def require_hooks
return if configuration.disable_monkey_patch
wrap_delayed_worker

if defined?(ActiveRecord)
require 'active_record/version'
require 'rollbar/active_record_extension' if ActiveRecord::VERSION::MAJOR >= 3
end

require 'rollbar/sidekiq' if defined?(Sidekiq)
require 'rollbar/active_job' if defined?(ActiveJob)
require 'rollbar/goalie' if defined?(Goalie)
require 'rollbar/rack' if defined?(Rack) unless configuration.disable_rack_monkey_patch
require 'rollbar/rake' if defined?(Rake)
end

def require_core_extensions
# This monkey patch is always needed in order
# to use Rollbar.scoped
require 'rollbar/core_ext/thread'

return if configuration.disable_core_monkey_patch

# Needed to avoid active_support (< 4.1.0) bug serializing JSONs
require 'rollbar/core_ext/basic_socket' if monkey_patch_socket?
end

def monkey_patch_socket?
defined?(ActiveSupport::VERSION::STRING)
end

def wrap_delayed_worker
return unless defined?(Delayed) && defined?(Delayed::Worker) && configuration.delayed_job_enabled

require 'rollbar/delayed_job'
Rollbar::Delayed.wrap_worker
def plugins
@plugins ||= Rollbar::Plugins.new
end

def notifier
Expand Down Expand Up @@ -984,3 +942,5 @@ def report_message_with_request(message, level = 'info', request_data = nil, per
end
end
end

Rollbar.plugins.require_all
14 changes: 0 additions & 14 deletions lib/rollbar/active_record_extension.rb

This file was deleted.

7 changes: 0 additions & 7 deletions lib/rollbar/core_ext/basic_socket.rb

This file was deleted.

9 changes: 0 additions & 9 deletions lib/rollbar/core_ext/thread.rb

This file was deleted.

33 changes: 0 additions & 33 deletions lib/rollbar/goalie.rb

This file was deleted.

28 changes: 0 additions & 28 deletions lib/rollbar/js.rb
Original file line number Diff line number Diff line change
@@ -1,32 +1,4 @@
require "rollbar/js/version"

module Rollbar
module Js
extend self

attr_reader :framework
attr_reader :framework_loader

def prepare
@framework ||= detect_framework
@framework_loader ||= load_framework_class.new

@framework_loader.prepare
end

private

def detect_framework
case
when defined?(::Rails::VERSION)
:rails
end
end

def load_framework_class
require "rollbar/js/frameworks/#{framework}"

Rollbar::Js::Frameworks.const_get(framework.to_s.capitalize)
end
end
end
6 changes: 0 additions & 6 deletions lib/rollbar/js/frameworks.rb

This file was deleted.

49 changes: 0 additions & 49 deletions lib/rollbar/js/frameworks/rails.rb

This file was deleted.

5 changes: 0 additions & 5 deletions lib/rollbar/js/version.rb

This file was deleted.

5 changes: 2 additions & 3 deletions lib/rollbar/js/middleware.rb → lib/rollbar/middleware/js.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
require 'rack'
require 'rack/response'


module Rollbar
module Js
class Middleware
module Middleware
class Js
attr_reader :app
attr_reader :config

Expand Down
63 changes: 63 additions & 0 deletions lib/rollbar/plugin.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
module Rollbar
# Represents a plugin in the gem. Every plugin can have multiple dependencies
# and multiple execution blocks.
# On Rollbar initialization, all plugins will be saved in memory and those that
# satisfy the dependencies will be loaded
class Plugin
attr_reader :name
attr_reader :dependencies
attr_reader :callables
attr_accessor :loaded

private :loaded=

def initialize(name)
@name = name
@dependencies = []
@callables = []
@loaded = false
end

def configuration
Rollbar.configuration
end

def load!
return unless load?

begin
callables.each(&:call)
rescue => e
log_loading_error(e)
ensure
self.loaded = true
end
end

private

def dependency(&block)
dependencies << block
end

def execute(&block)
callables << block
end

def execute!(&block)
block.call if load?
end

def load?
!loaded && dependencies.all?(&:call)
rescue => e
log_loading_error(e)

false
end

def log_loading_error(e)
Rollbar.log_error("Error trying to load plugin '#{name}': #{e.class}, #{e.message}")
end
end
end
41 changes: 41 additions & 0 deletions lib/rollbar/plugins.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
require 'rollbar/plugin'

module Rollbar
# Stores the available plugin definitions and loads them
class Plugins
attr_reader :collection

def initialize
@collection = []
end

def require_all
Dir.glob(plugin_files).each do |file|
require file.to_s
end
end

def plugin_files
File.expand_path('../plugins/*.rb', __FILE__)
end

def define(name, &block)
return if loaded?(name)

plugin = Rollbar::Plugin.new(name)
collection << plugin

plugin.instance_eval(&block)
end

def load!
collection.each(&:load!)
end

private

def loaded?(name)
collection.any? { |plugin| plugin.name == name }
end
end
end
File renamed without changes.
Loading

0 comments on commit c85d1b7

Please sign in to comment.