class Procrastinator::Config

Configuration object (State Pattern) used to coordinate settings across various components within Procrastinator.

All of its state is read-only, set using the methods in the DSL module below.

@author Robin Miller

@!attribute [r] :test_mode?

@return [Boolean] Whether test mode is enabled

@!attribute [r] :queues

@return [Array] List of defined queues

@!attribute [r] :context

@return [Object] Provided context object that will be forwarded to tasks

@!attribute [r] :loader

@return [Object] Provided persistence strategy object to use for task I/O

@!attribute [r] :log_dir

@return [Pathname] Directory to write log files in

@!attribute [r] :log_level

@return [Integer] Logging level to use

@!attribute [r] :prefix

@return [String] The prefix to prepend to process names

@!attribute [r] :pid_dir

@return [Pathname] Directory to write process ID records in

Constants

DEFAULT_LOG_DIRECTORY
DEFAULT_PID_DIRECTORY

Attributes

context[R]
loader[R]
log_dir[R]
log_level[R]
pid_dir[R]
prefix[R]
queues[R]
test_mode[R]
test_mode?[R]

Public Class Methods

new() click to toggle source
# File lib/procrastinator/config.rb, line 34
def initialize
   @test_mode        = false
   @queues           = []
   @loader           = nil
   @context          = nil
   @subprocess_block = nil
   @log_dir          = Pathname.new(DEFAULT_LOG_DIRECTORY)
   @log_level        = Logger::INFO
   @pid_dir          = Pathname.new(DEFAULT_PID_DIRECTORY)
end

Public Instance Methods

queue(name: nil) click to toggle source
# File lib/procrastinator/config.rb, line 144
def queue(name: nil)
   if name
      @queues.find do |q|
         q.name == name
      end
   else
      @queues.first
   end
end
queues_string() click to toggle source
# File lib/procrastinator/config.rb, line 131
def queues_string
   # it drops the colon if you call #to_s on a symbol, so we need to add it back
   @queues.map { |queue| ":#{ queue.name }" }.join(', ')
end
run_process_block() click to toggle source
# File lib/procrastinator/config.rb, line 140
def run_process_block
   @subprocess_block&.call
end
setup(test_mode = false) { |self| ... } click to toggle source
# File lib/procrastinator/config.rb, line 107
      def setup(test_mode = false)
         yield(self)

         enable_test_mode if test_mode

         load_with(Loader::CSVLoader.new) unless @loader

         raise 'setup block must call #define_queue on the environment' if @queues.empty?

         if @context && @queues.none? { |queue| queue.task_class.method_defined?(:context=) }
            raise <<~ERROR
               setup block called #provide_context, but no queue task classes import :context.

               Add this to your Task classes that expect to receive the context:

                  include Procrastinator::Task

                  task_attr :context
            ERROR
         end

         self
      end
single_queue?() click to toggle source
# File lib/procrastinator/config.rb, line 136
def single_queue?
   @queues.size == 1
end

Private Instance Methods

verify_task_class(task_class) click to toggle source
# File lib/procrastinator/config.rb, line 156
def verify_task_class(task_class)
   unless task_class.method_defined? :run
      raise MalformedTaskError, "task #{ task_class } does not support #run method"
   end

   # We're checking the interface compliance on init because it's one of those extremely rare cases where
   # you'd want to know early because the sub-processes would crash async, which is harder to debug.
   # It's a bit belt-and suspenders, but UX is important for devs, too. - robinetmiller
   if task_class.method_defined?(:run) && task_class.instance_method(:run).arity.positive?
      err = "task #{ task_class } cannot require parameters to its #run method"

      raise MalformedTaskError, err
   end

   expected_arity = 1

   [:success, :fail, :final_fail].each do |method_name|
      next unless task_class.method_defined?(method_name)
      next if task_class.instance_method(method_name).arity == expected_arity

      err = "task #{ task_class } must accept #{ expected_arity } parameter to its ##{ method_name } method"

      raise MalformedTaskError, err
   end
end