1 | package LATMOS::Accounts::SyncManager; |
---|
2 | |
---|
3 | use strict; |
---|
4 | use warnings; |
---|
5 | use LATMOS::Accounts::Log; |
---|
6 | use LATMOS::Accounts; |
---|
7 | use Config::IniFiles; |
---|
8 | |
---|
9 | =head1 NAME |
---|
10 | |
---|
11 | LATMOS::Accounts::SyncManager - Routine task manager |
---|
12 | |
---|
13 | =head1 DESCRIPTION |
---|
14 | |
---|
15 | Handle Task process run by L<la-sync-manager> |
---|
16 | |
---|
17 | =head1 FUNCTIONS |
---|
18 | |
---|
19 | =cut |
---|
20 | |
---|
21 | =head2 new($config) |
---|
22 | |
---|
23 | Instaciate object. |
---|
24 | |
---|
25 | C<$config> is a path to an alternative configuration file to default one. |
---|
26 | |
---|
27 | =cut |
---|
28 | |
---|
29 | sub 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(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 | |
---|
57 | Return a reference to the L<Ini::Files> object handling configuration. |
---|
58 | |
---|
59 | =cut |
---|
60 | |
---|
61 | sub ini { $_[0]->{ini} } |
---|
62 | |
---|
63 | =head2 dbrev |
---|
64 | |
---|
65 | Return the current base revision |
---|
66 | |
---|
67 | =cut |
---|
68 | |
---|
69 | sub dbrev { |
---|
70 | my ($self) = @_; |
---|
71 | my $LA = LATMOS::Accounts->new($self->{config}, noacl => 1); |
---|
72 | my $base = $LA->base; |
---|
73 | return $base->current_rev; |
---|
74 | } |
---|
75 | |
---|
76 | sub _load_state_ini { |
---|
77 | my ($self) = @_; |
---|
78 | my $la = LATMOS::Accounts->new($self->{config}, noacl => 1); |
---|
79 | |
---|
80 | my $state_file = $la->val('_default_', 'state_dir', '/'); |
---|
81 | $state_file .= '/la-sync-manager.ini'; |
---|
82 | la_log(LA_DEBUG, "Status file is %s", $state_file); |
---|
83 | if ($state_file && ! -w $state_file) { |
---|
84 | # don't exists, we have to create it |
---|
85 | open(my $handle, '>', $state_file) or do { |
---|
86 | la_log(LA_ERR, "Cannot open build net status file %s", |
---|
87 | $state_file); |
---|
88 | return; |
---|
89 | }; |
---|
90 | print $handle "[_default_]\n"; |
---|
91 | close($handle); |
---|
92 | } |
---|
93 | return Config::IniFiles->new( |
---|
94 | -file => $state_file |
---|
95 | ); |
---|
96 | } |
---|
97 | |
---|
98 | =head2 get_last_rev($module) |
---|
99 | |
---|
100 | Return the last revision the module succeed |
---|
101 | |
---|
102 | =cut |
---|
103 | |
---|
104 | sub get_last_rev { |
---|
105 | my ($self, $module) = @_; |
---|
106 | |
---|
107 | my $ini = $self->_load_state_ini or return(0, 0); |
---|
108 | |
---|
109 | return ($ini->val($module, 'rev', 0), $ini->val($module, 'time', 0)); |
---|
110 | } |
---|
111 | |
---|
112 | =head2 list_modules |
---|
113 | |
---|
114 | List configured task module |
---|
115 | |
---|
116 | =cut |
---|
117 | |
---|
118 | sub list_modules { |
---|
119 | my ($self) = @_; |
---|
120 | $self->ini->Sections; |
---|
121 | } |
---|
122 | |
---|
123 | =head2 process_module($module) |
---|
124 | |
---|
125 | Process C<$module>. |
---|
126 | |
---|
127 | =cut |
---|
128 | |
---|
129 | # TODO what is $dbrev, why is it need here ?? |
---|
130 | |
---|
131 | sub process_module { |
---|
132 | my ($self, $module) = @_; |
---|
133 | |
---|
134 | eval { |
---|
135 | if (!$self->ini->SectionExists($module)) { |
---|
136 | la_log LA_ERR, "Cannot run inexistant module %s", $module; |
---|
137 | return; |
---|
138 | } |
---|
139 | |
---|
140 | my $modtype = $self->ini->val($module, 'type', $module); |
---|
141 | my $task = LATMOS::Accounts::Task->new( |
---|
142 | $modtype, |
---|
143 | config => $self->{config}, |
---|
144 | name => $module, |
---|
145 | syncm => $self, |
---|
146 | ) |
---|
147 | or do { |
---|
148 | la_log(LA_ERR, 'Cannot load module %s', $modtype); |
---|
149 | return; |
---|
150 | }; |
---|
151 | if (!$task->init) { |
---|
152 | la_log(LA_ERR, 'init() failed for module %s', $module); |
---|
153 | return; |
---|
154 | } |
---|
155 | |
---|
156 | my $currentrev = $self->dbrev; |
---|
157 | $task->needToBeRun($self, $module) or return 1; |
---|
158 | |
---|
159 | la_log LA_NOTICE, "Processing sync module %s (%s)", $module, $modtype; |
---|
160 | |
---|
161 | my $res = $task->run; |
---|
162 | if (!$res) { |
---|
163 | my $ini = $self->_load_state_ini or return; |
---|
164 | $ini->newval($module, 'error', LATMOS::Accounts::Log::lastmessage()); |
---|
165 | la_log LA_ERR, "Task %s did not end successfully", $module; |
---|
166 | $ini->RewriteConfig; |
---|
167 | } else { |
---|
168 | $task->post if ($res); |
---|
169 | my $ini = $self->_load_state_ini or return; |
---|
170 | |
---|
171 | $ini->delval($module, 'error'); |
---|
172 | $ini->newval($module, 'rev', $currentrev); |
---|
173 | my $time = time; |
---|
174 | $ini->newval($module, 'time', $time); |
---|
175 | $ini->SetParameterComment( |
---|
176 | $module, 'time', |
---|
177 | scalar(localtime(time)), |
---|
178 | ); |
---|
179 | $ini->RewriteConfig; |
---|
180 | |
---|
181 | la_log LA_DEBUG, "end process $module"; |
---|
182 | } |
---|
183 | return $res; |
---|
184 | }; |
---|
185 | |
---|
186 | if ($@) { |
---|
187 | la_log(LA_CRIT, 'Fatal Perl Error: %s', $@); |
---|
188 | return; |
---|
189 | } else { |
---|
190 | return 1; |
---|
191 | } |
---|
192 | } |
---|
193 | |
---|
194 | =head2 reset_module_savepoint($module) |
---|
195 | |
---|
196 | Reset the savepoint for module C<$module> to force it to run at next |
---|
197 | synchronisation. |
---|
198 | |
---|
199 | =cut |
---|
200 | |
---|
201 | sub reset_module_savepoint { |
---|
202 | my ($self, $module) = @_; |
---|
203 | if (!$self->ini->SectionExists($module)) { |
---|
204 | la_log LA_ERR, "Cannot run inexistant module %s", $module; |
---|
205 | return; |
---|
206 | } |
---|
207 | |
---|
208 | my $modtype = $self->ini->val($module, 'type', $module); |
---|
209 | my $task = LATMOS::Accounts::Task->new( |
---|
210 | $modtype, |
---|
211 | config => $self->{config}, |
---|
212 | name => $module, |
---|
213 | syncm => $self, |
---|
214 | ) |
---|
215 | or do { |
---|
216 | la_log(LA_ERR, 'Cannot load module %s', $modtype); |
---|
217 | return; |
---|
218 | }; |
---|
219 | $task->init or do { |
---|
220 | la_log LA_ERR, "Cannot init module %s", $_; |
---|
221 | return; |
---|
222 | }; |
---|
223 | |
---|
224 | my $ini = $self->_load_state_ini or return; |
---|
225 | |
---|
226 | $ini->delval($module, 'rev'); |
---|
227 | $ini->newval($module, 'time', 0); |
---|
228 | $ini->SetParameterComment( |
---|
229 | $module, 'time', |
---|
230 | 'Reset to 0 the ' . scalar(localtime(time)), |
---|
231 | ); |
---|
232 | |
---|
233 | $task->reset_savepoint; # Specific reset |
---|
234 | $ini->RewriteConfig; |
---|
235 | } |
---|
236 | |
---|
237 | 1; |
---|
238 | |
---|
239 | __END__ |
---|
240 | |
---|
241 | =head1 SEE ALSO |
---|
242 | |
---|
243 | L<LATMOS::Accountsi::Task> |
---|
244 | |
---|
245 | =head1 AUTHOR |
---|
246 | |
---|
247 | Olivier Thauvin, E<lt>olivier.thauvin@latmos.ipsl.frE<gt> |
---|
248 | |
---|
249 | =head1 COPYRIGHT AND LICENSE |
---|
250 | |
---|
251 | Copyright (C) 2012 CNRS SA/CETP/LATMOS |
---|
252 | |
---|
253 | This library is free software; you can redistribute it and/or modify |
---|
254 | it under the same terms as Perl itself, either Perl version 5.10.0 or, |
---|
255 | at your option, any later version of Perl 5 you may have available. |
---|
256 | |
---|
257 | =cut |
---|