Sha256: f926534a6dc630e28e990661e405bb10a2a7b2e2ffd647400e10f81133b5446c

Contents?: true

Size: 1.97 KB

Versions: 1

Compression:

Stored size: 1.97 KB

Contents

# frozen_string_literal: true

require "sequel"

require_relative "tobox/version"

module Tobox
  class Error < StandardError; end

  EMPTY = [].freeze

  module Plugins
    PLUGINS_MUTEX = Thread::Mutex.new
    @plugins = {}

    # Loads a plugin based on a name. If the plugin hasn't been loaded, tries to load
    # it from the load path under "httpx/plugins/" directory.
    #
    def self.load_plugin(name)
      h = @plugins
      unless (plugin = PLUGINS_MUTEX.synchronize { h[name] })
        require "tobox/plugins/#{name}"
        raise "Plugin #{name} hasn't been registered" unless (plugin = PLUGINS_MUTEX.synchronize { h[name] })
      end
      plugin
    end

    # Registers a plugin (+mod+) in the central store indexed by +name+.
    #
    def self.register_plugin(name, mod)
      h = @plugins
      PLUGINS_MUTEX.synchronize { h[name] = mod }
    end
  end

  # when using batch sizes higher than 1, this method can be used to signal multiple errors
  # for a subset of the events which may have failed processing; these events are identified
  # by the index inside the batch.
  #
  #     on(:event_type) do |*events|
  #       successful, failed = handle_event_batch(events)
  #
  #       deal_with_success(successful)
  #
  #       batch_errors = failed.to_h do |failed_event|
  #         [
  #            events.index(failed_event),
  #            MyException.new("failed handling process batch")
  #         ]
  #       end
  #
  #       Tobox.raise_batch_error(batch_errors)
  #     end
  def self.raise_batch_errors(batch_errors)
    batch_errors = Hash.try_convert(batch_errors)
    unless batch_errors && batch_errors.all? { |k, v| k.is_a?(Integer) && v.is_a?(Exception) }
      raise Error, "batch errors must be an array of index-to-exception tuples"
    end

    throw(:tobox_batch_errors, batch_errors)
  end
end

require_relative "tobox/fetcher"
require_relative "tobox/worker"
require_relative "tobox/pool"
require_relative "tobox/application"
require_relative "tobox/configuration"

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
tobox-0.7.0 lib/tobox.rb