class HonestPubsub::CLI

Attributes

pidfile[RW]
subscribers[RW]

Public Instance Methods

parse(args = ARGV) click to toggle source

Method to support parsing of arguments passed through the command line

# File lib/honest_pubsub/cli.rb, line 16
def parse(args = ARGV)
  optparse = OptionParser.new do |opts|
    opts.banner = "Usage: bundle exec start_subscribers [options]"
    opts.on '-P', '--pidfile PATH', "path to pidfile" do |arg|
      @pidfile = arg
    end

    opts.on("-o", "--only [SUBSCRIBERS]", "comma separated name of subsriber classes that should be run") do |subscribers|
      @subscribers = subscribers.split(/\,/)
    end

    opts.on '-r', '--require [PATH|DIR]', "Location of Rails application with workers or file to require" do |arg|
      @require_path = arg
    end

    opts.on '-v', '--version', "Print version and exit" do |arg|
      puts "HonestPubsub #{HonestPubsub::VERSION}"
      abort
    end
  end

  optparse.parse!(args)
end
remove_pid() click to toggle source
# File lib/honest_pubsub/cli.rb, line 60
def remove_pid
  return unless pidfile
  File.delete(pidfile) if File.exist?(pidfile)
end
require_path() click to toggle source
# File lib/honest_pubsub/cli.rb, line 46
def require_path
  @require_path || "."
end
run() click to toggle source
# File lib/honest_pubsub/cli.rb, line 40
def run
  load_environment
  write_pidfile
  load_subscribers
end
subscriber_classes() click to toggle source

@return [Array] returns array of subscriber classes that will be executed by the CLI

# File lib/honest_pubsub/cli.rb, line 52
def subscriber_classes
  if subscribers.present?
    subscribers.map(&:constantize)
  else
    HonestPubsub::Subscriber.class_variable_get(:@@registered_subscribers)
  end
end

Private Instance Methods

load_environment() click to toggle source
# File lib/honest_pubsub/cli.rb, line 67
def load_environment
  if require_path
    raise ArgumentError, "#{require_path} does not exist" unless File.exist?(require_path)
  end

  if File.directory?(require_path)
    require 'rails'
    require File.expand_path("#{require_path}/config/environment.rb")
    ::Rails.application.eager_load!
  else
    require require_path
  end
end
load_subscribers() click to toggle source
# File lib/honest_pubsub/cli.rb, line 88
def load_subscribers
  HonestPubsub::Server::SubscriberServer.new(subscriber_classes).start
end
write_pidfile() click to toggle source
# File lib/honest_pubsub/cli.rb, line 81
def write_pidfile
  return unless pidfile
  File.open(pidfile, 'w') do |f|
    f.puts Process.pid
  end
end