Module: Smith

Includes:
Logger
Defined in:
lib/smith/commands/agency/restart.rb,
lib/smith.rb,
lib/smith/agent.rb,
lib/smith/cache.rb,
lib/smith/utils.rb,
lib/smith/config.rb,
lib/smith/daemon.rb,
lib/smith/logger.rb,
lib/smith/command.rb,
lib/smith/version.rb,
lib/smith/bootstrap.rb,
lib/smith/self_pipe.rb,
lib/smith/acl_parser.rb,
lib/smith/exceptions.rb,
lib/smith/agent_cache.rb,
lib/smith/amqp_errors.rb,
lib/smith/acl_compiler.rb,
lib/smith/agent_config.rb,
lib/smith/command_base.rb,
lib/smith/object_count.rb,
lib/smith/agent_process.rb,
lib/smith/messaging/util.rb,
lib/smith/commands/common.rb,
lib/smith/messaging/queue.rb,
lib/smith/agent_monitoring.rb,
lib/smith/messaging/sender.rb,
lib/smith/commands/template.rb,
lib/smith/messaging/factory.rb,
lib/smith/messaging/requeue.rb,
lib/smith/queue_definitions.rb,
lib/smith/application/agency.rb,
lib/smith/messaging/receiver.rb,
lib/smith/messaging/responder.rb,
lib/smith/commands/agency/kill.rb,
lib/smith/commands/agency/list.rb,
lib/smith/commands/agency/stop.rb,
lib/smith/commands/smithctl/rm.rb,
lib/smith/commands/agency/group.rb,
lib/smith/commands/agency/start.rb,
lib/smith/commands/smithctl/acl.rb,
lib/smith/commands/smithctl/pop.rb,
lib/smith/commands/smithctl/top.rb,
lib/smith/messaging/acl/default.rb,
lib/smith/commands/agency/agents.rb,
lib/smith/commands/agency/logger.rb,
lib/smith/commands/smithctl/dump.rb,
lib/smith/commands/smithctl/push.rb,
lib/smith/messaging/amqp_options.rb,
lib/smith/commands/agency/version.rb,
lib/smith/messaging/queue_factory.rb,
lib/smith/commands/agency/metadata.rb,
lib/smith/commands/smithctl/status.rb,
lib/smith/messaging/acl_type_cache.rb,
lib/smith/messaging/message_counter.rb,
lib/smith/commands/smithctl/commands.rb,
lib/smith/commands/smithctl/firehose.rb,
lib/smith/messaging/queue_definition.rb,
lib/smith/commands/smithctl/subscribe.rb,
lib/smith/commands/agency/object_count.rb

Overview

FIXME. This needs to be either fixed up or removed. It dumps the output into the log at the moment which isn’t the place for it. It should also be a smithctl command rather than an agency command.

Defined Under Namespace

Modules: ACL, AgentProcessObserver, Commands, LoggeMethods, Logger, Messaging, ObjectCount, QueueDefinitions, Utils Classes: ACLCompiler, ACLParser, AclTypeCache, Agency, AgencyRunning, Agent, AgentBootstrap, AgentCache, AgentConfig, AgentMonitoring, AgentProcess, Cache, Command, CommandBase, Config, ConfigNotFoundError, Daemon, MissingConfigItemError, QueueDefinition, QueueFactory, SelfPipe, UnknownEnvironmentError

Constant Summary collapse

VERSION =
"0.8.9"

Class Method Summary collapse

Methods included from Logger

included

Class Method Details

.acl_cache_directoryObject

Return the acl cache path.



70
71
72
73
74
# File 'lib/smith.rb', line 70

def acl_cache_directory
  cache_directory.join('acl').tap do |path|
    check_path(path, true)
  end
end

.acl_directoriesObject



61
62
63
# File 'lib/smith.rb', line 61

def acl_directories
  config.agency.acl_directories
end

.agent_directoriesObject



52
53
54
# File 'lib/smith.rb', line 52

def agent_directories
  config.agency.agent_directories
end

.cache_directoryObject



65
66
67
# File 'lib/smith.rb', line 65

def cache_directory
  Utils.check_and_create_directory(config.agency.cache_directory)
end

.compile_aclsObject



76
77
78
79
# File 'lib/smith.rb', line 76

def compile_acls
  @compiler = ACLCompiler.new
  @compiler.compile
end

.configObject



36
37
38
# File 'lib/smith.rb', line 36

def config
  Config.get
end

.config_pathObject



40
41
42
# File 'lib/smith.rb', line 40

def config_path
  Smith.config.path
end

.connectionObject

Raises:

  • (RuntimeError)


27
28
29
30
# File 'lib/smith.rb', line 27

def connection
  raise RuntimeError, "You must run this in a Smith.start block" if @connection.nil?
  @connection
end

.environmentObject



32
33
34
# File 'lib/smith.rb', line 32

def environment
  ENV['SMITH_ENV'] || 'development'
end

.hostnameObject

Convenience method to get the hostname



57
58
59
# File 'lib/smith.rb', line 57

def hostname
  Socket.gethostname
end

.lib_directoriesObject



48
49
50
# File 'lib/smith.rb', line 48

def lib_directories
  config.agency.lib_directories
end

.root_pathObject



44
45
46
# File 'lib/smith.rb', line 44

def root_path
  Pathname.new(__FILE__).dirname.parent.expand_path
end

.running?Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/smith.rb', line 81

def running?
  EM.reactor_running?
end

.shutdown_hook(&block) ⇒ Object



137
138
139
# File 'lib/smith.rb', line 137

def shutdown_hook(&block)
  EM.add_shutdown_hook(&block)
end

.start(opts = {}, &block) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/smith.rb', line 85

def start(opts={}, &block)

  if EM.epoll? && Smith.config.eventmachine.epoll
    logger.debug { "Using epoll for I/O event notification." }
    EM.epoll
  end

  if EM.kqueue? && Smith.config.eventmachine.kqueue
    logger.debug { "Using kqueue for I/O event notification." }
    EM.kqueue
  end

  connection_settings = {
    :on_tcp_connection_failure => method(:tcp_connection_failure_handler),
    :on_possible_authentication_failure => method(:authentication_failure_handler)
  }

  AMQP.start(config.amqp.broker.uri.to_s, connection_settings) do |connection|
    @connection = connection

    connection.on_connection do
      logger.info { "Connected to: AMQP Broker: #{broker_identifier(connection)}" } unless opts[:quiet]
    end

    # FIXME This should go in the config.
    reconnection_delay = 5

    connection.on_tcp_connection_loss do |connection, settings|
      logger.info { "Reconnecting to AMQP Broker: #{broker_identifier(connection)} in #{reconnection_delay}s" }
      connection.reconnect(false, reconnection_delay)
    end

    connection.after_recovery do |connection|
      logger.info { "Connection with AMQP Broker restored: #{broker_identifier(connection)}" } unless opts[:quiet]
    end

    connection.on_error do |connection, connection_close|
      # If the broker is gracefully shutdown we get a 320. Log a nice message.
      if connection_close.reply_code == 320
        logger.warn { "AMQP Broker shutdown: #{broker_identifier(connection)}" }
      else
        logger.warn {  connection_close.reply_text }
      end
    end

    # This will be the last thing run by the reactor.
    shutdown_hook { logger.debug { "Reactor Stopped" } }

    block.call
  end
end

.stop(immediately = false, &blk) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/smith.rb', line 141

def stop(immediately=false, &blk)
  shutdown_hook(&blk) if blk

  if running?
    if immediately
      EM.next_tick do
        @connection.close { EM.stop_event_loop }
      end
    else
      EM.add_timer(1) do
        @connection.close { EM.stop_event_loop }
      end
    end
  else
    logger.fatal { "Eventmachine is not running, exiting with prejudice" }
    exit!
  end
end