lib/Daemon/Generic/AnyEvent.pm

filter: Statementstmt 97.0% 33 / 34 Branchbran 50.0% 2 / 4 Conditioncond 42.8% 3 / 7 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 100% 10 / 10 Podpod 60.0% 3 / 5 totaltotal 80.9% 51 / 63 CC 6 SCAR 8.2
CC
6
Cov
84%
gd_setup_timer
4.6
__ANON__
2.3
newdaemon
2.0
CRAP 2.3
« lib/Daemon/Generic/Event.pm ↑ Summary
1  
2 package Daemon::Generic::AnyEvent;
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 AnyEvent;
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'
l!l
110
MC/DC0%0/1
caller() || 'main'
caller()
Truth table50%1/2
A
A= caller()
Aresult
11
00
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.1
Pod documented
23 4 my $self = shift;
24  
25 $self->{gd_reload_event} = AnyEvent->signal(
26 signal => 'HUP',
27 1 ▶ cb => sub {
Subroutine __ANON__: called CC=2 SCAR=8.1
28 1 $self->gd_reconfig_event;
29 $self->{gd_timer}->cancel()
30 1 ▶ if $self->{gd_timer};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"gd_timer"} 1 0 1
31 0 $self->gd_setup_timer();
32 },
33 4 );
34 $self->{gd_quit_event} = AnyEvent->signal(
35 signal => 'INT',
36 4 ▶ cb => sub { $self->gd_quit_event; },
Subroutine __ANON__: called CC=1 SCAR=0.0
37 4 );
38 }
39  
40 sub gd_setup_timer
41 4 ▶{
Subroutine gd_setup_timer: called CC=4 SCAR=15.2
Pod undocumented
42 4 my $self = shift;
43 4 ▶ if ($self->can('gd_run_body')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can("gd_run_body")) 4 0 4
44 4 ▶ my $interval = ($self->can('gd_interval') && $self->gd_interval()) || 1;
Condition40%2/5
$self->can('gd_interval') && $self->gd_interval || 1
l!l
40
$self->can("gd_interval") and $self->gd_interval
!ll&&!rl&&r
004
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
ABresult
111
0X0
100
45 $self->{gd_timer} = AnyEvent->timer(
46 146 ▶ cb => sub { $self->gd_run_body() },
Subroutine __ANON__: called CC=1 SCAR=0.0
47 4 interval => $interval,
48 );
49 }
50 }
51  
52 sub gd_run
53 4 ▶{
Subroutine gd_run: called CC=1 SCAR=0.0
Pod documented
54 4 my $self = shift;
55 4 $self->gd_setup_timer();
56 4 Event::loop();
57 }
58  
59 sub gd_quit_event
60 4 ▶{
Subroutine gd_quit_event: called CC=1 SCAR=0.0
Pod documented
61 4 my $self = shift;
62 4 print STDERR "Quitting...\n";
63 4 Event::unloop_all();
64 }
65  
66 11 1;
67  
68 =head1 NAME
69  
70 Daemon::Generic::AnyEvent - Generic daemon framework with AnyEvent.pm
71  
72 =head1 SYNOPSIS
73  
74 use Daemon::Generic::AnyEvent;
75 use Some::Event::Loop::Supported::By::AnyEvent;
76  
77 @ISA = qw(Daemon::Generic::AnyEvent);
78  
79 sub gd_preconfig {
80 # stuff
81 }
82  
83 =head1 DESCRIPTION
84  
85 Daemon::Generic::AnyEvent is a subclass of L<Daemon::Generic> that
86 predefines some methods:
87  
88 =over 15
89  
90 =item gd_run()
91  
92 Setup a periodic callback to C<gd_run_body()> if there is a C<gd_run_body()>.
93 Call C<Event::loop()>.
94  
95 =item gd_setup_signals()
96  
97 Bind SIGHUP to call C<gd_reconfig_event()>.
98 Bind SIGINT to call C<gd_quit_event()>.
99  
100 =back
101  
102 To use Daemon::Generic::Event, you have to provide a C<gd_preconfig()>
103 method. It can be empty if you have a C<gd_run_body()>.
104  
105 Set up your own events in C<gd_preconfig()> and C<gd_postconfig()>.
106  
107 If you have a C<gd_run_body()> method, it will be called once per
108 second or every C<gd_interval()> seconds if you have a C<gd_interval()>
109 method. Unlike in L<Daemon::Generic::While1>, C<gd_run_body()> should
110 not include a call to C<sleep()>.
111  
112 =head1 LICENSE
113  
114 Copyright (C) 2006-2010 David Muir Sharnoff <muir@idiom.com>.
115 Copyright (C) 2011 Google, Inc.
116 This module may be used and distributed on the same terms
117 as Perl itself.
118  
« lib/Daemon/Generic/Event.pm ↑ Summary