#!/usr/bin/perl -w # MAPLAT (C) 2008-2009 Rene Schickbauer # Developed under Artistic license # for Magna Powertrain Ilz use 5.010; use strict; use warnings; BEGIN { unshift @INC, "/home/cavac/src/maplat_prodit/server"; unshift @INC, "/home/cavac/src/maplat_prodit/lib"; unshift @INC, "/home/cavac/src/maplat_logging/lib"; unshift @INC, "/home/cavac/src/maplat_framework/lib"; } use MaplatSVCLinux; use XML::Simple; use Maplat::Helpers::Logo; our $APPNAME = "Maplat SVC"; our $VERSION = 0.995; MaplatLogo($APPNAME, $VERSION); our $isCompiled = 0; if(defined($PerlApp::VERSION)) { $isCompiled = 1; } our $cycleStartTime = 0; # ------------------------------------------ # MAPLAT - Service/Daemon for Linux # ------------------------------------------ my $action = shift @ARGV; if(($action ne "start" && $action ne "stop" && $action ne "reset")) { print "ARGV: " . $#ARGV . "\n"; print("Usage:\n\tmaplat_svc_linux.pl [start|stop|reset] config.xml\n"); exit(1); } my $configfile = shift @ARGV; print "Loading config file $configfile\n"; my $config = XMLin($configfile, ForceArray => ['module', 'run'],); if(defined($config->{basedir})) { print "Changing dir to " . $config->{basedir} . "\n"; chdir $config->{basedir}; } my @modlist = @{$config->{module}}; my $svcserver = new MaplatSVCLinux(0, $config->{basedir}, $config->{memhserver}, $config->{memhnamespace}, $APPNAME, $VERSION, $isCompiled, ); if($action eq "stop") { my $status = $svcserver->getServerStatus(); if($status eq "stopping") { print "STOP already requested\n"; exit(0); } elsif($status eq "stopped") { print "Service not running\n"; exit(0); } $svcserver->requestStop(); print "STOP requested\n"; while(1) { $status = $svcserver->getServerStatus(); last if($status eq "stopped"); print "Waiting for service to stop...\n"; sleep(1); } print "Service shut down.\n"; exit(0); } if($action eq "reset") { print "'Manually' resetting service status fields...\n"; #$svcserver->requestStop(); $svcserver->setServerStatus("stopped"); sleep(5); my $status = $svcserver->getServerStatus(); if($status eq "stopped") { print "Reset seems to have worked.\n"; } else { print "Failed: Got back status '$status'\n"; } exit(0); } # Configure run-once scripts $svcserver->setServerStatus("starting"); if(defined($config->{startup}->{run})) { foreach my $script (@{$config->{startup}->{run}}) { $svcserver->configure_startup($script); } } if(defined($config->{shutdown}->{run})) { foreach my $script (@{$config->{shutdown}->{run}}) { $svcserver->configure_shutdown($script); } } foreach my $module (@modlist) { $svcserver->configure_module($module); } $svcserver->endconfig(); $svcserver->setServerStatus("running"); my $loopcount = 0; while (!$svcserver->shouldStop()) { my $workCount = $svcserver->work(); sleep(1); # Just in case - set our status to running as long as the main loop # runs. This helps prevent some troubles $svcserver->setServerStatus("running"); $loopcount++; if($loopcount == 100) { print "...ping...\n"; $loopcount = 0; } } $svcserver->setServerStatus("stopping"); $svcserver->shutdown; $svcserver->setServerStatus("stopped");