NetSDS::App - common application superclass
MyApp->run( conf_file => '/etc/NetSDS/myapp.conf', daemon => 1, use_pidfile => 1, ); package MyApp; use base 'NetSDS::App'; sub process { my ($this) = @_; print "Hello!"; }
NetSDS::App provides common application functionality implemented as superclass to inherit real applications from it.
NetSDS::App
Common application workflow is looking like this:
start() | process() | stop()
It may be redefined in main_loop() method if necessary. But about 90% of applications will match this scheme.
main_loop()
So if you need to implement some common logic, it's necessary to rewrite start(), process() and stop() methods.
start()
process()
stop()
Constructor is usually invoked from run() class method. It creates application object and set its initial properties from oarameters passed as hash.
run()
Standard parameters are:
* name - application name * debug - set to 1 for debugging * daemon - set to 1 for daemonization * verbose - set to 1 for more verbosity * use_pidfile - set to 1 for PID files processing * pid_dir - path to PID files catalog * conf_file - path to configuration file * has_conf - set to 1 if configuration file is necessary * auto_features - set to 1 for auto features inclusion * infinite - set to 1 for inifinite loop
This method calls class constructor and then switch to main_loop() method.
All method parameters are transparently passed to application constructor.
MyApp->run( conf_file => '/etc/myapp.conf', daemon => 1, ); 1; package MyApp; use base 'NetSDS::App'; 1;
This method is an accessor to application name allowing to retrieve this or set new one.
print "My name is " . $this->name;
Read only access to process identifier (PID).
print "My PID is " . $this->pid;
This method provides an accessor to debugging flag. If application called with --debug option it will return TRUE value.
if ($this->debug) { print "Debug info: " . $debug_data; }
This method provides an accessor to verbosity flag.
It may be used to increase application verbosity level if necessary.
if ($this->verbose) { print "I'm working!"; };
NOTE: This flag is is for normal operations. If you need implement debug output or other development/testing functionality - use debug() instead.
This method is accessor to logger (object of NetSDS::Logger class).
NOTE: There is no need to use this method directly in application. See log() method description to understand logging features.
log()
This method is accessor to application configuration represented as hash reference returned by NetSDS::Conf module.
Configuration sample:
------------------------ content_dir /var/lib/content <kannel> send_url http://127.0.0.1:13013/ login netsds passwd topsecret </kannel> ------------------------
Code sample:
# Retrieve configuration my $content_dir = $this->conf->{content_dir}; my $kannel_url = $this->conf->{kannel}->{send_url};
Paramters: TRUE if PID file checking required
Paramters: directory name
$app->pid_dir("/var/run");
Paramters: TRUE if application should be a daemon
if ($this->daemon()) { $this->log("info", "Yeah! I'm daemon!"); };
Automatic features inclusion allowed if TRUE.
$app->infinite(1); # set infinite loop
Common application initialization:
1. Reading config if necessary.
2. Daemonize application.
3. Check PID file for already running application instances.
4. Start logger.
5. Prepare default signal handlers.
This method implements automatic features inclusion by application configuration file (see feature sections).
feature
Paramters: feature name, class name, parameters (optional)
Returns: feature object
$this->add_feature('kannel','NetSDS::Feature::Kannel', $this->conf->{feature}->{kannel}); $this->kannel->send(.....);
This method called if we need to finish application.
Abstract method for postinitialization procedures execution.
Arguments and return defined in inherited classes. This method should be overwritten in exact application.
Remember that start() methhod is invoked after initialize()
Abstract method for main loop iteration procedures execution.
Arguments and return defined in inherited classes.
This method should be overwritten in exact application.
This method should be rewritten in target class to contain real post processing routines.
This method provide default main loop alghorythm implementation and may be rewritten for alternative logic.
This method provides ablity to write log messages to syslog.
Example:
$this->log("info", "New message arrived with id=$msg_id");
This method extends inherited method functionality with automatically logging this message to syslog.
if (!$dbh->ping) { return $this->error("We have problem with DBMS"); }
Paramters: list of strings to be written as verbose output
This method implements verbose output to STDOUT.
$this->speak("Do something");
See samples/app.pl
This module is a one bug itself :-)
NetSDS, NetSDS::Class::Abstract, NetSDS::Logger
Fix and cleanup!
Valentyn Solomko <val@pere.org.ua>
Michael Bochkaryov <misha@rattler.kiev.ua>
To install NetSDS, copy and paste the appropriate command in to your terminal.
cpanm
cpanm NetSDS
CPAN shell
perl -MCPAN -e shell install NetSDS
For more information on module installation, please visit the detailed CPAN module installation guide.