| 1 | package Zonemaster::Backend::Config::DCPlugin; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | =head1 NAME | ||
| 7 | |||
| 8 | Zonemaster::Backend::Config::DCPlugin - Daemon::Control plugin that | ||
| 9 | loads the backend configuration. | ||
| 10 | |||
| 11 | =head1 SYNOPSIS | ||
| 12 | |||
| 13 | Provides validated and sanity-checked backend configuration through the | ||
| 14 | L<config>, L<db> and L<pm> properties. | ||
| 15 | |||
| 16 | my $daemon = Daemon::Control | ||
| 17 | ->with_plugins('+Zonemaster::Backend::Config::DCPlugin') | ||
| 18 | ->new({ | ||
| 19 | program => sub { | ||
| 20 | my $self = shift; | ||
| 21 | |||
| 22 | $self->init_backend_config(); | ||
| 23 | |||
| 24 | my $config = $self->config; | ||
| 25 | my $db = $self->db; | ||
| 26 | my $pm = $self->pm; | ||
| 27 | ... | ||
| 28 | }, | ||
| 29 | }); | ||
| 30 | |||
| 31 | No configuration is loaded automatically. | ||
| 32 | Instead a successful call to init_backend_config() is required. | ||
| 33 | |||
| 34 | On restart the reload_config() method is called automatically. | ||
| 35 | |||
| 36 | =head1 AUTHOR | ||
| 37 | |||
| 38 | Mattias P, C<< <mattias.paivarinta@iis.se> >> | ||
| 39 | |||
| 40 | =cut | ||
| 41 | |||
| 42 | use parent 'Daemon::Control'; | ||
| 43 | use Role::Tiny; # Must be loaded before Class::Method::Modifiers or it will warn | ||
| 44 | |||
| 45 | use Carp; | ||
| 46 | use Class::Method::Modifiers; | ||
| 47 | use Hash::Util::FieldHash qw( fieldhash ); | ||
| 48 | use Log::Any qw( $log ); | ||
| 49 | use Zonemaster::Backend::Config; | ||
| 50 | |||
| 51 | before do_restart => \&init_backend_config; | ||
| 52 | |||
| 53 | # Using the inside-out technique to avoid collisions with other instance | ||
| 54 | # variables. | ||
| 55 | fieldhash my %config; | ||
| 56 | fieldhash my %db; | ||
| 57 | fieldhash my %pm; | ||
| 58 | |||
| 59 | =head1 INSTANCE METHODS | ||
| 60 | |||
| 61 | =head2 init_backend_config | ||
| 62 | |||
| 63 | Initializes or reinitializes the L<config>, L<db> and L<pm> properties. | ||
| 64 | |||
| 65 | A candidate for the L<config> property is either accepted as an argument, | ||
| 66 | or L<Zonemaster::Backend::Config::load_config> is invoked to provide one. | ||
| 67 | Candidates for the L<db> and L<pm> properties are constructed according to the | ||
| 68 | L<config> candidate. | ||
| 69 | |||
| 70 | Returns 1 if all candidates are successfully constructed. | ||
| 71 | In this case all properties are assigned their respective candidate values. | ||
| 72 | |||
| 73 | Returns 0 if the construction of any one of the candidates fails. | ||
| 74 | Details about the construction failure are logged. | ||
| 75 | None of the properties are updated. | ||
| 76 | |||
| 77 | =cut | ||
| 78 | |||
| 79 | sub init_backend_config { | ||
| 80 | my ( $self, $config_candidate ) = @_; | ||
| 81 | |||
| 82 | eval { | ||
| 83 | $config_candidate //= Zonemaster::Backend::Config->load_config(); | ||
| 84 | my $db_candidate = $config_candidate->new_DB(); | ||
| 85 | my $pm_candidate = $config_candidate->new_PM(); | ||
| 86 | |||
| 87 | $config{$self} = $config_candidate; | ||
| 88 | $db{$self} = $db_candidate; | ||
| 89 | $pm{$self} = $pm_candidate; | ||
| 90 | }; | ||
| 91 | |||
| 92 | if ( $@ ) { | ||
| 93 | $log->warn( "Failed to load the configuration: $@" ); | ||
| 94 | return 0; | ||
| 95 | } | ||
| 96 | |||
| 97 | return 1; | ||
| 98 | } | ||
| 99 | |||
| 100 | =head1 PROPERTIES | ||
| 101 | |||
| 102 | =head2 config | ||
| 103 | |||
| 104 | Getter for the currently loaded configuration. | ||
| 105 | |||
| 106 | Throws an exception if no successful call to init_backend_config() has been | ||
| 107 | made prior to this call. | ||
| 108 | |||
| 109 | =cut | ||
| 110 | |||
| 111 | sub config { | ||
| 112 | my ( $self ) = @_; | ||
| 113 | |||
| 114 | exists $config{$self} or croak "Not initialized"; | ||
| 115 | |||
| 116 | return $config{$self}; | ||
| 117 | } | ||
| 118 | |||
| 119 | =head2 db | ||
| 120 | |||
| 121 | Getter for a database adapter constructed according to the current | ||
| 122 | configuration. | ||
| 123 | |||
| 124 | Throws an exception if no successful call to init_backend_config() has been | ||
| 125 | made prior to this call. | ||
| 126 | |||
| 127 | =cut | ||
| 128 | |||
| 129 | sub db { | ||
| 130 | my ( $self ) = @_; | ||
| 131 | |||
| 132 | exists $db{$self} or croak "Not initialized"; | ||
| 133 | |||
| 134 | return $db{$self}; | ||
| 135 | } | ||
| 136 | |||
| 137 | =head2 pm | ||
| 138 | |||
| 139 | Getter for a processing manager constructed according to the current | ||
| 140 | configuration. | ||
| 141 | |||
| 142 | Throws an exception if no successful call to init_backend_config() has been | ||
| 143 | made prior to this call. | ||
| 144 | |||
| 145 | =cut | ||
| 146 | |||
| 147 | sub pm { | ||
| 148 | my ( $self ) = @_; | ||
| 149 | |||
| 150 | exists $pm{$self} or croak "Not initialized"; | ||
| 151 | |||
| 152 | return $pm{$self}; | ||
| 153 | } | ||
| 154 | |||
| 155 | 1; |