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