1 # Copyright (C) 2006, David Muir Sharnoff <muir@idiom.com>
3 package Daemon::Generic::While1;
8 require Daemon::Generic;
12 our @ISA = qw(Daemon::Generic Exporter);
13 our @EXPORT = @Daemon::Generic::EXPORT;
18 local($Daemon::Generic::caller) = caller() || 'main';
19 local($Daemon::Generic::package) = __PACKAGE__;
20 Daemon::Generic::newdaemon(@_);
27 $self->{gd_sighup} = time;
31 $self->{gd_sigint} = time;
33 # We'll be getting a SIGTERM in a bit if we're not dead, so let's use it.
36 $self->gd_quit_event();
37 kill(15, $child) if $child; # if we're still alive, let's stay that way
44 my ($self, $period) = @_;
45 croak "Sleep period must be defined" unless defined $period;
47 if ($period*1000 != int($period*1000)) {
50 import Time::HiRes qw(time sleep);
53 while (time - $t < $period) {
54 return if $self->{gd_sigint};
55 return if $self->{gd_sighup};
57 my $p = (time - $t < 1)
71 if ($self->{gd_sigint}) {
72 $self->{gd_sigint} = 0;
73 $self->gd_quit_event();
76 if ($self->{gd_sighup}) {
77 $self->{gd_sighup} = 0;
78 $self->gd_reconfig_event();
88 print STDERR "Reconfiguration requested\n";
89 $self->gd_postconfig($self->gd_preconfig());
94 print STDERR "Quitting...\n";
99 sub gd_run_body { die "must override gd_run_body()" }
105 Daemon::Generic::While1 - Daemon framework with default while(1) loop
109 @ISA = qw(Daemon::Generic::While1);
117 This is a slight variation on L<Daemon::Generic>: a default
118 C<gd_run()> provided. It has a while(1) loop that calls
119 C<gd_run_body()> over and over. It checks for reconifg and
120 and terminate events and only actions them between calls
123 Terminate events will be forced through after
124 C<$Daemon::Generic::force_quit_delay> seconds if
125 C<gd_run_body()> doesn't return quickly enough.
127 =head1 SUBCLASS METHODS REQUIRD
129 The following method is required to be overridden to subclass
130 Daemon::Generic::While1:
136 This method will be called over and over. This method should
137 include a call to C<sleep(1)> (or a bit more). Reconfig events
138 will not interrupt it. Quit events will only interrupt it
143 =head1 ADDITIONAL METHODS
145 The following additional methods are available for your use
146 (as compared to L<Daemon::Generic>):
150 =item gd_sleep($period)
152 This will sleep for C<$period> seconds but in one-second
153 intervals so that if a SIGINT or SIGHUP arrives the sleep
154 period can end more quickly.
156 Using this makes it safe for C<gd_run_body()> to sleep for
157 longer than C<$Daemon::Generic::force_quit_delay> seconds
162 =head1 ADDITIONAL MEMBER DATA
164 The following additional bits of member data are defined:
170 The time at which an (unprocessed) SIGINT was recevied.
174 The time at which an (unprocessed) SIGHUP was recevied.
178 =head1 THANK THE AUTHOR
180 If you need high-speed internet services (T1, T3, OC3 etc), please
181 send me your request-for-quote. I have access to very good pricing:
182 you'll save money and get a great service.
186 Copyright(C) 2006 David Muir Sharnoff <muir@idiom.com>.
187 This module may be used and distributed on the same terms