Class: HomeQ::Base::System

Inherits:
Object
  • Object
show all
Includes:
Configuration, Logging, Options, Observable, Singleton
Defined in:
lib/homeq/base/system.rb

Constant Summary collapse

PERIOD_TASK_INTERVAL =

Run periodic tasks every so many seconds

60

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Options

#options

Methods included from Logging

#logger

Methods included from Configuration

#config

Constructor Details

#initializeSystem

Returns a new instance of System.



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/homeq/base/system.rb', line 113

def initialize
  @handlers 	= {}
  @servers 	= []
  @queues 	= []
  @topology 	= HomeQ::SOBS::Topology::Topology.new
  @state = Statemachine.build do
    state :start do
      event :init, :waiting_to_run, :delayed_init
      event :error, :stopped
    end
    state :waiting_to_run do
      on_entry :broadcast_change
      event :init, :waiting_to_run
      event :started, :running
      event :stop, :stopped
    end
    state :running do
      on_entry :broadcast_change
      event :error, :stopped
      event :stop, :stopped
    end
    state :stopped do
      on_entry :shutdown
      event :start, :running
      event :stop, :stopped
      event :started, :stopped
      event :init, :running
    end
    state :exit do
    end
  end
  @state.context = self
  
  catch_signals

  setup_em
end

Instance Attribute Details

#config_fileObject

Returns the value of attribute config_file.



107
108
109
# File 'lib/homeq/base/system.rb', line 107

def config_file
  @config_file
end

#cp_serverObject

Returns the value of attribute cp_server.



108
109
110
# File 'lib/homeq/base/system.rb', line 108

def cp_server
  @cp_server
end

#handlersObject

Returns the value of attribute handlers.



109
110
111
# File 'lib/homeq/base/system.rb', line 109

def handlers
  @handlers
end

#pid_fileObject

Returns the value of attribute pid_file.



111
112
113
# File 'lib/homeq/base/system.rb', line 111

def pid_file
  @pid_file
end

#queuesObject

Returns the value of attribute queues.



106
107
108
# File 'lib/homeq/base/system.rb', line 106

def queues
  @queues
end

#serversObject

Returns the value of attribute servers.



105
106
107
# File 'lib/homeq/base/system.rb', line 105

def servers
  @servers
end

#topologyObject

Returns the value of attribute topology.



110
111
112
# File 'lib/homeq/base/system.rb', line 110

def topology
  @topology
end

Instance Method Details

#add_breakpoint(file, line, expr) ⇒ Object

Tell the debugger to break at the supplied file and line.



235
236
237
# File 'lib/homeq/base/system.rb', line 235

def add_breakpoint(file, line, expr)
  Debugger.add_breakpoint(file, line, expr)
end

#debug_nowObject

If debugging is already enabled (via enable_debugging), then call ‘debugger’.



200
201
202
203
204
205
206
207
208
# File 'lib/homeq/base/system.rb', line 200

def debug_now
  if @debugging
    debugger
  else
    logger.info {
      "Debugging not started."
    }
  end
end

#die(msg = nil, exit_status = -1)) ⇒ Object

We don’t do any nice cleanup, just go down fast.



193
194
195
196
# File 'lib/homeq/base/system.rb', line 193

def die(msg=nil, exit_status=-1)
  logger.fatal(msg) if msg
  exit!(exit_status)
end

#disable_debuggingObject

If already debugging, disable it and call Debugger.stop().



224
225
226
227
228
229
230
231
232
# File 'lib/homeq/base/system.rb', line 224

def disable_debugging
  if @debugging
    logger.info {
      "Disabled debugging"
    }
    @debugging = false
    Debugger.stop
  end
end

#enable_debuggingObject

If not already enabled, setup for debugging by doing a require ‘ruby-debug’, and then a Debugger.start_remote()



212
213
214
215
216
217
218
219
220
221
# File 'lib/homeq/base/system.rb', line 212

def enable_debugging
  if !@debugging
    logger.info {
      "Enabled debugging."
    }
    @debugging = true
    require 'ruby-debug'
    Debugger.start_remote
  end
end

#startObject

Set the system in motion. Initializes, reading command line args and config file, calls EventMachine::run. If called with a block, it will yield to the block inside the block passed to EventMachine::run, so application code will have a chance to do any startup tasks inside the EventMachine::run block.

After yielding to your application code (while still in the EventMachine::run block), it starts the server, queues, and the control port, and then away we go.



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/homeq/base/system.rb', line 160

def start
  @state.init
  if @state.state == :waiting_to_run
    EventMachine::set_descriptor_table_size(10000)
    # TODO Hmm.  Use this?
    # EventMachine::set_effective_user("nobody")
    logger.info {
      "Starting Eventmachine.  Pid: #{Process.pid} " +
      "HOMEQ_ENV: #{HOMEQ_ENV} HOMEQ_APP_ROOT: #{HOMEQ_APP_ROOT}"
    }
    EventMachine::run {
      yield if block_given?
      HomeQ::SOBS::Server.create_home_queue(config.queue_name,
                                            @handlers[config.queue_name])
      HomeQ::SOBS::Queue.create_queues_from_topology(config.queue_name)
      @cp_server = CP::Server.new(config.cp_host, config.cp_port)
      
      @cp_server.start
      start_servers
      start_queues
      start_periodic_tasks

      @state.started
    }
  end
end

#start_profilingObject

Begin profiling



240
241
242
243
244
245
246
247
248
# File 'lib/homeq/base/system.rb', line 240

def start_profiling
  if !@profiling
    logger.info {
      'Beginning profiling'
    }
    @profiling = true
    RubyProf.start
  end
end

#stopObject

Start a graceful shutdown.



188
189
190
# File 'lib/homeq/base/system.rb', line 188

def stop
  @state.stop
end

#stop_profilingObject

End profiling. Results are printed to STDOUT.



251
252
253
254
255
256
257
258
259
260
261
# File 'lib/homeq/base/system.rb', line 251

def stop_profiling
  if @profiling
    logger.info {
      'Ending profiling'
    }
    @profiling = false
    result = RubyProf.stop
    printer = RubyProf::GraphPrinter.new(result)
    printer.print(STDOUT, 0)
  end
end