| 1 | |||||||||||||||||||||||||
| 2 | package Daemon::Generic::Event; | ||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||
| 4 | 11 | ▶ | use strict; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 5 | 11 | ▶ | use warnings; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 6 | 11 | require Daemon::Generic; | |||||||||||||||||||||||
| 7 | 11 | require Event; | |||||||||||||||||||||||
| 8 | 11 | require Exporter; | |||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||
| 10 | 11 | our @ISA = qw(Daemon::Generic Exporter); | |||||||||||||||||||||||
| 11 | 11 | our @EXPORT = @Daemon::Generic::EXPORT; | |||||||||||||||||||||||
| 12 | 11 | our $VERSION = 0.84; | |||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||
| 14 | sub newdaemon | ||||||||||||||||||||||||
| 15 | 11 | ▶ | { | ||||||||||||||||||||||
|
Subroutine
newdaemon: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||||||||||||||
| 16 | 11 | ▶ | local($Daemon::Generic::caller) = caller() || 'main'; | ||||||||||||||||||||||
|
Condition50%1/2
caller() || 'main'
MC/DC0%0/1
caller() || 'main'
caller()
Truth table50%1/2
A
A= caller()
| |||||||||||||||||||||||||
| 17 | 11 | local($Daemon::Generic::package) = __PACKAGE__; | |||||||||||||||||||||||
| 18 | 11 | Daemon::Generic::newdaemon(@_); | |||||||||||||||||||||||
| 19 | } | ||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||
| 21 | sub gd_setup_signals | ||||||||||||||||||||||||
| 22 | 4 | ▶ | { | ||||||||||||||||||||||
|
Subroutine
gd_setup_signals: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||
| 23 | 4 | my $self = shift; | |||||||||||||||||||||||
| 24 | my $reload_event = Event->signal( | ||||||||||||||||||||||||
| 25 | signal => 'HUP', | ||||||||||||||||||||||||
| 26 | desc => 'reload on SIGHUP', | ||||||||||||||||||||||||
| 27 | prio => 6, | ||||||||||||||||||||||||
| 28 | 1 | ▶ | cb => sub { | ||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||
| 29 | 1 | $self->gd_reconfig_event; | |||||||||||||||||||||||
| 30 | $self->{gd_timer}->cancel() | ||||||||||||||||||||||||
| 31 | 1 | ▶ | if $self->{gd_timer}; | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 32 | 1 | $self->gd_setup_timer(); | |||||||||||||||||||||||
| 33 | }, | ||||||||||||||||||||||||
| 34 | 4 | ); | |||||||||||||||||||||||
| 35 | my $quit_event = Event->signal( | ||||||||||||||||||||||||
| 36 | signal => 'INT', | ||||||||||||||||||||||||
| 37 | 4 | ▶ | cb => sub { $self->gd_quit_event; }, | ||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 38 | 4 | ); | |||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||
| 41 | sub gd_setup_timer | ||||||||||||||||||||||||
| 42 | 5 | ▶ | { | ||||||||||||||||||||||
|
Subroutine
gd_setup_timer: called CC=4 SCAR=15.6
Pod
undocumented
| |||||||||||||||||||||||||
| 43 | 5 | my $self = shift; | |||||||||||||||||||||||
| 44 | 5 | ▶ | if ($self->can('gd_run_body')) { | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 45 | 5 | ▶ | my $interval = ($self->can('gd_interval') && $self->gd_interval()) || 1; | ||||||||||||||||||||||
|
Condition40%2/5
$self->can('gd_interval') && $self->gd_interval || 1
$self->can("gd_interval") and $self->gd_interval
MC/DC0%0/2
$self->can('gd_interval') && $self->gd_interval || 1
$self->can("gd_interval")
$self->gd_interval
Truth table33%1/3
A and B
A= $self->can("gd_interval")B= $self->gd_interval
| |||||||||||||||||||||||||
| 46 | 5 | $self->{gd_timer} = Event->timer( | |||||||||||||||||||||||
| 47 | cb => [ $self, 'gd_run_body' ], | ||||||||||||||||||||||||
| 48 | interval => $interval, | ||||||||||||||||||||||||
| 49 | hard => 0, | ||||||||||||||||||||||||
| 50 | ); | ||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||
| 54 | sub gd_run | ||||||||||||||||||||||||
| 55 | 4 | ▶ | { | ||||||||||||||||||||||
|
Subroutine
gd_run: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||
| 56 | 4 | my $self = shift; | |||||||||||||||||||||||
| 57 | 4 | $self->gd_setup_timer(); | |||||||||||||||||||||||
| 58 | 4 | Event::loop(); | |||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||
| 61 | sub gd_quit_event | ||||||||||||||||||||||||
| 62 | 4 | ▶ | { | ||||||||||||||||||||||
|
Subroutine
gd_quit_event: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||
| 63 | 4 | my $self = shift; | |||||||||||||||||||||||
| 64 | 4 | print STDERR "Quitting...\n"; | |||||||||||||||||||||||
| 65 | 4 | Event::unloop_all(); | |||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||
| 68 | 11 | 1; | |||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||
| 70 | =head1 NAME | ||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||
| 72 | Daemon::Generic::Event - Generic daemon framework with Event.pm | ||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||
| 74 | =head1 SYNOPSIS | ||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||
| 76 | use Daemon::Generic::Event; | ||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||
| 78 | @ISA = qw(Daemon::Generic::Event); | ||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||
| 80 | sub gd_preconfig { | ||||||||||||||||||||||||
| 81 | # stuff | ||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||
| 84 | =head1 DESCRIPTION | ||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||
| 86 | Daemon::Generic::Event is a subclass of L<Daemon::Generic> that | ||||||||||||||||||||||||
| 87 | predefines some methods: | ||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||
| 89 | =over 15 | ||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||
| 91 | =item gd_run() | ||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||
| 93 | Setup a periodic callback to C<gd_run_body()> if there is a C<gd_run_body()>. | ||||||||||||||||||||||||
| 94 | Call C<Event::loop()>. | ||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||
| 96 | =item gd_setup_signals() | ||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||
| 98 | Bind SIGHUP to call C<gd_reconfig_event()>. | ||||||||||||||||||||||||
| 99 | Bind SIGINT to call C<gd_quit_event()>. | ||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||
| 101 | =back | ||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||
| 103 | To use Daemon::Generic::Event, you have to provide a C<gd_preconfig()> | ||||||||||||||||||||||||
| 104 | method. It can be empty if you have a C<gd_run_body()>. | ||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||
| 106 | Set up your own events in C<gd_preconfig()> and C<gd_postconfig()>. | ||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||
| 108 | If you have a C<gd_run_body()> method, it will be called once per | ||||||||||||||||||||||||
| 109 | second or every C<gd_interval()> seconds if you have a C<gd_interval()> | ||||||||||||||||||||||||
| 110 | method. Unlike in L<Daemon::Generic::While1>, C<gd_run_body()> should | ||||||||||||||||||||||||
| 111 | not include a call to C<sleep()>. | ||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||
| 113 | =head1 LICENSE | ||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||
| 115 | Copyright (C) 2006-2010 David Muir Sharnoff <muir@idiom.com>. | ||||||||||||||||||||||||
| 116 | Copyright (C) 2011 Google, Inc. | ||||||||||||||||||||||||
| 117 | This module may be used and distributed on the same terms | ||||||||||||||||||||||||
| 118 | as Perl itself. | ||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||