Sha256: b0dfcc57b4fb6e5ca8303ee49e289bd012253877e0a4a96be0409f33ea3a3702

Contents?: true

Size: 1.08 KB

Versions: 4

Compression:

Stored size: 1.08 KB

Contents

require "logger"
require "optparse"
require "sneakers/runner"

class Example
  def initialize(filename, klass)
    @filename = filename
    @klass = klass
  end

  def run(&client_code)
    configure_workers
    parse_options
    if @options[:client]
      start_client(client_code)
    elsif @options[:worker]
      start_worker
    end
  end

  protected

  def configure_workers
    Tennis.configure do |config|
      config.async = true
      config.exchange = "example"
      config.workers = 1
      config.logger = Logger.new(STDOUT)
      config.logger.level = Logger::WARN
    end
  end

  def start_client(block, i = 1)
    block.call(i)
    start_client(block, i + 1)
  rescue Interrupt
    puts "Exiting the client"
  end

  def start_worker
    Sneakers::Runner.new([@klass.worker]).run
  end

  def parse_options
    @options = {}
    OptionParser.new do |opts|
      opts.banner = "Usage: #{@filename}"
      opts.on("-c", "--client", "Run the client") { @options[:client] = true }
      opts.on("-w", "--worker", "Run the worker") { @options[:worker] = true }
    end.parse!
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
tennis-jobs-0.3.1 examples/example.rb
tennis-jobs-0.3.0 examples/example.rb
tennis-jobs-0.2.0 examples/example.rb
tennis-jobs-0.1.0 examples/example.rb