| 1 | package App::Standby::Service; | ||||||||||
| 2 | 2 | $App::Standby::Service::VERSION = '0.04'; | |||||||||
| 3 | 2 | ▶ | BEGIN { | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 2 | $App::Standby::Service::AUTHORITY = 'cpan:TEX'; | |||||||||
| 5 | } | ||||||||||
| 6 | # ABSTRACT: Service Plugin baseclass | ||||||||||
| 7 | |||||||||||
| 8 | 2 | ▶ | use 5.010_000; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 2 | ▶ | use mro 'c3'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 2 | ▶ | use feature ':5.10'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | |||||||||||
| 12 | 2 | ▶ | use Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | 2 | ▶ | use namespace::autoclean; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | |||||||||||
| 15 | # use IO::Handle; | ||||||||||
| 16 | # use autodie; | ||||||||||
| 17 | # use MooseX::Params::Validate; | ||||||||||
| 18 | # use Carp; | ||||||||||
| 19 | # use English qw( -no_match_vars ); | ||||||||||
| 20 | # use Try::Tiny; | ||||||||||
| 21 | |||||||||||
| 22 | # extends ... | ||||||||||
| 23 | 2 | extends 'App::Standby'; | |||||||||
| 24 | # has ... | ||||||||||
| 25 | 2 | has 'name' => ( | |||||||||
| 26 | 'is' => 'ro', | ||||||||||
| 27 | 'isa' => 'Str', | ||||||||||
| 28 | 'required' => 1, | ||||||||||
| 29 | ); | ||||||||||
| 30 | |||||||||||
| 31 | 2 | has 'description' => ( | |||||||||
| 32 | 'is' => 'rw', | ||||||||||
| 33 | 'isa' => 'Str', | ||||||||||
| 34 | 'required' => 0, | ||||||||||
| 35 | ); | ||||||||||
| 36 | |||||||||||
| 37 | 2 | has 'group_id' => ( | |||||||||
| 38 | 'is' => 'ro', | ||||||||||
| 39 | 'isa' => 'Int', | ||||||||||
| 40 | 'required' => 1, | ||||||||||
| 41 | ); | ||||||||||
| 42 | # with ... | ||||||||||
| 43 | # initializers ... | ||||||||||
| 44 | |||||||||||
| 45 | # your code here ... | ||||||||||
| 46 | 0 | ▶ | sub update { | ||||||||
|
Subroutine
update: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 47 | 0 | my $self = shift; | |||||||||
| 48 | 0 | my $user_ref = shift; | |||||||||
| 49 | |||||||||||
| 50 | # this base class does nothing here | ||||||||||
| 51 | # subclasses should override this method | ||||||||||
| 52 | # and update their services here ... | ||||||||||
| 53 | 0 | my $new_user_ref = $self->_inject_per_user_config($user_ref); | |||||||||
| 54 | |||||||||||
| 55 | 0 | return $self->_update($new_user_ref); | |||||||||
| 56 | } | ||||||||||
| 57 | |||||||||||
| 58 | 0 | ▶ | sub _update { | ||||||||
|
Subroutine
_update: not called CC=1 SCAR=6.9
| |||||||||||
| 59 | 0 | my $self = shift; | |||||||||
| 60 | 0 | my $user_ref = shift; | |||||||||
| 61 | # this base class does nothing here | ||||||||||
| 62 | # subclasses should override this method | ||||||||||
| 63 | # and update their services here ... | ||||||||||
| 64 | 0 | return; | |||||||||
| 65 | } | ||||||||||
| 66 | |||||||||||
| 67 | 0 | ▶ | sub _inject_per_user_config { | ||||||||
|
Subroutine
_inject_per_user_config: not called CC=8 SCAR=42.8
| |||||||||||
| 68 | 0 | my $self = shift; | |||||||||
| 69 | 0 | my $user_ref = shift; | |||||||||
| 70 | |||||||||||
| 71 | 0 | my $sql = 'SELECT id,key,value FROM config_contacts WHERE contact_id = ?'; | |||||||||
| 72 | 0 | my $sth = $self->dbh()->prepare($sql); | |||||||||
| 73 | 0 | ▶ | if(!$sth) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 74 | 0 | $self->logger()->log( message => 'Failed to prepare SQL '.$sql.' w/ error: '.$self->dbh()->errstr, level => 'error', ); | |||||||||
| 75 | 0 | return; | |||||||||
| 76 | } | ||||||||||
| 77 | |||||||||||
| 78 | 0 | my @new_users = (); | |||||||||
| 79 | |||||||||||
| 80 | 0 | foreach my $user (@{$user_ref}) { | |||||||||
| 81 | 0 | ▶ | if(!$sth->execute($user->{'id'})) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 82 | 0 | $self->logger()->log( message => 'Failed to execute stmt w/ error: '.$sth->errstr, level => 'error', ); | |||||||||
| 83 | } | ||||||||||
| 84 | |||||||||||
| 85 | # copy existing values | ||||||||||
| 86 | 0 | my $new_user = {}; | |||||||||
| 87 | 0 | foreach my $key (keys %{$user}) { | |||||||||
| 88 | 0 | $new_user->{$key} = $user->{$key}; | |||||||||
| 89 | } | ||||||||||
| 90 | # add new values | ||||||||||
| 91 | 0 | ▶ | while(my ($cconfig_id,$cconfig_key,$cconfig_value) = $sth->fetchrow_array()) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 92 | 0 | my $service_name = $self->name(); | |||||||||
| 93 | # only use config items for this service | ||||||||||
| 94 | 0 | ▶ | next unless $cconfig_key =~ m/^${service_name}_/; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 95 | # strip leading serice name prefix | ||||||||||
| 96 | 0 | $cconfig_key =~ s/^${service_name}_//i; | |||||||||
| 97 | 0 | ▶ | if($new_user->{$cconfig_key}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 98 | 0 | $self->logger()->log( message => 'Per-user config item '.$cconfig_key.' collides w/ existing value. Overwriting global item with per-user item', level => 'warning', ); | |||||||||
| 99 | } | ||||||||||
| 100 | 0 | $new_user->{$cconfig_key} = $cconfig_value; | |||||||||
| 101 | } | ||||||||||
| 102 | |||||||||||
| 103 | 0 | push(@new_users,$new_user); | |||||||||
| 104 | } | ||||||||||
| 105 | |||||||||||
| 106 | 0 | $sth->finish(); | |||||||||
| 107 | |||||||||||
| 108 | 0 | return \@new_users; | |||||||||
| 109 | } | ||||||||||
| 110 | |||||||||||
| 111 | 0 | ▶ | sub _config_values { | ||||||||
|
Subroutine
_config_values: not called CC=1 SCAR=6.9
| |||||||||||
| 112 | 0 | my $self = shift; | |||||||||
| 113 | 0 | my $key = shift; | |||||||||
| 114 | |||||||||||
| 115 | 0 | return $self->SUPER::_config_values($key, $self->group_id()); | |||||||||
| 116 | } | ||||||||||
| 117 | |||||||||||
| 118 | 0 | ▶ | sub _config_value { | ||||||||
|
Subroutine
_config_value: not called CC=2 SCAR=17.9
| |||||||||||
| 119 | 0 | my $self = shift; | |||||||||
| 120 | 0 | my $key = shift; | |||||||||
| 121 | |||||||||||
| 122 | 0 | my $vals = $self->_config_values($key); | |||||||||
| 123 | 0 | ▶ | if(ref($vals) eq 'ARRAY') { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 124 | 0 | return $vals->[0]; | |||||||||
| 125 | } else { | ||||||||||
| 126 | 0 | return; | |||||||||
| 127 | } | ||||||||||
| 128 | } | ||||||||||
| 129 | |||||||||||
| 130 | 2 | ▶ | no Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 131 | 2 | __PACKAGE__->meta->make_immutable; | |||||||||
| 132 | |||||||||||
| 133 | 2 | 1; | |||||||||
| 134 | |||||||||||
| 135 | __END__ | ||||||||||
| 136 | |||||||||||
| 137 | =pod | ||||||||||
| 138 | |||||||||||
| 139 | =encoding utf-8 | ||||||||||
| 140 | |||||||||||
| 141 | =head1 NAME | ||||||||||
| 142 | |||||||||||
| 143 | App::Standby::Service - Service Plugin baseclass | ||||||||||
| 144 | |||||||||||
| 145 | =head1 ATTRIBUTES | ||||||||||
| 146 | |||||||||||
| 147 | =head2 name | ||||||||||
| 148 | |||||||||||
| 149 | The name of this plugin. SHOULD be lowercase. SHOULD also used as a key prefix in the | ||||||||||
| 150 | config table. | ||||||||||
| 151 | |||||||||||
| 152 | =head2 description | ||||||||||
| 153 | |||||||||||
| 154 | A human readable description of this service plugin. Used for display and logging. | ||||||||||
| 155 | MAY be empty. | ||||||||||
| 156 | |||||||||||
| 157 | =head2 group_id | ||||||||||
| 158 | |||||||||||
| 159 | The numeric group id of the group this service plugins instance is associated with. | ||||||||||
| 160 | MUST be numeric and MUST NOT be empty. | ||||||||||
| 161 | |||||||||||
| 162 | =head1 METHODS | ||||||||||
| 163 | |||||||||||
| 164 | =head2 update | ||||||||||
| 165 | |||||||||||
| 166 | This method is called with a array_ref containing the new ordering on any changed. | ||||||||||
| 167 | |||||||||||
| 168 | =head1 NAME | ||||||||||
| 169 | |||||||||||
| 170 | App::Standby::Service - Service Plugin baseclass | ||||||||||
| 171 | |||||||||||
| 172 | =head1 ADDING A NEW SERVICE | ||||||||||
| 173 | |||||||||||
| 174 | First of all there are two kinds of services: Simple HTTP endpoints and complex plugins. | ||||||||||
| 175 | |||||||||||
| 176 | The simple HTTP plugins just receive the whole queue in as JSON encoded array. | ||||||||||
| 177 | Those only need to subclass App::Standby::Service::HTTP and provide an implementation | ||||||||||
| 178 | for _init_endpoints. Have a look at the simple example. | ||||||||||
| 179 | |||||||||||
| 180 | All other services will need to subclass App::Standby::Service and implement an | ||||||||||
| 181 | update() method. Have a look at App::Standby::Service::Pingom for an example. | ||||||||||
| 182 | |||||||||||
| 183 | The method _config_values helps with getting values to known keys from | ||||||||||
| 184 | the config table. A service plugin MUST always prepend its name to | ||||||||||
| 185 | the key to allow for multiple instances of one plugin registered at | ||||||||||
| 186 | the same time. | ||||||||||
| 187 | |||||||||||
| 188 | =head1 AUTHOR | ||||||||||
| 189 | |||||||||||
| 190 | Dominik Schulz <dominik.schulz@gauner.org> | ||||||||||
| 191 | |||||||||||
| 192 | =head1 COPYRIGHT AND LICENSE | ||||||||||
| 193 | |||||||||||
| 194 | This software is copyright (c) 2012 by Dominik Schulz. | ||||||||||
| 195 | |||||||||||
| 196 | This is free software; you can redistribute it and/or modify it under | ||||||||||
| 197 | the same terms as the Perl 5 programming language system itself. | ||||||||||
| 198 | |||||||||||
| 199 | =cut | ||||||||||