File Coverage

blib/lib/Rex/Notify.pm
Criterion Covered Total %
statement 19 42 45.2
branch 0 8 0.0
condition 1 14 7.1
subroutine 4 7 57.1
pod 0 4 0.0
total 24 75 32.0


line stmt bran cond sub pod time code
1             #
2             # (c) Jan Gehring
3             #
4              
5             package Rex::Notify;
6              
7 102     102   1171 use v5.12.5;
  102         296  
8 102     102   484 use warnings;
  102         215  
  102         67976  
9              
10             our $VERSION = '1.14.3'; # VERSION
11              
12             sub new {
13 103     103 0 322 my $that = shift;
14 103   33     916 my $proto = ref($that) || $that;
15 103         319 my $self = {@_};
16              
17 103         273 bless( $self, $proto );
18              
19 103         686 $self->{__types__} = {};
20 103         412 $self->{__postponed__} = [];
21 103         286 $self->{__running_postponed__} = 0;
22 103         252 $self->{__in_notify__} = 0;
23              
24 103         2498 return $self;
25             }
26              
27             sub add {
28 0     0 0 0 my ( $self, %option ) = @_;
29              
30 0 0       0 return if ( $self->{__in_notify__} );
31              
32 0 0       0 if ( exists $self->{__types__}->{ $option{type} }->{ $option{name} } ) {
33 0         0 Rex::Logger::debug(
34             "A resource of the type $option{type} and name $option{name}"
35             . "already exists.",
36             "warn"
37             );
38 0         0 return;
39             }
40              
41             $self->{__types__}->{ $option{type} }->{ $option{name} } = {
42             postpone => $option{postpone} || 0,
43             options => $option{options},
44             cb => $option{cb},
45 0   0     0 };
46             }
47              
48             sub run {
49 0     0 0 0 my ( $self, %option ) = @_;
50              
51 0         0 Rex::Logger::debug("Try to notify $option{type} -> $option{name}");
52              
53 0 0 0     0 if ( exists $self->{__types__}->{ $option{type} }
      0        
      0        
54             && exists $self->{__types__}->{ $option{type} }->{ $option{name} }
55             && exists $self->{__types__}->{ $option{type} }->{ $option{name} }->{cb}
56             && $self->{__types__}->{ $option{type} }->{ $option{name} }->{postpone} ==
57             0 )
58             {
59 0         0 Rex::Logger::debug("Running notify $option{type} -> $option{name}");
60              
61 0         0 my $cb = $self->{__types__}->{ $option{type} }->{ $option{name} }->{cb};
62              
63 0         0 $self->{__in_notify__} = 1;
64              
65             $cb->(
66 0         0 $self->{__types__}->{ $option{type} }->{ $option{name} }->{options} );
67              
68 0         0 $self->{__in_notify__} = 0;
69             }
70             else {
71 0 0       0 if ( !$self->{__running_postponed__} ) {
72 0         0 Rex::Logger::debug(
73             "Can't notify $option{type} -> $option{name}. Postponing...");
74 0         0 $self->_postpone(%option);
75             }
76             else {
77 0         0 Rex::Logger::info(
78             "Can't run postponed notification. "
79             . "Resource not found ($option{type} -> $option{name})",
80             "warn"
81             );
82             }
83             }
84              
85             }
86              
87             sub run_postponed {
88 47     47 0 203 my ($self) = @_;
89 47         201 $self->{__running_postponed__} = 1;
90 47         305 Rex::Logger::debug("Running postponed notifications.");
91 47         144 for my $p ( @{ $self->{__postponed__} } ) {
  47         653  
92 0           $self->run( %{$p} );
  0            
93             }
94             }
95              
96             sub _postpone {
97 0     0     my ( $self, %option ) = @_;
98 0           push @{ $self->{__postponed__} }, \%option;
  0            
99             }
100              
101             1;