source: trunk/LATMOS-Accounts/bin/la-sync-manager @ 1634

Last change on this file since 1634 was 1200, checked in by nanardon, 11 years ago

store module freshness state in inifile

File size: 3.5 KB
Line 
1#!/usr/bin/perl
2
3use strict;
4use warnings;
5use Getopt::Long;
6use Pod::Usage;
7use Config::IniFiles;
8use LATMOS::Accounts;
9use LATMOS::Accounts::SyncManager;
10use LATMOS::Accounts::Log;
11use LATMOS::Accounts::Task;
12
13=head1 NAME
14
15    la-sync-manager - Daemon performing synchronisation and various tasks
16
17=head1 SYNOPSIS
18
19    la-sync-manager [options...]
20
21=cut
22
23GetOptions(
24    'nodaemon' => \my $nodaemon,
25    'c|config=s'   => \my $config,
26    'help'         => sub { pod2usage(0) },
27    'wait=i'       => \my $wait,
28) or pod2usage();
29
30=head1 OPTIONS
31
32=over 4
33
34=item --nodaemon
35
36Don't go into background
37
38=item -c|--config configdir
39
40Use this configuration instead default one
41
42=item --help
43
44Display this help
45
46=item --wait minutes
47
48Wait this number of minutes before process synchronisation (default is 5
49minutes)
50
51=back
52
53=cut
54
55my $needsync = 2;
56my $pidfile = undef;
57$wait ||= 5; # default in minutes
58
59my $syncm = LATMOS::Accounts::SyncManager->new($config) or do {
60    la_log LA_ERR, "Cannot instanciate Sync Manager";
61    exit(1);
62};
63
64la_set_log(
65    syslog => [],
66    console => ($nodaemon ? LA_NOTICE : undef),
67);
68
69# Trap perl message, send it to log
70$SIG{__DIE__} = sub {
71    la_log LA_ERR, "Die: %s", $_[0] unless($_[0] =~ /^Can't locate/);
72    die $_[0];
73};
74$SIG{__WARN__} = sub {
75    la_log LA_WARN, "Warn: %s", $_[0];
76    warn $_[0];
77};
78
79{
80    my $LA = LATMOS::Accounts->new($config, noacl => 1);
81    if ($LA->val('_default_', 'state_dir')) {
82        $pidfile = $LA->val('_default_', 'state_dir') . '/sync-manager.pid';
83    }
84    if (my $mail = $LA->val('_network_', 'maillog')) {
85            la_set_log(mail => $mail);
86    }
87}
88
89# Daemonize
90if (!$nodaemon) {
91    my $pid = fork;
92    if ($pid) {
93        exit 0;
94    }
95}
96
97if ($pidfile) {
98    # maybe we want to lock here !!
99    open(my $fh, '>', $pidfile) or die "Cannot open pidfile $pidfile\n";
100    print $fh "$$\n";
101    close($fh);
102}
103
104$SIG{INT} = sub {
105    unlink($pidfile) if ($pidfile);
106    exit 0;
107};
108
109$SIG{'HUP'} = sub {
110    $needsync = 1;
111    la_log LA_NOTICE, "SigHup received, synchronise now";
112};
113
114sub process {
115    my ($module) = @_;
116    my $pid = fork;
117    if ($pid == 0) {
118        $SIG{INT} = 'DEFAULT';
119        my $res = $syncm->process_module($module);
120        exit($res ? 0 : 1);
121    }
122    my $retpid;
123    while(($retpid = waitpid(-1, 0)) <= 0) {}
124    local $SIG{HUP} = 'IGNORE';
125    if ($retpid) {
126        my $res = $? >> 8;
127        if ($res) {
128            la_log LA_ERR, "Sync process exit with $res";
129            return;
130        }
131    }
132    return 1;
133}
134
135my $lasttout;
136my %revs;
137while (1) {
138    if ($needsync) {
139        my $levelsync = $needsync;
140        $needsync = 0;
141        {
142            local $SIG{HUP} = 'IGNORE';
143            # listing module to run in this loop
144            $lasttout = scalar(time) if ($levelsync == 2);
145        }
146        foreach my $module ($syncm->list_modules()) {
147            next if ($syncm->ini->val($module, 'lazy') && $levelsync < 2);
148            my $res = process($module);
149        }
150    }
151
152    # waiting, to perform next sync
153    sleep(15) unless($needsync);
154    # if it is not time yet:
155    next if($lasttout + ($wait * 60) > scalar(time));
156    $needsync = 2;
157}
158
159__END__
160
161=head1 SEE ALSO
162
163L<la-sync-manager.ini>, L<latmos-accounts.ini>
164
165=head1 AUTHOR
166
167Olivier Thauvin, E<lt>olivier.thauvin@latmos.ipsl.frE<gt>
168
169=head1 COPYRIGHT AND LICENSE
170
171Copyright (C) 2008, 2009, 2010, 2011, 2012 CNRS SA/CETP/LATMOS
172
173This library is free software; you can redistribute it and/or modify
174it under the same terms as Perl itself, either Perl version 5.10.0 or,
175at your option, any later version of Perl 5 you may have available.
176
177=cut
Note: See TracBrowser for help on using the repository browser.