lib/tobox/configuration.rb in tobox-0.6.1 vs lib/tobox/configuration.rb in tobox-0.7.0

- old
+ new

@@ -5,11 +5,12 @@ module Tobox class Configuration extend Forwardable - attr_reader :plugins, :handlers, :lifecycle_events, :arguments_handler, :default_logger, :database, :fetcher_class, + attr_reader :plugins, :handlers, :lifecycle_events, :arguments_handler, :default_logger, :database, + :fetcher_class, :worker_class, :config def_delegator :@config, :[] DEFAULT_CONFIGURATION = { @@ -17,10 +18,12 @@ logger: nil, log_level: nil, database_uri: nil, database_options: nil, table: :outbox, + visibility_column: :run_at, + attempts_column: :attempts, created_at_column: nil, batch_size: 1, max_attempts: 10, exponential_retry_factor: 2, wait_for_events_delay: 5, @@ -45,10 +48,11 @@ @lifecycle_events = {} @handlers = {} @message_to_arguments = nil @plugins = [] @fetcher_class = Class.new(Fetcher) + @worker_class = Class.new(Worker) if block case block.arity when 0 instance_exec(&block) @@ -115,10 +119,15 @@ def on_error_event(&callback) (@lifecycle_events[:error_event] ||= []) << callback self end + def on_start_worker(&callback) + (@lifecycle_events[:start_worker] ||= []) << callback + self + end + def on_error_worker(&callback) (@lifecycle_events[:error_worker] ||= []) << callback self end @@ -130,10 +139,20 @@ def message_to_arguments(&callback) @arguments_handler = callback self end + def visibility_type_bool? + _, visibility_info = @database.schema(@config[:table]).find do |column, _| + column == @config[:visibility_column] + end + + raise Error, "a visibility column is required" unless visibility_info + + visibility_info[:type] == :boolean + end + def plugin(plugin, **options, &block) raise Error, "Cannot add a plugin to a frozen config" if frozen? plugin = Plugins.load_plugin(plugin) if plugin.is_a?(Symbol) @@ -143,9 +162,10 @@ plugin.load_dependencies(self, **options, &block) if plugin.respond_to?(:load_dependencies) extend(plugin::ConfigurationMethods) if defined?(plugin::ConfigurationMethods) @fetcher_class.__send__(:include, plugin::FetcherMethods) if defined?(plugin::FetcherMethods) + @worker_class.__send__(:include, plugin::WorkerMethods) if defined?(plugin::WorkerMethods) plugin.configure(self, **options, &block) if plugin.respond_to?(:configure) end def freeze