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 | =head1 NAME |
---|
14 | |
---|
15 | la-sync-manager - Daemon performing synchronisation and various tasks |
---|
16 | |
---|
17 | =head1 SYNOPSIS |
---|
18 | |
---|
19 | la-sync-manager [options...] |
---|
20 | |
---|
21 | =cut |
---|
22 | |
---|
23 | GetOptions( |
---|
24 | 'nodaemon' => \my $nodaemon, |
---|
25 | 'c|config=s' => \my $config, |
---|
26 | 'help' => sub { pod2usage(0) }, |
---|
27 | 'wait=i' => \my $wait, |
---|
28 | ) or pod2usage(); |
---|
29 | |
---|
30 | =head1 OPTIONS |
---|
31 | |
---|
32 | =over 4 |
---|
33 | |
---|
34 | =item --nodaemon |
---|
35 | |
---|
36 | Don't go into background |
---|
37 | |
---|
38 | =item -c|--config configdir |
---|
39 | |
---|
40 | Use this configuration instead default one |
---|
41 | |
---|
42 | =item --help |
---|
43 | |
---|
44 | Display this help |
---|
45 | |
---|
46 | =item --wait minutes |
---|
47 | |
---|
48 | Wait this number of minutes before process synchronisation (default is 5 |
---|
49 | minutes) |
---|
50 | |
---|
51 | =back |
---|
52 | |
---|
53 | =cut |
---|
54 | |
---|
55 | my $needsync = 0; |
---|
56 | my $pidfile = undef; |
---|
57 | $wait ||= 5; # default in minutes |
---|
58 | |
---|
59 | my $syncm = LATMOS::Accounts::SyncManager->new($config) or do { |
---|
60 | la_log LA_ERR, "Cannot instanciate Sync Manager"; |
---|
61 | exit(1); |
---|
62 | }; |
---|
63 | |
---|
64 | la_set_log( |
---|
65 | syslog => [], |
---|
66 | console => ($nodaemon ? LA_NOTICE : undef), |
---|
67 | ); |
---|
68 | |
---|
69 | # Trap perl message, send it to log |
---|
70 | $SIG{__DIE__} = sub { |
---|
71 | la_log LA_ERR, "Die: %s", $_[0] unless($_[0] =~ /^Can't locate/); |
---|
72 | die $_[0]; |
---|
73 | }; |
---|
74 | $SIG{__WARN__} = sub { |
---|
75 | la_log LA_WARN, "Warn: %s", $_[0]; |
---|
76 | warn $_[0]; |
---|
77 | }; |
---|
78 | |
---|
79 | { |
---|
80 | my $LA = LATMOS::Accounts->new($config, noacl => 1); |
---|
81 | if ($LA->val('_default_', 'state_dir')) { |
---|
82 | $pidfile = $LA->val('_default_', 'state_dir') . '/sync-manager.pid'; |
---|
83 | } |
---|
84 | if (my $mail = $LA->val('_network_', 'maillog')) { |
---|
85 | la_set_log(mail => $mail); |
---|
86 | } |
---|
87 | } |
---|
88 | |
---|
89 | # Daemonize |
---|
90 | if (!$nodaemon) { |
---|
91 | my $pid = fork; |
---|
92 | if ($pid) { |
---|
93 | exit 0; |
---|
94 | } |
---|
95 | } |
---|
96 | |
---|
97 | if ($pidfile) { |
---|
98 | # maybe we want to lock here !! |
---|
99 | open(my $fh, '>', $pidfile) or die "Cannot open pidfile $pidfile\n"; |
---|
100 | print $fh "$$\n"; |
---|
101 | close($fh); |
---|
102 | } |
---|
103 | |
---|
104 | $SIG{INT} = sub { |
---|
105 | unlink($pidfile) if ($pidfile); |
---|
106 | exit 0; |
---|
107 | }; |
---|
108 | |
---|
109 | $SIG{'HUP'} = sub { |
---|
110 | $needsync = 1; |
---|
111 | la_log LA_NOTICE, "SigHup received, synchronise now"; |
---|
112 | }; |
---|
113 | |
---|
114 | sub process { |
---|
115 | |
---|
116 | la_log LA_NOTICE, "Start processing modules"; |
---|
117 | my $pid = fork(); |
---|
118 | |
---|
119 | if ($pid == 0) { |
---|
120 | $SIG{INT} = 'DEFAULT'; |
---|
121 | my $res = 0; |
---|
122 | foreach my $module ($syncm->listSortedModules()) { |
---|
123 | $res = 1 if (!$syncm->process_module($module)); |
---|
124 | } |
---|
125 | exit($res); |
---|
126 | } |
---|
127 | |
---|
128 | my $retpid; |
---|
129 | while(($retpid = waitpid(-1, 0)) <= 0) {} |
---|
130 | local $SIG{HUP} = 'IGNORE'; |
---|
131 | if ($retpid) { |
---|
132 | my $res = $? >> 8; |
---|
133 | if ($res) { |
---|
134 | la_log LA_ERR, "Sync process exit with $res"; |
---|
135 | return; |
---|
136 | } |
---|
137 | } |
---|
138 | la_log LA_NOTICE, "End processing modules"; |
---|
139 | return 1; |
---|
140 | } |
---|
141 | |
---|
142 | while (1) { |
---|
143 | $needsync = 0; |
---|
144 | |
---|
145 | process(); |
---|
146 | |
---|
147 | # waiting, to perform next sync |
---|
148 | if ($needsync) { |
---|
149 | la_log(LA_DEBUG, "NeedSync received"); |
---|
150 | next; |
---|
151 | } |
---|
152 | |
---|
153 | la_log(LA_DEBUG, "Sleeping $wait minutes"); |
---|
154 | sleep($wait * 60); |
---|
155 | |
---|
156 | } |
---|
157 | |
---|
158 | __END__ |
---|
159 | |
---|
160 | =head1 SEE ALSO |
---|
161 | |
---|
162 | L<la-sync-manager.ini>, L<latmos-accounts.ini> |
---|
163 | |
---|
164 | =head1 AUTHOR |
---|
165 | |
---|
166 | Olivier Thauvin, E<lt>olivier.thauvin@latmos.ipsl.frE<gt> |
---|
167 | |
---|
168 | =head1 COPYRIGHT AND LICENSE |
---|
169 | |
---|
170 | Copyright (C) 2008, 2009, 2010, 2011, 2012 CNRS SA/CETP/LATMOS |
---|
171 | |
---|
172 | This library is free software; you can redistribute it and/or modify |
---|
173 | it under the same terms as Perl itself, either Perl version 5.10.0 or, |
---|
174 | at your option, any later version of Perl 5 you may have available. |
---|
175 | |
---|
176 | =cut |
---|