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

Last change on this file since 1023 was 1023, checked in by nanardon, 12 years ago
  • complete POD

This patch a basic documentation to all functions.
It also add two test to ensure all POD syntax are correct and coverage is full.

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
70{
71    my $LA = LATMOS::Accounts->new($config, noacl => 1);
72    if ($LA->val('_default_', 'state_dir')) {
73        $pidfile = $LA->val('_default_', 'state_dir') . '/sync-manager.pid';
74    }
75    if (my $mail = $LA->val('_network_', 'maillog')) {
76            la_set_log(mail => $mail);
77    }
78}
79
80# Daemonize
81if (!$nodaemon) {
82    my $pid = fork;
83    if ($pid) {
84        exit 0;
85    }
86}
87
88if ($pidfile) {
89    # maybe we want to lock here !!
90    open(my $fh, '>', $pidfile) or die "Cannot open pidfile $pidfile\n";
91    print $fh "$$\n";
92    close($fh);
93}
94
95$SIG{INT} = sub {
96    unlink($pidfile) if ($pidfile);
97    exit 0;
98};
99
100$SIG{'HUP'} = sub {
101    $needsync = 1;
102    la_log LA_NOTICE, "SigHup received, synchronise now";
103};
104
105sub process {
106    my ($module, $baserev) = @_;
107    my $pid = fork;
108    if ($pid == 0) {
109        $SIG{INT} = 'DEFAULT';
110        my $res = $syncm->process_module($module, $baserev);
111        exit($res ? 0 : 1);
112    }
113    my $retpid;
114    while(($retpid = waitpid(-1, 0)) <= 0) {}
115    local $SIG{HUP} = 'IGNORE';
116    if ($retpid) {
117        my $res = $? >> 8;
118        if ($res) {
119            la_log LA_ERR, "Sync process exit with $res";
120            return;
121        }
122    }
123    return 1;
124}
125
126my $lasttout;
127my %revs;
128while (1) {
129    if ($needsync) {
130        my $levelsync = $needsync;
131        $needsync = 0;
132        {
133            local $SIG{HUP} = 'IGNORE';
134            $syncm->updrev;
135            # listing module to run in this loop
136            $lasttout = scalar(time) if ($levelsync == 2);
137        }
138        foreach my $module ($syncm->list_modules()) {
139            next if ($syncm->ini->val($module, 'lazy') && $levelsync < 2);
140            my $res = process($module, $revs{$module} || 0);
141            if ($res) {
142                $revs{$module} = $syncm->dbrev;
143            }
144        }
145    }
146
147    # waiting, to perform next sync
148    sleep(15) unless($needsync);
149    # if it is not time yet:
150    next if($lasttout + ($wait * 60) > scalar(time));
151    $needsync = 2;
152}
153
154__END__
155
156=head1 SEE ALSO
157
158L<la-sync-manager.ini>, L<latmos-accounts.ini>
159
160=head1 AUTHOR
161
162Olivier Thauvin, E<lt>olivier.thauvin@latmos.ipsl.frE<gt>
163
164=head1 COPYRIGHT AND LICENSE
165
166Copyright (C) 2008, 2009, 2010, 2011, 2012 CNRS SA/CETP/LATMOS
167
168This library is free software; you can redistribute it and/or modify
169it under the same terms as Perl itself, either Perl version 5.10.0 or,
170at your option, any later version of Perl 5 you may have available.
171
172=cut
Note: See TracBrowser for help on using the repository browser.