Sha256: 86b56702e43aa3285dbc03345b10bb50d83034532bdb4fc51936826cc0e79c2c

Contents?: true

Size: 1.44 KB

Versions: 1

Compression:

Stored size: 1.44 KB

Contents

require "optparse"

require "tennis/launcher"

module Tennis
  class CLI

    DEFAULT_OPTIONS = {
      concurrency: 2,
      job_class_names: [],
    }.freeze

    def self.start
      options = DEFAULT_OPTIONS.dup
      OptionParser.new do |opts|
        opts.banner = "Usage: tennis [options]"
        opts.on("-j", "--jobs JOBS", "List of the job classes to handle") do |jobs|
          options[:job_class_names] = jobs.split(",")
        end
        opts.on("-c", "--concurrency COUNT", "The number of concurrent jobs") do |concurrency|
          options[:concurrency] = concurrency.to_i
        end
        opts.on("-r", "--require PATH", "Require files before starting") do |path|
          options[:require] ||= []
          options[:require] << path
        end
      end.parse!
      new(options).start
    end

    def initialize(options)
      @options = options
    end

    def start
      require_paths
      start_launcher
    end

    private

    def require_paths
      return unless requires = @options[:require]
      requires.each { |path| require path } if @options[:require]
    end

    def start_launcher
      raise "You must specify at least one job class" if job_classes.empty?
      Launcher.new({
        job_classes: job_classes,
        concurrency: @options[:concurrency]
      }).start
    end

    def job_classes
      @job_classes ||= @options[:job_class_names].map do |name|
        Object.const_get(name)
      end
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
tennis-jobs-0.4.0 lib/tennis/cli.rb