Class: Sanford::Server::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/sanford/server.rb

Constant Summary collapse

DEFAULT_NUM_WORKERS =
4.freeze
DEFAULT_IP_ADDRESS =
'0.0.0.0'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfig

Returns a new instance of Config.



262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/sanford/server.rb', line 262

def initialize
  @name             = nil
  @ip               = DEFAULT_IP_ADDRESS
  @port             = nil
  @pid_file         = nil
  @shutdown_timeout = nil
  @worker_class     = DefaultWorker
  @worker_params    = nil
  @num_workers      = DEFAULT_NUM_WORKERS
  @init_procs       = []
  @error_procs      = []
  @template_source  = Sanford::NullTemplateSource.new(ENV['PWD'])
  @logger           = Sanford::NullLogger.new
  @router           = Sanford::Router.new

  @receives_keep_alive = false
  @verbose_logging     = true

  @valid = nil
end

Instance Attribute Details

#error_procsObject

Returns the value of attribute error_procs.



259
260
261
# File 'lib/sanford/server.rb', line 259

def error_procs
  @error_procs
end

#init_procsObject

Returns the value of attribute init_procs.



259
260
261
# File 'lib/sanford/server.rb', line 259

def init_procs
  @init_procs
end

#ipObject

Returns the value of attribute ip.



257
258
259
# File 'lib/sanford/server.rb', line 257

def ip
  @ip
end

#loggerObject

Returns the value of attribute logger.



259
260
261
# File 'lib/sanford/server.rb', line 259

def logger
  @logger
end

#nameObject

Returns the value of attribute name.



257
258
259
# File 'lib/sanford/server.rb', line 257

def name
  @name
end

#num_workersObject

Returns the value of attribute num_workers.



258
259
260
# File 'lib/sanford/server.rb', line 258

def num_workers
  @num_workers
end

#pid_fileObject

Returns the value of attribute pid_file.



257
258
259
# File 'lib/sanford/server.rb', line 257

def pid_file
  @pid_file
end

#portObject

Returns the value of attribute port.



257
258
259
# File 'lib/sanford/server.rb', line 257

def port
  @port
end

#receives_keep_aliveObject

Returns the value of attribute receives_keep_alive.



260
261
262
# File 'lib/sanford/server.rb', line 260

def receives_keep_alive
  @receives_keep_alive
end

#routerObject

Returns the value of attribute router.



259
260
261
# File 'lib/sanford/server.rb', line 259

def router
  @router
end

#shutdown_timeoutObject

Returns the value of attribute shutdown_timeout.



257
258
259
# File 'lib/sanford/server.rb', line 257

def shutdown_timeout
  @shutdown_timeout
end

#template_sourceObject

Returns the value of attribute template_source.



259
260
261
# File 'lib/sanford/server.rb', line 259

def template_source
  @template_source
end

#verbose_loggingObject

Returns the value of attribute verbose_logging.



260
261
262
# File 'lib/sanford/server.rb', line 260

def verbose_logging
  @verbose_logging
end

#worker_classObject

Returns the value of attribute worker_class.



258
259
260
# File 'lib/sanford/server.rb', line 258

def worker_class
  @worker_class
end

#worker_paramsObject

Returns the value of attribute worker_params.



258
259
260
# File 'lib/sanford/server.rb', line 258

def worker_params
  @worker_params
end

Instance Method Details

#routesObject



283
284
285
# File 'lib/sanford/server.rb', line 283

def routes
  self.router.routes
end

#valid?Boolean

Returns:

  • (Boolean)


287
288
289
# File 'lib/sanford/server.rb', line 287

def valid?
  !!@valid
end

#validate!Object

for the config to be considered “valid”, a few things need to happen. The key here is that this only needs to be done once for each config.



294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
# File 'lib/sanford/server.rb', line 294

def validate!
  return @valid if !@valid.nil? # only need to run this once per config

  # ensure all user and plugin configs/settings are applied
  self.init_procs.each(&:call)
  [:name, :ip, :port].each do |a|
    if self.send(a).nil?
      raise InvalidError, "a name, ip and port must be configured"
    end
  end

  # validate the worker class
  if !self.worker_class.kind_of?(Class) || !self.worker_class.include?(Sanford::Worker)
    raise InvalidError, "worker class must include `#{Sanford::Worker}`"
  end

  # validate the router
  self.router.validate!

  @valid = true # if it made it this far, it's valid!
end