source: LATMOS-Accounts/bin/la-sync-manager @ 983

Last change on this file since 983 was 983, checked in by nanardon, 12 years ago
  • add task module to validate automated request
File size: 2.6 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
13GetOptions(
14    'nodaemon' => \my $nodaemon,
15    'c|config=s'   => \my $config,
16    'help'         => sub { pod2usage(0) },
17    'test'         => \my $test,
18    'wait=i'       => \my $wait,
19) or pod2usage();
20
21my $needsync = 2;
22my $pidfile = undef;
23$wait ||= 5; # default in minutes
24
25my $syncm = LATMOS::Accounts::SyncManager->new($config) or do {
26    la_log LA_ERR, "Cannot instanciate Sync Manager";
27    exit(1);
28};
29
30la_set_log(
31    syslog => [],
32    console => ($nodaemon ? LA_NOTICE : undef),
33);
34
35
36{
37    my $LA = LATMOS::Accounts->new($config, noacl => 1);
38    if ($LA->val('_default_', 'state_dir')) {
39        $pidfile = $LA->val('_default_', 'state_dir') . '/sync-manager.pid';
40    }
41    if (my $mail = $LA->val('_network_', 'maillog')) {
42            la_set_log(mail => $mail);
43    }
44}
45
46# Daemonize
47if (!$nodaemon) {
48    my $pid = fork;
49    if ($pid) {
50        exit 0;
51    }
52}
53
54if ($pidfile) {
55    # maybe we want to lock here !!
56    open(my $fh, '>', $pidfile) or die "Cannot open pidfile $pidfile\n";
57    print $fh "$$\n";
58    close($fh);
59}
60
61$SIG{INT} = sub {
62    unlink($pidfile) if ($pidfile);
63    exit 0;
64};
65
66$SIG{'HUP'} = sub {
67    $needsync = 1;
68    la_log LA_NOTICE, "SigHup received, synchronise now";
69};
70
71sub process {
72    my ($module, $baserev) = @_;
73    my $pid = fork;
74    if ($pid == 0) {
75        $SIG{INT} = 'DEFAULT';
76        my $res = $syncm->process_module($module, $baserev);
77        exit($res ? 0 : 1);
78    }
79    my $retpid;
80    while(($retpid = waitpid(-1, 0)) <= 0) {}
81    local $SIG{HUP} = 'IGNORE';
82    if ($retpid) {
83        my $res = $? >> 8;
84        if ($res) {
85            la_log LA_ERR, "Sync process exit with $res";
86            return;
87        }
88    }
89    return 1;
90}
91
92my $lasttout;
93my %revs;
94while (1) {
95    if ($needsync) {
96        my $levelsync = $needsync;
97        $needsync = 0;
98        {
99            local $SIG{HUP} = 'IGNORE';
100            $syncm->updrev;
101            # listing module to run in this loop
102            $lasttout = scalar(time) if ($levelsync == 2);
103        }
104        foreach my $module ($syncm->list_modules()) {
105            next if ($syncm->ini->val($module, 'lazy') && $levelsync < 2);
106            my $res = process($module, $revs{$module} || 0);
107            if ($res) {
108                $revs{$module} = $syncm->dbrev;
109            }
110        }
111    }
112
113    # waiting, to perform next sync
114    sleep(15) unless($needsync);
115    # if it is not time yet:
116    next if($lasttout + ($wait * 60) > scalar(time));
117    $needsync = 2;
118}
119
Note: See TracBrowser for help on using the repository browser.