source: trunk/LATMOS-Accounts/lib/LATMOS/Accounts/SyncManager.pm @ 2448

Last change on this file since 2448 was 2439, checked in by nanardon, 4 years ago

Flag in log when change are made by SyncManager?

File size: 6.6 KB
Line 
1package LATMOS::Accounts::SyncManager;
2
3use strict;
4use warnings;
5use LATMOS::Accounts::Log;
6use LATMOS::Accounts;
7use Config::IniFiles;
8
9=head1 NAME
10
11LATMOS::Accounts::SyncManager - Routine task manager
12
13=head1 DESCRIPTION
14
15Handle Task process run by L<la-sync-manager>
16
17=head1 FUNCTIONS
18
19=cut
20
21=head2 new($config)
22
23Instaciate object.
24
25C<$config> is a path to an alternative configuration file to default one.
26
27=cut
28
29sub new {
30    my ($class, $config) = @_;
31
32    my $ini = Config::IniFiles->new(
33        -file => join('/', ($config || '/etc/latmos-accounts'),
34            'la-sync-manager.ini'),
35    ) or do {
36        la_log LA_ERR, "Cannot load config";
37        return;
38    };
39
40    foreach my $section (qw(update basessynchro refreshexpired updatedyndata)) {
41        if ( !grep {
42            lc($ini->val($_, 'type', '')) eq $section
43            } $ini->Sections ) {
44            $ini->newval("_$section", 'type', $section);
45        }
46    }
47
48    bless {
49        ini => $ini,
50        config => $config,
51        lastrev => 0,
52    }, $class;
53}
54
55=head2 ini
56
57Return a reference to the L<Ini::Files> object handling configuration.
58
59=cut
60
61sub ini { $_[0]->{ini} }
62
63=head2 LA
64
65Return a LATMOS::Accounts setup for SynchM
66
67=cut
68
69sub LA {
70    my ( $self ) = @_;
71
72    return LATMOS::Accounts->new($self->{config}, noacl => 1, loguser => '@SyncManager');
73}
74
75=head2 dbrev
76
77Return the current base revision
78
79=cut
80
81sub dbrev {
82    my ($self) = @_;
83    my $LA = $self->LA;
84    my $base = $LA->base;
85    return $base->current_rev;
86}
87
88sub _load_state_ini {
89    my ($self) = @_;
90    my $la = $self->LA;
91
92    my $state_file =  $la->val('_default_', 'state_dir', '/');
93    $state_file .= '/la-sync-manager.ini';
94    la_log(LA_DEBUG, "Status file is %s", $state_file);
95    if ($state_file && ! -w $state_file) {
96        # don't exists, we have to create it
97        open(my $handle, '>', $state_file) or do {
98            la_log(LA_ERR, "Cannot open build net status file %s",
99                $state_file);
100            return;
101        };
102        print $handle "[_default_]\n";
103        close($handle);
104    }
105    return Config::IniFiles->new(
106        -file => $state_file
107    );
108}
109
110=head2 get_last_rev($module)
111
112Return the last revision the module succeed
113
114=cut
115
116sub get_last_rev {
117    my ($self, $module) = @_;
118
119    my $ini = $self->_load_state_ini or return(0, 0);
120
121    return ($ini->val($module, 'rev', 0), $ini->val($module, 'time', 0));
122}
123
124=head2 list_modules
125
126List configured task module
127
128=cut
129
130sub list_modules {
131    my ($self) = @_;
132    $self->ini->Sections;
133}
134
135
136=head2 listSortedModules
137
138List configured module ordered for running
139
140=cut
141
142sub listSortedModules {
143    my ($self) = @_;
144
145    my %modules = ();
146
147    foreach my $module ($self->list_modules) {
148
149        $self->ini->val($module, 'disable') and do {
150            la_log(LA_DEBUG, "Module $module is disable, not running it");
151            next;
152        };
153
154        $modules{$module} = 1; # default
155        my $modtype = $self->ini->val($module, 'type', $module);
156        my $task = LATMOS::Accounts::Task->new(
157            $modtype,
158            config => $self->{config},
159            name   => $module,
160            syncm  => $self,
161        )
162            or do {
163            la_log(LA_ERR, 'Cannot load module %s', $modtype);
164            next;
165        };
166
167        $modules{$module} = $task->order;
168    }
169
170    return sort { $modules{$a} <=> $modules{$b} } keys %modules;
171}
172
173=head2 process_module($module)
174
175Process C<$module>.
176
177=cut
178
179sub process_module {
180    my ($self, $module) = @_;
181
182    eval {
183        if (!$self->ini->SectionExists($module)) {
184            la_log LA_ERR, "Cannot run inexistant module %s", $module;
185            return;
186        }
187
188        my $modtype = $self->ini->val($module, 'type', $module);
189        my $task = LATMOS::Accounts::Task->new(
190            $modtype,
191            config => $self->{config},
192            name   => $module,
193            syncm  => $self,
194        )
195            or do {
196            la_log(LA_ERR, 'Cannot load module %s', $modtype);
197            return;
198        };
199        if (!$task->init) {
200            la_log(LA_ERR, 'init() failed for module %s', $module);
201            return;
202        }
203
204        my $currentrev = $self->dbrev;
205        $task->needToBeRun($self, $module) or return 1;
206
207        la_log LA_NOTICE, "Processing sync module %s (%s)", $module, $modtype;
208
209        my $res = $task->run;
210        if (!$res) {
211            my $ini = $self->_load_state_ini or return;
212            $ini->newval($module, 'error', LATMOS::Accounts::Log::lastmessage());
213            la_log LA_ERR, "Task %s did not end successfully", $module;
214            $ini->RewriteConfig;
215        } else {
216            $task->post if ($res);
217            my $ini = $self->_load_state_ini or return;
218
219            $ini->delval($module, 'error');
220            $ini->newval($module, 'rev', $currentrev);
221            my $time = time;
222            $ini->newval($module, 'time', $time);
223            $ini->SetParameterComment(
224                $module, 'time',
225                scalar(localtime(time)),
226            );
227            $ini->RewriteConfig;
228
229            la_log LA_DEBUG, "end process $module";
230        }
231        return $res;
232    };
233
234    if ($@) {
235        la_log(LA_CRIT, 'Fatal Perl Error: %s', $@);
236        return;
237    } else {
238        return 1;
239    }
240}
241
242=head2 reset_module_savepoint($module)
243
244Reset the savepoint for module C<$module> to force it to run at next
245synchronisation.
246
247=cut
248
249sub reset_module_savepoint {
250    my ($self, $module) = @_;
251    if (!$self->ini->SectionExists($module)) {
252        la_log LA_ERR, "Cannot run inexistant module %s", $module;
253        return;
254    }
255
256    my $modtype = $self->ini->val($module, 'type', $module);
257    my $task = LATMOS::Accounts::Task->new(
258        $modtype,
259        config => $self->{config},
260        name   => $module,
261        syncm  => $self,
262    )
263        or do {
264        la_log(LA_ERR, 'Cannot load module %s', $modtype);
265        return;
266    };
267    $task->init or do {
268        la_log LA_ERR, "Cannot init module %s", $_;
269        return;
270    };
271
272    my $ini = $self->_load_state_ini or return;
273
274    $ini->delval($module, 'rev');
275    $ini->newval($module, 'time', 0);
276    $ini->SetParameterComment(
277        $module, 'time',
278        'Reset to 0 the ' . scalar(localtime(time)),
279    );
280
281    $task->reset_savepoint; # Specific reset
282    $ini->RewriteConfig;
283}
284
2851;
286
287__END__
288
289=head1 SEE ALSO
290
291L<LATMOS::Accountsi::Task>
292
293=head1 AUTHOR
294
295Olivier Thauvin, E<lt>olivier.thauvin@latmos.ipsl.frE<gt>
296
297=head1 COPYRIGHT AND LICENSE
298
299Copyright (C) 2012 CNRS SA/CETP/LATMOS
300
301This library is free software; you can redistribute it and/or modify
302it under the same terms as Perl itself, either Perl version 5.10.0 or,
303at your option, any later version of Perl 5 you may have available.
304
305=cut
Note: See TracBrowser for help on using the repository browser.