class Portal::JobManager::Builder

@api private @since 0.1.0

Attributes

scheduled_jobs_config[R]

@return [Hash]

@api private @since 0.1.0

timezone[R]

@return [String]

@api private @since 0.1.0

Public Class Methods

build(config) click to toggle source

@param config [Sidekiq::Portal::Config] @return [void]

@api private @since 0.1.0

# File lib/portal/job_manager/builder.rb, line 12
def build(config)
  new(config).build
end
new(config) click to toggle source

@param config [Sidekiq::Portal::Config] @return [void]

@api private @since 0.1.0

# File lib/portal/job_manager/builder.rb, line 22
def initialize(config)
  @scheduled_jobs_config = config.settings.scheduler_config
  @timezone = config.settings.default_timezone
end

Public Instance Methods

build() click to toggle source

@return [Sidekiq::Portal::Manager]

@api private @since 0.1.0

# File lib/portal/job_manager/builder.rb, line 31
def build
  current_time = ActiveSupport::TimeZone[timezone].at(Time.current)
  job_registry = Sidekiq::Portal::JobRegistry.new

  scheduled_jobs_config.each_pair do |job_config_series, scheduler_options|
    job_klass = resolve_job_klass(job_config_series, scheduler_options)
    cron_pattern, every_pattern = resolve_time_configuration(job_klass, scheduler_options)
    job_wrapper = build_job_wrapper(job_klass, cron_pattern, every_pattern, current_time)

    job_registry.register(job_wrapper)
  end

  Sidekiq::Portal::JobManager.new(job_registry)
end

Private Instance Methods

build_job_wrapper(job_klass, cron_pattern, every_pattern, current_time) click to toggle source

@param job_klass [Class] @param cron_pattern [String, NilClass] @param every_pattern [String, NilClass] @param current_time [Time] @return [Sidekiq::Portal::Job]

@api private @since 0.1.0

# File lib/portal/job_manager/builder.rb, line 108
def build_job_wrapper(job_klass, cron_pattern, every_pattern, current_time)
  Sidekiq::Portal::Job::Builder.build(
    job_klass,
    initial_time: current_time,
    cron_pattern: cron_pattern,
    every_pattern: every_pattern,
    timezone: timezone
  )
end
resolve_job_klass(job_config_series, scheduler_options) click to toggle source

@param job_config_series [String] @param scheduler_options [Hash] @return [Class]

@api private @since 0.1.0

# File lib/portal/job_manager/builder.rb, line 66
  def resolve_job_klass(job_config_series, scheduler_options)
    job_klass = scheduler_options['class'].to_s.constantize rescue nil
    job_klass ||= job_config_series.to_s.constantize rescue nil

    raise(Sidekiq::Portal::ConfusingJobConfigError, <<~ERROR_MESSAGE) unless job_klass
      Can't resolve job class from \"#{scheduler_options['class'] || job_config_series}\" job name
    ERROR_MESSAGE

    job_klass
  end
resolve_time_configuration(job_klass, scheduler_options) click to toggle source

@return Array

@api private @since 0.1.0

# File lib/portal/job_manager/builder.rb, line 81
  def resolve_time_configuration(job_klass, scheduler_options)
    cron_time_pattern  = scheduler_options['cron'] || scheduler_options[:cron]
    every_time_pattern = scheduler_options['every'] || scheduler_options[:every]

    unless cron_time_pattern || every_time_pattern
      raise(Sidekiq::Portal::NoJobTimeConfigurationError, <<~ERROR_MESSAGE)
        Time configuration for "#{job_klass}" job klass is not found
      ERROR_MESSAGE
    end

    if cron_time_pattern && every_time_pattern
      raise(Sidekq::Portal::AmbiguousJobTimeConfigurationError, <<~ERROR_MESSAGE)
        Ambiguous time config (you should provide either <cron> pattern or <every> pattern)
      ERROR_MESSAGE
    end

    [cron_time_pattern, every_time_pattern]
  end