summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2011-03-07 20:44:54 +0000
committerPaul Buetow <paul@buetow.org>2011-03-07 20:44:54 +0000
commit894a911a52a7689c5e19b3b5f9e70a793b625a55 (patch)
treecc27caa8a1126915483a989f05ad6ab8fec57e14
parent60abe9cbd2a702a86b90730cb4cda0b48094939c (diff)
oop
-rw-r--r--CHANGELOG4
-rw-r--r--perldaemon.conf1
-rw-r--r--perldaemon.pl154
3 files changed, 91 insertions, 68 deletions
diff --git a/CHANGELOG b/CHANGELOG
index 44ecd25..e909e10 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -1,3 +1,7 @@
+Mo 7. Mär 21:43:23 CET 2011
+* Introduced simple OOP to the PerlDaemon
+ Added a Logger and a DaemonLogic class.
+
So 6. Feb 13:14:32 CET 2011
* PerlDaemon is now CamelCase
diff --git a/perldaemon.conf b/perldaemon.conf
index cf25e30..d066d88 100644
--- a/perldaemon.conf
+++ b/perldaemon.conf
@@ -2,3 +2,4 @@
daemon.wd = ./
daemon.pidfile = ./run/perldaemon.pid
daemon.logfile = ./log/perldaemon.log
+daemon.require = ./lib/require.pm
diff --git a/perldaemon.pl b/perldaemon.pl
index f876140..6aa6def 100644
--- a/perldaemon.pl
+++ b/perldaemon.pl
@@ -4,35 +4,22 @@
use strict;
use warnings;
-use POSIX qw(setsid strftime);
-use Shell qw(mv);
-use constant VERSION => 'Daemon v1';
+package Logger;
+use Shell qw(mv);
+use POSIX qw(strftime);
$| = 1;
-sub trimstr (@) {
- my @str = @_;
-
- for (@str) {
- chomp;
- s/^[\t\s]+//;
- s/[\t\s]+$//;
- }
-
- return @str;
-}
-
-sub trunc ($) {
- my $file = shift;
- open my $fh, ">$file" or die "Can't write $file: $!\n";
- print $fh '';
- close $fh;
+sub new ($$) {
+ my ($class, $conf) = @_;
+ return bless { conf => $conf }, $class;
}
sub logmsg ($$) {
- my ($config, $msg) = @_;
- my $logfile = $config->{'daemon.logfile'};
+ my ($self, $msg) = @_;
+ my $conf = $self->{conf};
+ my $logfile = $conf->{'daemon.logfile'};
open my $fh, ">>$logfile" or die "Can't write logfile $logfile: $!\n";
print $fh localtime()." (PID $$): $msg\n";
@@ -40,54 +27,83 @@ sub logmsg ($$) {
}
sub err ($$) {
- my ($config, $msg) = @_;
- logmsg $config => $msg;
+ my ($self, $msg) = @_;
+ $self->logmsg($msg);
die "$msg\n";
}
sub rotatelog ($) {
- my $config = shift;
- my $logfile = $config->{'daemon.logfile'};
+ my $self = shift;
+ my $conf = $self->{conf};
+ my $logfile = $conf->{'daemon.logfile'};
- logmsg $config => 'Rotating logfile';
+ $self->logmsg('Rotating logfile');
my $timestr = strftime "%Y%m%d-%H%M%S", localtime();
mv($logfile, "$logfile.$timestr");
}
-sub checkpid ($) {
- my $config = shift;
+package PerlDaemon;
+
+use POSIX qw(setsid);
+use DaemonLogic;
+
+$| = 1;
+
+sub trimstr (@) {
+ my @str =
+ @_;
- my $pidfile = $config->{'daemon.pidfile'};
+ for (@str) {
+ chomp;
+ s/^[\t\s]+//;
+ s/[\t\s]+$//;
+ }
+
+ return @str;
+}
+
+sub trunc ($) {
+ my $file = shift;
+ open my $fh, ">$file" or die "Can't write $file: $!\n";
+ print $fh '';
+ close $fh;
+}
+
+sub checkpid ($) {
+ my $conf = shift;
+ my $pidfile = $conf->{'daemon.pidfile'};
+ my $logger = $conf->{logger};
trunc $pidfile unless -f $pidfile;
- open my $fh, $pidfile or err $config => "Can't read pidfile $pidfile: $!";
+ open my $fh, $pidfile or $logger->err("Can't read pidfile $pidfile: $!");
my ($pid) = <$fh>;
close $fh;
if (defined $pid) {
chomp $pid;
- err $config => "Process with pid $pid already running" if 0 < int $pid && kill 0, $pid;
+ $logger->err("Process with pid $pid already running") if 0 < int $pid && kill 0, $pid;
}
}
sub writepid ($) {
- my $config = shift;
+ my $conf = shift;
+ my $logger = $conf->{logger};
- my $pidfile = $config->{'daemon.pidfile'};
+ my $pidfile = $conf->{'daemon.pidfile'};
- open my $fh, ">$pidfile" or err $config => "Can't write pidfile: $!";
+ open my $fh, ">$pidfile" or $logger->err("Can't write pidfile: $!");
print $fh "$$\n";
close $fh;
}
-sub readconfig ($) {
- my $configfile = shift;
+sub readconf ($) {
+ my $conffile = shift;
- open my $fh, $configfile or die "Can't read $configfile\n";
- my %config;
+ open my $fh, $conffile or die "Can't read $conffile\n";
+ my %conf;
while (<$fh>) {
next if /^[\t\w]+#/;
@@ -96,7 +112,7 @@ sub readconfig ($) {
my ($key, $val) = trimstr split '=', $_, 2;
next unless defined $val;
- $config{$key} = $val;
+ $conf{$key} = $val;
}
close $fh;
@@ -106,72 +122,74 @@ sub readconfig ($) {
foreach (qw(wd pidfile logfile)) {
my $key = "daemon.$_";
- die "$msg $key\n" unless exists $config{$key};
+ die "$msg $key\n" unless exists $conf{$key};
}
- logmsg \%config => "Reading $configfile complete";
- return \%config;
+ return \%conf;
}
sub daemonize ($) {
- my $config = shift;
- logmsg $config => 'Daemonizing...';
+ my $conf = shift;
+ my $logger = $conf->{logger};
+ $logger->logmsg('Daemonizing...');
- chdir $config->{'daemon.wd'} or err $config => "Can't chdir to wd: $!";
+ chdir $conf->{'daemon.wd'} or $logger->err("Can't chdir to wd: $!");
my $msg = 'Can\'t read /dev/null:';
- open STDIN, '>/dev/null' or err $config => "$msg $!";
- open STDOUT, '>/dev/null' or err $config => "$msg $!";
- open STDERR, '>/dev/null' or err $config => "$msg $!";
+ open STDIN, '>/dev/null' or $logger->err("$msg $!");
+ open STDOUT, '>/dev/null' or $logger->err("$msg $!");
+ open STDERR, '>/dev/null' or $logger->err("$msg $!");
- defined (my $pid = fork) or err $config => "Can't fork: $!";
+ defined (my $pid = fork) or $logger->err("Can't fork: $!");
exit if $pid;
- setsid or err $config => "Can't start a new session: $!";
-
- writepid $config;
+ setsid or $logger->err("Can't start a new session: $!");
- logmsg $config => 'Daemonizing completed';
+ writepid $conf;
+ $logger->logmsg('Daemonizing completed');
}
sub sighandlers ($) {
- my $config = shift;
+ my $conf = shift;
+ my $logger = $conf->{logger};
$SIG{TERM} = sub {
# On shutdown
- logmsg $config => 'Received SIGTERM. Shutting down....';
- unlink $config->{'daemon.pidfile'};
+ $logger->logmsg('Received SIGTERM. Shutting down....');
+ unlink $conf->{'daemon.pidfile'};
exit 0;
};
$SIG{HUP} = sub {
# On logrotate
- logmsg $config => 'Received SIGHUP.';
- rotatelog $config;
+ $logger->logmsg('Received SIGHUP.');
+ $logger->rotatelog();
};
}
sub prestartup ($) {
- my $config = shift;
- checkpid $config;
+ my $conf = shift;
+ checkpid $conf;
}
sub daemonloop ($) {
- my $config = shift;
+ my $conf = shift;
+ my $dlogic = DaemonLogic->new($conf);
my $loop = shift;
for (my $i = 1;;++$i) {
- logmsg $config => VERSION . ": Hello $i";
+ $dlogic->do();
sleep 3;
}
}
-my $config = readconfig shift;
+my $conf = readconf shift;
+$conf->{logger} = Logger->new($conf);
-prestartup $config;
-daemonize $config;
-sighandlers $config;
-daemonloop $config;
+prestartup $conf;
+daemonize $conf;
+sighandlers $conf;
+daemonloop $conf;