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