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/config.rb,
lib/smith/daemon.rb,
lib/smith/logger.rb,
lib/smith/command.rb,
lib/smith/version.rb,
lib/smith/bootstrap.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/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 Classes: ACLCompiler, ACLParser, AclTypeCache, Agency, AgencyRunning, Agent, AgentBootstrap, AgentCache, AgentConfig, AgentMonitoring, AgentProcess, Cache, Command, CommandBase, Config, ConfigNotFoundError, Daemon, QueueDefinition, QueueFactory, UnknownEnvironmentError

Constant Summary collapse

VERSION =
"0.6.5"

Class Method Summary collapse

Methods included from Logger

included

Class Method Details

.acl_cache_pathObject

Return the acl cache path.



62
63
64
65
66
# File 'lib/smith.rb', line 62

def acl_cache_path
  @acl_cache_path = Pathname.new(Smith.config.agency.acl_cache_path).tap do |path|
    check_path(path, true)
  end
end

.acl_pathObject



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

def acl_path
  path_to_pathnames(config.agency.acl_path)
end

.agent_pathsObject



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

def agent_paths
  path_to_pathnames(config.agency.agent_path)
end

.cache_pathObject



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

def cache_path
  Pathname.new(config.agency.cache_path).expand_path
end

.compile_aclsObject



68
69
70
71
# File 'lib/smith.rb', line 68

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

.configObject



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

def config
  Config.get
end

.config_pathObject



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

def config_path
  Smith.config.path
end

.connectionObject

Raises:

  • (RuntimeError)


23
24
25
26
# File 'lib/smith.rb', line 23

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

.environmentObject



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

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

.hostnameObject

Convenience method to get the hostname



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

def hostname
  Socket.gethostname
end

.root_pathObject



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

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

.running?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/smith.rb', line 73

def running?
  EM.reactor_running?
end

.shutdown_hook(&block) ⇒ Object



130
131
132
# File 'lib/smith.rb', line 130

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

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



77
78
79
80
81
82
83
84
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
# File 'lib/smith.rb', line 77

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

  EM.set_descriptor_table_size(Smith.config.eventmachine.file_descriptors)

  connection_settings = config.amqp.broker.merge(
    :on_tcp_connection_failure => method(:tcp_connection_failure_handler),
    :on_possible_authentication_failure => method(:authentication_failure_handler))

  AMQP.start(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



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/smith.rb', line 134

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