Class: RubyRest::Engine
- Inherits:
-
Object
- Object
- RubyRest::Engine
- Includes:
- Tools
- Defined in:
- lib/rubyrest/engine.rb
Overview
Objects of this class take a configuration as argument then launch a new server instance.
Constant Summary
Constants included from Tools
Tools::ATOM_DATE_FORMAT, Tools::ERRORS
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Enables external objects to read the engine configuration.
Instance Method Summary collapse
-
#configure_database ⇒ Object
Configures the database connectivity.
-
#initialize(original) ⇒ Engine
constructor
Creates a new RubyRest engine, for the specified hash of configuration data.
-
#start ⇒ Object
Starts the engine.
-
#start_daemon ⇒ Object
Starts the server in another process.
-
#start_server ⇒ Object
Starts the server.
-
#stop ⇒ Object
Shutdowns the current server, if existing.
-
#to_s ⇒ Object
Returns the service name.
Methods included from Tools
#error, #format_atom_date, #nvl, #parse_atom_date, #to_class, #to_gem_name, #to_module_name
Constructor Details
#initialize(original) ⇒ Engine
Creates a new RubyRest engine, for the specified hash of configuration data. The specified hash of params will be ‘interned’ so that config keys are accessible as symbols instead of strings.
21 22 23 24 25 26 27 28 29 30 |
# File 'lib/rubyrest/engine.rb', line 21 def initialize( original ) params = {} original.each{ |k,v| params[ k.intern ] = v } @service = params[ :service ] @prefix = params[ :prefix ] service_module = to_module_name( @prefix, @service ) require @service @config = to_class( service_module, "config" ).new( params ) @config[ :servicemodule ] = service_module end |
Instance Attribute Details
#config ⇒ Object (readonly)
Enables external objects to read the engine configuration
15 16 17 |
# File 'lib/rubyrest/engine.rb', line 15 def config @config end |
Instance Method Details
#configure_database ⇒ Object
Configures the database connectivity
55 56 57 58 59 60 61 62 |
# File 'lib/rubyrest/engine.rb', line 55 def configure_database @config.connect_to_database @config.setup_persistence if @config[ :destroy ] == true @config.init_schema @config.load_initial_data end end |
#start ⇒ Object
Starts the engine. The following operations are accomplished:
1. configure the database
2. launch the webserver, in daemon mode unless false is
specified in the configuration
37 38 39 40 41 42 43 44 |
# File 'lib/rubyrest/engine.rb', line 37 def start configure_database if @config.has( :adapter ) @server = RubyRest::Server.new( @config ) @server.mount "/", CRUDServlet if @config.has( :daemon ) and @config[ :daemon ] == false start_server else start_daemon end end |
#start_daemon ⇒ Object
Starts the server in another process
65 66 67 68 69 70 |
# File 'lib/rubyrest/engine.rb', line 65 def start_daemon @pid = fork do start_server end puts "#{self.to_s}: started process with pid=#{@pid}" end |
#start_server ⇒ Object
Starts the server
73 74 75 76 77 78 |
# File 'lib/rubyrest/engine.rb', line 73 def start_server [ "INT", "TERM" ].each { |signal| trap( signal ) { @server.shutdown } } @server.start end |
#stop ⇒ Object
Shutdowns the current server, if existing. This is only useful when working in daemon mode.
48 49 50 51 52 |
# File 'lib/rubyrest/engine.rb', line 48 def stop return if @pid == nil Process.kill( 9, @pid ) puts "#{self.to_s}: killed process with pid=#{@pid}" end |
#to_s ⇒ Object
Returns the service name
81 82 83 |
# File 'lib/rubyrest/engine.rb', line 81 def to_s "rubyrest: #{@service}" end |