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

Last change on this file was 2497, checked in by nanardon, 3 years ago

Restore LA() to 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->state_dir . '/la-sync-manager.ini';
93    la_log(LA_DEBUG, "Status file is %s", $state_file);
94    if ($state_file && ! -w $state_file) {
95        # don't exists, we have to create it
96        open(my $handle, '>', $state_file) or do {
97            la_log(LA_ERR, "Cannot open build net status file %s",
98                $state_file);
99            return;
100        };
101        print $handle "[_default_]\n";
102        close($handle);
103    }
104    return Config::IniFiles->new(
105        -file => $state_file
106    );
107}
108
109=head2 get_last_rev($module)
110
111Return the last revision the module succeed
112
113=cut
114
115sub get_last_rev {
116    my ($self, $module) = @_;
117
118    my $ini = $self->_load_state_ini or return(0, 0);
119
120    return ($ini->val($module, 'rev', 0), $ini->val($module, 'time', 0));
121}
122
123=head2 list_modules
124
125List configured task module
126
127=cut
128
129sub list_modules {
130    my ($self) = @_;
131    $self->ini->Sections;
132}
133
134
135=head2 listSortedModules
136
137List configured module ordered for running
138
139=cut
140
141sub listSortedModules {
142    my ($self) = @_;
143
144    my %modules = ();
145
146    foreach my $module ($self->list_modules) {
147
148        $self->ini->val($module, 'disable') and do {
149            la_log(LA_DEBUG, "Module $module is disable, not running it");
150            next;
151        };
152
153        $modules{$module} = 1; # default
154        my $modtype = $self->ini->val($module, 'type', $module);
155        my $task = LATMOS::Accounts::Task->new(
156            $modtype,
157            config => $self->{config},
158            name   => $module,
159            syncm  => $self,
160        )
161            or do {
162            la_log(LA_ERR, 'Cannot load module %s', $modtype);
163            next;
164        };
165
166        $modules{$module} = $task->order;
167    }
168
169    return sort { $modules{$a} <=> $modules{$b} } keys %modules;
170}
171
172=head2 process_module($module)
173
174Process C<$module>.
175
176=cut
177
178sub process_module {
179    my ($self, $module) = @_;
180
181    eval {
182        if (!$self->ini->SectionExists($module)) {
183            la_log LA_ERR, "Cannot run inexistant module %s", $module;
184            return;
185        }
186
187        my $modtype = $self->ini->val($module, 'type', $module);
188        my $task = LATMOS::Accounts::Task->new(
189            $modtype,
190            config => $self->{config},
191            name   => $module,
192            syncm  => $self,
193        )
194            or do {
195            la_log(LA_ERR, 'Cannot load module %s', $modtype);
196            return;
197        };
198        if (!$task->init) {
199            la_log(LA_ERR, 'init() failed for module %s', $module);
200            return;
201        }
202
203        my $currentrev = $self->dbrev;
204        $task->needToBeRun($self, $module) or return 1;
205
206        la_log LA_NOTICE, "Processing sync module %s (%s)", $module, $modtype;
207
208        my $res = $task->run;
209        if (!$res) {
210            my $ini = $self->_load_state_ini or return;
211            $ini->newval($module, 'error', LATMOS::Accounts::Log::lastmessage());
212            la_log LA_ERR, "Task %s did not end successfully", $module;
213            $ini->RewriteConfig;
214        } else {
215            $task->post if ($res);
216            my $ini = $self->_load_state_ini or return;
217
218            $ini->delval($module, 'error');
219            $ini->newval($module, 'rev', $currentrev);
220            my $time = time;
221            $ini->newval($module, 'time', $time);
222            $ini->SetParameterComment(
223                $module, 'time',
224                scalar(localtime(time)),
225            );
226            $ini->RewriteConfig;
227
228            la_log LA_DEBUG, "end process $module";
229        }
230        return $res;
231    };
232
233    if ($@) {
234        la_log(LA_CRIT, 'Fatal Perl Error: %s', $@);
235        return;
236    } else {
237        return 1;
238    }
239}
240
241=head2 reset_module_savepoint($module)
242
243Reset the savepoint for module C<$module> to force it to run at next
244synchronisation.
245
246=cut
247
248sub reset_module_savepoint {
249    my ($self, $module) = @_;
250    if (!$self->ini->SectionExists($module)) {
251        la_log LA_ERR, "Cannot run inexistant module %s", $module;
252        return;
253    }
254
255    my $modtype = $self->ini->val($module, 'type', $module);
256    my $task = LATMOS::Accounts::Task->new(
257        $modtype,
258        config => $self->{config},
259        name   => $module,
260        syncm  => $self,
261    )
262        or do {
263        la_log(LA_ERR, 'Cannot load module %s', $modtype);
264        return;
265    };
266    $task->init or do {
267        la_log LA_ERR, "Cannot init module %s", $_;
268        return;
269    };
270
271    my $ini = $self->_load_state_ini or return;
272
273    $ini->delval($module, 'rev');
274    $ini->newval($module, 'time', 0);
275    $ini->SetParameterComment(
276        $module, 'time',
277        'Reset to 0 the ' . scalar(localtime(time)),
278    );
279
280    $task->reset_savepoint; # Specific reset
281    $ini->RewriteConfig;
282}
283
2841;
285
286__END__
287
288=head1 SEE ALSO
289
290L<LATMOS::Accountsi::Task>
291
292=head1 AUTHOR
293
294Olivier Thauvin, E<lt>olivier.thauvin@latmos.ipsl.frE<gt>
295
296=head1 COPYRIGHT AND LICENSE
297
298Copyright (C) 2012 CNRS SA/CETP/LATMOS
299
300This library is free software; you can redistribute it and/or modify
301it under the same terms as Perl itself, either Perl version 5.10.0 or,
302at your option, any later version of Perl 5 you may have available.
303
304=cut
Note: See TracBrowser for help on using the repository browser.