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

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