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

Last change on this file since 656 was 610, checked in by nanardon, 14 years ago
  • use shebang not altering process name
File size: 2.1 KB
Line 
1#!/usr/bin/perl
2
3use strict;
4use warnings;
5use Getopt::Long;
6use Pod::Usage;
7use LATMOS::Accounts;
8use LATMOS::Accounts::Log;
9
10GetOptions(
11    'nodaemon' => \my $nodaemon,
12    'c|config=s'   => \my $config,
13    'help'         => sub { pod2usage(0) },
14    'test'         => \my $test,
15    's|syncname=s' => \my $syncname,
16) or pod2usage();
17
18my $needsync = 0;
19my $pidfile = undef;
20
21$SIG{'HUP'} = sub {
22    $needsync = 1;
23};
24
25la_set_log(
26    syslog => [],
27    console => ($nodaemon ? LA_NOTICE : undef),
28);
29
30{
31    my $LA = LATMOS::Accounts->new($config, noacl => 1);
32    if ($LA->val('_default_', 'state_dir')) {
33        $pidfile = $LA->val('_default_', 'state_dir') . '/sync-manager.pid';
34    }
35}
36
37# Daemonize
38if (!$nodaemon) {
39    my $pid = fork;
40    if ($pid) {
41        exit 0;
42    }
43}
44
45$SIG{INT} = sub {
46    unlink($pidfile) if ($pidfile);
47    exit 0;
48};
49
50if ($pidfile) {
51    # maybe we want to lock here !!
52    open(my $fh, '>', $pidfile) or die "Cannot open pidfile $pidfile\n";
53    print $fh "$$\n";
54    close($fh);
55}
56
57sub sync {
58
59    la_log LA_NOTICE, "Starting synchronisation";
60    my $LA = LATMOS::Accounts->new($config, noacl => 1) or do {
61        la_log LA_ERR, "Cannot instantiate LA";
62        return 1;
63    };
64
65    my $sync = $LA->create_synchro(
66        $syncname || $LA->default_synchro_name,
67        test     => $test,
68    ) or do {
69       la_log LA_ERR, "cannot create sync object";
70       return 2;
71    };
72
73    $sync->load_dest and do {
74        la_log LA_ERR, "Cannot load destination";
75        return 3;
76    };
77
78    $sync->process() or do {
79        la_log LA_ERR, "Sync has failed\n";
80        return 4;
81    };
82    la_log LA_NOTICE, "Ending synchronisation";
83
84    return 0;
85}
86
87while (1) {
88    if ($needsync) {
89        $needsync = 0;
90        my $pid = fork;
91        if ($pid == 0) {
92            $SIG{INT} = undef;
93            exit sync();
94        } else {
95            while(waitpid($pid, 0) <= 0) {}
96            my $res = $? << 8;
97            if ($res) {
98               la_log LA_ERR, "Sync process exit with $res";
99           } 
100        }
101        next;
102    }
103
104    # waiting, to perform next sync
105    sleep(60 * 15);
106    $needsync = 1;
107}
108
Note: See TracBrowser for help on using the repository browser.