Sha256: 04097ed0a69afb94ae6df9a940171c4dc5ca3ff89647db70063d91a0b1d3b661

Contents?: true

Size: 1.38 KB

Versions: 2

Compression:

Stored size: 1.38 KB

Contents

# frozen_string_literal: true

module DelayedJobWorkerPool
  class DSL
    class NoWorkerGroupsDefined < StandardError; end
    class NonUniqueGroupName < StandardError; end

    CALLBACK_SETTINGS = [:after_preload_app, :on_worker_boot, :after_worker_boot, :after_worker_shutdown].freeze
    DEFAULT_WORKER_GROUP_NAME = :default

    def self.load(path)
      options = {}

      dsl = new(options)
      dsl.instance_eval(File.read(path), path, 1)
      dsl.assert_groups_defined!

      options
    end

    def initialize(options)
      @options = options
      @options[:worker_groups] ||= {}
    end

    def preload_app(preload = true)
      @options[:preload_app] = preload
    end

    def worker_group(name = DEFAULT_WORKER_GROUP_NAME)
      name_sym = name.to_sym
      if @options[:worker_groups].key?(name_sym)
        raise NonUniqueGroupName.new("Worker group name #{name_sym} is already in use")
      end

      group_options = WorkerGroupOptions.new
      yield(group_options)
      @options[:worker_groups][name_sym] = group_options
    end

    def assert_groups_defined!
      return unless @options[:worker_groups].empty?

      raise NoWorkerGroupsDefined.new('No worker groups defined. Define groups using `worker_group`.')
    end

    CALLBACK_SETTINGS.each do |option_name|
      define_method(option_name) do |&block|
        @options[option_name] = block
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
delayed_job_worker_pool-1.0.0 lib/delayed_job_worker_pool/dsl.rb
delayed_job_worker_pool-0.3.0 lib/delayed_job_worker_pool/dsl.rb