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

Last change on this file since 1017 was 1017, checked in by nanardon, 12 years ago
  • fix eval trap
File size: 2.0 KB
Line 
1package LATMOS::Accounts::SyncManager;
2
3use strict;
4use warnings;
5use LATMOS::Accounts::Log;
6use LATMOS::Accounts;
7
8sub new {
9    my ($class, $config) = @_;
10
11    my $ini = Config::IniFiles->new(
12        -file => join('/', ($config || '/etc/latmos-accounts'),
13            'la-sync-manager.ini'),
14    ) or do {
15        la_log LA_ERR, "Cannot load config";
16        return;
17    };
18
19    bless {
20        ini => $ini,
21        config => $config,
22        lastrev => 0,
23    }, $class;
24}
25
26sub ini { $_[0]->{ini} }
27
28sub dbrev { $_[0]->{lastrev} }
29
30sub updrev {
31    my ($self) = @_;
32    my $LA = LATMOS::Accounts->new($self->{config}, noacl => 1);
33    my $base = $LA->default_base;
34    my $newrev = $base->current_rev;
35    $self->{lastrev} = $newrev;
36    return $newrev;
37}
38
39sub list_modules {
40    my ($self) = @_;
41    $self->ini->Sections;
42}
43
44sub process_module {
45    my ($self, $module, $dbrev) = @_;
46
47    eval {
48        if (!$self->ini->SectionExists($module)) {
49            la_log LA_ERR, "Cannot run inexistant module %s", $module;
50            return;
51        }
52
53        my $modtype = $self->ini->val($module, 'type', $module);
54        my $task = LATMOS::Accounts::Task->new(
55            $modtype,
56            config => $self->{config},
57        )
58            or do {
59            la_log(LA_ERR, 'Cannot load module %s', $modtype);
60            return;
61        };
62        la_log LA_NOTICE, "Processing sync module %s (%s)", $module, $modtype;
63        if (!$task->init) {
64            la_log(LA_ERR, 'init() failed for module %s', $module);
65            return;
66        }
67
68        if (!$task->needupd($dbrev, $self)) {
69            la_log LA_DEBUG, "No change on main base, aborting";
70            return 1;
71        }
72
73
74        my $res = $task->run;
75        if (!$res) {
76            la_log LA_ERR, "Task %s did not end successfully", $module;
77        }
78        $task->post if ($res);
79        la_log LA_DEBUG, "end process $module";
80        return $res;
81    };
82
83    if ($@) {
84        la_log(LA_CRIT, 'Fatal Perl Error: %s', $@);
85        return;
86    } else {
87        return 1;
88    }
89}
90
911;
Note: See TracBrowser for help on using the repository browser.