server.rb |
|
---|---|
A 1) Instantiated via 2) Instantiated via 3) Instantiated via 4) Instantiated explicitly and manually configured, for maximum control. All of these different workflows are supported, but if you are simply looking
to build applications with
|
module Newman
class Server |
|
|
The following example demonstrates how to use this method:
Given a proper configuration file, this will make it possible to easily get your applications up and running with simple request and response logging enabled. |
def self.simple!(app, settings_file)
settings = Settings.from_file(settings_file)
mailer = Mailer.new(settings)
server = new(settings, mailer)
server.apps = [RequestLogger, app, ResponseLogger]
server
end
|
|
|
|
def self.simple(app, settings_file)
server = simple!(app, settings_file)
server.run
end |
|
|
Using the application from the
It’s worth mentioning that although |
def self.test_mode(settings_file)
settings = Settings.from_file(settings_file)
mailer = TestMailer.new(settings)
new(settings, mailer)
end |
|
|
To initialize a Instantiating a server object directly can be useful for building live integration tests, or for building cron jobs which process email periodically rather than in a busy-wait loop. See one of Newman’s live tests for an example of how this approach works. |
def initialize(settings, mailer)
self.settings = settings
self.mailer = mailer
self.apps = []
end |
|
|
These accessors are mostly meant for use with server objects under test
mode, or server objects that have been explicitly instantiated. If you are
using |
attr_accessor :settings, :mailer, :apps
attr_writer :logger |
|
|
Returns the logger object that was set via |
def logger
@logger || default_logger
end |
|
|
|
def run
loop do
tick
sleep settings.service.polling_interval
end
end |
|
|
1) A response is generated with the TO field set to the FROM field of the
request, and the FROM field set to 2) The list of 2a) If any application raises an exception, that exception is caught and
the processing of the current request is halted. Details about the failure
are logged and if 2b) If there are any server errors (such as an error retrieving messages via IMAP), those errors are logged and re-raised, taking the server down. Currently, you should use a process watcher to restart Newman to protect against such failures, but be careful about restarting without knowing what went wrong! 3) Assuming an exception is not encountered, the response is delivered. |
def tick
mailer.messages.each do |request|
response = mailer.new_message(:to => request.from,
:from => settings.service.default_sender)
begin
apps.each do |app|
app.call(:request => request,
:response => response,
:settings => settings,
:logger => logger)
end
rescue StandardError => e
logger.info("FAIL") { e.to_s }
logger.debug("FAIL") { "#{e.inspect}\n"+e.backtrace.join("\n ") }
if settings.service.raise_exceptions
raise
else
next
end
end
response.deliver
end
rescue Exception => e
logger.fatal("Caught exception: #{e}\n\n#{e.backtrace.join("\n")}")
raise
end |
|
|
NOTE: Methods below this point in the file are implementation details, and should not be depended upon |
private |
|
|
|
def default_logger
self.logger = Logger.new(STDERR)
if settings.service.debug_mode || $DEBUG
logger.level = Logger::DEBUG
else
logger.level = Logger::INFO
end
logger
end
end
end |