class EventMachine::ApnManager::CLI

Public Class Methods

new(args = [], opts = [], config = {}) click to toggle source
3 ways to specify the redis
  1. config yml file

  2. pass redis-url is ‘redis://username:password@host:port/database’

like ‘redis://test:test@localhost:6379/em_apn_manager`

Calls superclass method
# File lib/em_apn_manager/cli.rb, line 28
def initialize(args = [], opts = [], config = {})
  super(args, opts, config)

  redis_config = options[:redis]
  if redis_config.nil?
    # read the environment var.
    @environment = ENV["RAILS_ENV"] || "development"
    @environment = options[:environment] if %w{test development production}.include? options[:environment]

    # Read config option, or use default config yml
    config_path = options[:config] || File.join(".", "config", "em_apn_manager.yml")
    if config_path && File.exists?(config_path)
      EM::ApnManager.config = Thor::CoreExt::HashWithIndifferentAccess.new(YAML.load_file(config_path))
      redis_config = EM::ApnManager.config[@environment]
    end

    # default redis
    redis_config ||= "redis://localhost:6379/em_apn_manager"
  end

  # create redis connection
  $apn_manager_redis = Redis.new url: redis_config
end

Public Instance Methods

mock_apn_server() click to toggle source
# File lib/em_apn_manager/cli.rb, line 82
def mock_apn_server
  EM::ApnManager.logger.info("Starting Mock APN Server")
  EM.run { EM.start_server("127.0.0.1", 2195, EM::ApnManager::ApnServer) }
end
push_test_message() click to toggle source

if message is DISCONNECT, mock apns will disconnect.

# File lib/em_apn_manager/cli.rb, line 66
def push_test_message
  EM::ApnManager.push_notification({
    env: @environment,
    cert: File.read(ENV["APN_CERT"]), # test cert
    # iPhone5
    # "D42A6795D0C6C0E5F3CC762F905C3654D2A07E72D64CDEC1E2F74AC43C4CC440"
    # iPhone4
    # "BACF565FB283E9F7378D5A0AEBCBCB49A4D7834AED8FAC7FE0CB28144D94E456"
    # My iPhone4s
    # "0F93C49EAAF3544B5218D2BAE893608C515F69B445279AB2B17511C37046C52B"
    token: ["0F93C49EAAF3544B5218D2BAE893608C515F69B445279AB2B17511C37046C52B"].sample,
    message: "Hahahaha I am going to spam you. #{rand * 100}"
  })
end
server() click to toggle source
# File lib/em_apn_manager/cli.rb, line 55
def server
  daemonize if options[:daemon]
  write_pid_file(options[:pid_file]) if options[:pid_file]

  EM::ApnManager.logger.info("Starting APN Manager")
  EM.run { EM::ApnManager::Manager.run options }
end

Private Instance Methods

daemonize() click to toggle source

Daemonize process (ruby >= 1.9 only) @return [void] Ruby ~>1.8 @return [0] Ruby 1.9+ (see Process::daemon) @raise [Errno] on failure

# File lib/em_apn_manager/cli.rb, line 93
def daemonize
  if Process.respond_to?(:daemon)
    Process.daemon(true, true)
  else
    Kernel.warn "Running process as daemon requires ruby >= 1.9"
  end
end
write_pid_file(path = nil) click to toggle source

Save worker’s pid to file @return [void]

# File lib/em_apn_manager/cli.rb, line 103
def write_pid_file(path = nil)
  File.open(path, 'w'){ |f| f << Process.pid } if path
end