1 | package LATMOS::Accounts::SyncManager; |
---|
2 | |
---|
3 | use strict; |
---|
4 | use warnings; |
---|
5 | use LATMOS::Accounts::Log; |
---|
6 | use LATMOS::Accounts; |
---|
7 | |
---|
8 | =head1 NAME |
---|
9 | |
---|
10 | LATMOS::Accounts::SyncManager - Routine task manager |
---|
11 | |
---|
12 | =head1 DESCRIPTION |
---|
13 | |
---|
14 | Handle Task process run by L<la-sync-manager> |
---|
15 | |
---|
16 | =head1 FUNCTIONS |
---|
17 | |
---|
18 | =cut |
---|
19 | |
---|
20 | =head2 new($config) |
---|
21 | |
---|
22 | Instaciate object. |
---|
23 | |
---|
24 | C<$config> is a path to an alternative configuration file to default one. |
---|
25 | |
---|
26 | =cut |
---|
27 | |
---|
28 | sub new { |
---|
29 | my ($class, $config) = @_; |
---|
30 | |
---|
31 | my $ini = Config::IniFiles->new( |
---|
32 | -file => join('/', ($config || '/etc/latmos-accounts'), |
---|
33 | 'la-sync-manager.ini'), |
---|
34 | ) or do { |
---|
35 | la_log LA_ERR, "Cannot load config"; |
---|
36 | return; |
---|
37 | }; |
---|
38 | |
---|
39 | if ( !grep { |
---|
40 | lc($ini->val($_, 'type', '')) eq 'basessynchro' |
---|
41 | } $ini->Sections ) { |
---|
42 | $ini->newval('_basesync', 'type', 'basessynchro'); |
---|
43 | } |
---|
44 | if ( !grep { |
---|
45 | lc($ini->val($_, 'type', '')) eq 'refreshexpired' |
---|
46 | } $ini->Sections ) { |
---|
47 | $ini->newval('_refreshexpired', 'type', 'refreshexpired'); |
---|
48 | } |
---|
49 | |
---|
50 | $ini->newval('_automatedrequest', 'type', 'automatedrequest'); |
---|
51 | |
---|
52 | bless { |
---|
53 | ini => $ini, |
---|
54 | config => $config, |
---|
55 | lastrev => 0, |
---|
56 | }, $class; |
---|
57 | } |
---|
58 | |
---|
59 | =head2 ini |
---|
60 | |
---|
61 | Return a reference to the L<Ini::Files> object handling configuration. |
---|
62 | |
---|
63 | =cut |
---|
64 | |
---|
65 | sub ini { $_[0]->{ini} } |
---|
66 | |
---|
67 | =head2 dbrev |
---|
68 | |
---|
69 | Return the current base revision |
---|
70 | |
---|
71 | =cut |
---|
72 | |
---|
73 | sub dbrev { $_[0]->{lastrev} } |
---|
74 | |
---|
75 | =head2 updrev |
---|
76 | |
---|
77 | Update status file with with current base revision as restart point |
---|
78 | |
---|
79 | =cut |
---|
80 | |
---|
81 | sub updrev { |
---|
82 | my ($self) = @_; |
---|
83 | my $LA = LATMOS::Accounts->new($self->{config}, noacl => 1); |
---|
84 | my $base = $LA->base; |
---|
85 | my $newrev = $base->current_rev; |
---|
86 | $self->{lastrev} = $newrev; |
---|
87 | return $newrev; |
---|
88 | } |
---|
89 | |
---|
90 | =head2 list_modules |
---|
91 | |
---|
92 | List configured task module |
---|
93 | |
---|
94 | =cut |
---|
95 | |
---|
96 | sub list_modules { |
---|
97 | my ($self) = @_; |
---|
98 | $self->ini->Sections; |
---|
99 | } |
---|
100 | |
---|
101 | =head2 process_module($module, $dbrev) |
---|
102 | |
---|
103 | Process C<$module>. |
---|
104 | |
---|
105 | =cut |
---|
106 | |
---|
107 | # TODO what is $dbrev, why is it need here ?? |
---|
108 | |
---|
109 | sub process_module { |
---|
110 | my ($self, $module, $dbrev) = @_; |
---|
111 | |
---|
112 | eval { |
---|
113 | if (!$self->ini->SectionExists($module)) { |
---|
114 | la_log LA_ERR, "Cannot run inexistant module %s", $module; |
---|
115 | return; |
---|
116 | } |
---|
117 | |
---|
118 | my $modtype = $self->ini->val($module, 'type', $module); |
---|
119 | my $task = LATMOS::Accounts::Task->new( |
---|
120 | $modtype, |
---|
121 | config => $self->{config}, |
---|
122 | ) |
---|
123 | or do { |
---|
124 | la_log(LA_ERR, 'Cannot load module %s', $modtype); |
---|
125 | return; |
---|
126 | }; |
---|
127 | la_log LA_NOTICE, "Processing sync module %s (%s)", $module, $modtype; |
---|
128 | if (!$task->init) { |
---|
129 | la_log(LA_ERR, 'init() failed for module %s', $module); |
---|
130 | return; |
---|
131 | } |
---|
132 | |
---|
133 | if (!$task->needupd($dbrev, $self)) { |
---|
134 | la_log LA_DEBUG, "No change on main base, aborting"; |
---|
135 | return 1; |
---|
136 | } |
---|
137 | |
---|
138 | |
---|
139 | my $res = $task->run; |
---|
140 | if (!$res) { |
---|
141 | la_log LA_ERR, "Task %s did not end successfully", $module; |
---|
142 | } |
---|
143 | $task->post if ($res); |
---|
144 | la_log LA_DEBUG, "end process $module"; |
---|
145 | return $res; |
---|
146 | }; |
---|
147 | |
---|
148 | if ($@) { |
---|
149 | la_log(LA_CRIT, 'Fatal Perl Error: %s', $@); |
---|
150 | return; |
---|
151 | } else { |
---|
152 | return 1; |
---|
153 | } |
---|
154 | } |
---|
155 | |
---|
156 | 1; |
---|
157 | |
---|
158 | __END__ |
---|
159 | |
---|
160 | =head1 SEE ALSO |
---|
161 | |
---|
162 | L<LATMOS::Accountsi::Task> |
---|
163 | |
---|
164 | =head1 AUTHOR |
---|
165 | |
---|
166 | Olivier Thauvin, E<lt>olivier.thauvin@latmos.ipsl.frE<gt> |
---|
167 | |
---|
168 | =head1 COPYRIGHT AND LICENSE |
---|
169 | |
---|
170 | Copyright (C) 2012 CNRS SA/CETP/LATMOS |
---|
171 | |
---|
172 | This library is free software; you can redistribute it and/or modify |
---|
173 | it under the same terms as Perl itself, either Perl version 5.10.0 or, |
---|
174 | at your option, any later version of Perl 5 you may have available. |
---|
175 | |
---|
176 | =cut |
---|