Sha256: 21b1bb08001bce6948368f1c0e1334fab96716395347201483735bad0f336220

Contents?: true

Size: 1.43 KB

Versions: 1

Compression:

Stored size: 1.43 KB

Contents

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

module Upperkut
  class CLI
    def initialize(args = ARGV)
      @options = {}
      parse_options(args)

      STDOUT.puts @options.inspect
    end

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

      manager = Manager.new(@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
        puts 'Shutting down'
        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

      end.parse!(args)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
upperkut-0.1.2 lib/upperkut/cli.rb