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

Last change on this file since 1135 was 1135, checked in by nanardon, 12 years ago

Merge branch 'dyngroup'

File size: 3.5 KB
Line 
1package LATMOS::Accounts::SyncManager;
2
3use strict;
4use warnings;
5use LATMOS::Accounts::Log;
6use LATMOS::Accounts;
7
8=head1 NAME
9
10LATMOS::Accounts::SyncManager - Routine task manager
11
12=head1 DESCRIPTION
13
14Handle Task process run by L<la-sync-manager>
15
16=head1 FUNCTIONS
17
18=cut
19
20=head2 new($config)
21
22Instaciate object.
23
24C<$config> is a path to an alternative configuration file to default one.
25
26=cut
27
28sub 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
61Return a reference to the L<Ini::Files> object handling configuration.
62
63=cut
64
65sub ini { $_[0]->{ini} }
66
67=head2 dbrev
68
69Return the current base revision
70
71=cut
72
73sub dbrev { $_[0]->{lastrev} }
74
75=head2 updrev
76
77Update status file with with current base revision as restart point
78
79=cut
80
81sub 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
92List configured task module
93
94=cut
95
96sub list_modules {
97    my ($self) = @_;
98    $self->ini->Sections;
99}
100
101=head2 process_module($module, $dbrev)
102
103Process C<$module>.
104
105=cut
106
107# TODO what is $dbrev, why is it need here ??
108
109sub 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
1561;
157
158__END__
159
160=head1 SEE ALSO
161
162L<LATMOS::Accountsi::Task>
163
164=head1 AUTHOR
165
166Olivier Thauvin, E<lt>olivier.thauvin@latmos.ipsl.frE<gt>
167
168=head1 COPYRIGHT AND LICENSE
169
170Copyright (C) 2012 CNRS SA/CETP/LATMOS
171
172This library is free software; you can redistribute it and/or modify
173it under the same terms as Perl itself, either Perl version 5.10.0 or,
174at your option, any later version of Perl 5 you may have available.
175
176=cut
Note: See TracBrowser for help on using the repository browser.