1 | #!/usr/bin/perl |
---|
2 | |
---|
3 | use strict; |
---|
4 | use warnings; |
---|
5 | use Getopt::Long; |
---|
6 | use Pod::Usage; |
---|
7 | use Config::IniFiles; |
---|
8 | use LATMOS::Accounts; |
---|
9 | use LATMOS::Accounts::SyncManager; |
---|
10 | use LATMOS::Accounts::Log; |
---|
11 | use LATMOS::Accounts::Task; |
---|
12 | |
---|
13 | GetOptions( |
---|
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 | |
---|
21 | my $needsync = 2; |
---|
22 | my $pidfile = undef; |
---|
23 | $wait ||= 5; # default in minutes |
---|
24 | |
---|
25 | my $syncm = LATMOS::Accounts::SyncManager->new($config) or do { |
---|
26 | la_log LA_ERR, "Cannot instanciate Sync Manager"; |
---|
27 | exit(1); |
---|
28 | }; |
---|
29 | |
---|
30 | la_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 |
---|
47 | if (!$nodaemon) { |
---|
48 | my $pid = fork; |
---|
49 | if ($pid) { |
---|
50 | exit 0; |
---|
51 | } |
---|
52 | } |
---|
53 | |
---|
54 | if ($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 | |
---|
71 | sub 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 | |
---|
92 | my $lasttout; |
---|
93 | my %revs; |
---|
94 | while (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 | |
---|