Sha256: 9afe27fd06c30fe30e3a0214fa33c9e402419d2fcde918503dd6d055e38b6502

Contents?: true

Size: 1.8 KB

Versions: 4

Compression:

Stored size: 1.8 KB

Contents

require 'optparse'
require_relative '../upperkut'
require_relative 'manager'
require_relative 'logging'

module Upperkut
  class CLI
    def initialize(args = ARGV)
      @options = {}
      @logger = Upperkut::Logging.logger

      parse_options(args)
    end

    def start
      if file = @options[:file]
        require file
      end

      if log_level = @options[:log_level]
        @logger.level = log_level
      end

      @options[:logger] = @logger

      manager = Manager.new(@options)

      @logger.info(@options)

      r, w = IO.pipe
      signals = %w[INT TERM]

      signals.each do |signal|
        trap signal do
          w.puts(signal)
        end
      end

      begin
        manager.run
        while readable_io = IO.select([r])
          signal = readable_io.first[0].gets.strip
          handle_signal(signal)
        end
      rescue Interrupt
        @logger.info(
          'Stopping managers, wait for 5 seconds and them kill processors'
        )

        manager.stop
        sleep(5)
        manager.kill
        exit(0)
      end
    end

    private

    def handle_signal(sig)
      case sig
      when 'INT'
        raise Interrupt
      when 'TERM'
        raise Interrupt
      end
    end

    def parse_options(args)
      OptionParser.new do |o|
        o.on('-w', '--worker WORKER', 'Define worker to be processed') do |arg|
          @options[:worker] = arg
        end
        o.on('-r', '--require FILE', 'Indicate a file to be required') do |arg|
          @options[:file] = arg
        end
        o.on('-c', '--concurrency INT', 'Numbers of threads to spawn') do |arg|
          @options[:concurrency] = Integer(arg)
        end
        o.on('-l', '--log-level LEVEL', 'Log level') do |arg|
          @options[:log_level] = arg.to_i
        end
      end.parse!(args)
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
upperkut-0.4.2 lib/upperkut/cli.rb
upperkut-0.4.1 lib/upperkut/cli.rb
upperkut-0.4.0 lib/upperkut/cli.rb
upperkut-0.3.0 lib/upperkut/cli.rb