[610] | 1 | #!/usr/bin/perl |
---|
[578] | 2 | |
---|
| 3 | use strict; |
---|
| 4 | use warnings; |
---|
| 5 | use Getopt::Long; |
---|
| 6 | use Pod::Usage; |
---|
[861] | 7 | use Config::IniFiles; |
---|
[578] | 8 | use LATMOS::Accounts; |
---|
[861] | 9 | use LATMOS::Accounts::SyncManager; |
---|
[578] | 10 | use LATMOS::Accounts::Log; |
---|
[861] | 11 | use LATMOS::Accounts::Task; |
---|
[578] | 12 | |
---|
[1023] | 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 | |
---|
[578] | 23 | GetOptions( |
---|
| 24 | 'nodaemon' => \my $nodaemon, |
---|
| 25 | 'c|config=s' => \my $config, |
---|
| 26 | 'help' => sub { pod2usage(0) }, |
---|
[861] | 27 | 'wait=i' => \my $wait, |
---|
[578] | 28 | ) or pod2usage(); |
---|
| 29 | |
---|
[1023] | 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 | |
---|
[861] | 55 | my $needsync = 2; |
---|
[578] | 56 | my $pidfile = undef; |
---|
[861] | 57 | $wait ||= 5; # default in minutes |
---|
[578] | 58 | |
---|
[861] | 59 | my $syncm = LATMOS::Accounts::SyncManager->new($config) or do { |
---|
| 60 | la_log LA_ERR, "Cannot instanciate Sync Manager"; |
---|
| 61 | exit(1); |
---|
| 62 | }; |
---|
| 63 | |
---|
[578] | 64 | la_set_log( |
---|
| 65 | syslog => [], |
---|
| 66 | console => ($nodaemon ? LA_NOTICE : undef), |
---|
| 67 | ); |
---|
| 68 | |
---|
[861] | 69 | |
---|
[578] | 70 | { |
---|
| 71 | my $LA = LATMOS::Accounts->new($config, noacl => 1); |
---|
| 72 | if ($LA->val('_default_', 'state_dir')) { |
---|
| 73 | $pidfile = $LA->val('_default_', 'state_dir') . '/sync-manager.pid'; |
---|
| 74 | } |
---|
[861] | 75 | if (my $mail = $LA->val('_network_', 'maillog')) { |
---|
| 76 | la_set_log(mail => $mail); |
---|
| 77 | } |
---|
[578] | 78 | } |
---|
| 79 | |
---|
| 80 | # Daemonize |
---|
| 81 | if (!$nodaemon) { |
---|
| 82 | my $pid = fork; |
---|
| 83 | if ($pid) { |
---|
| 84 | exit 0; |
---|
| 85 | } |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | if ($pidfile) { |
---|
| 89 | # maybe we want to lock here !! |
---|
| 90 | open(my $fh, '>', $pidfile) or die "Cannot open pidfile $pidfile\n"; |
---|
| 91 | print $fh "$$\n"; |
---|
| 92 | close($fh); |
---|
| 93 | } |
---|
| 94 | |
---|
[816] | 95 | $SIG{INT} = sub { |
---|
| 96 | unlink($pidfile) if ($pidfile); |
---|
| 97 | exit 0; |
---|
| 98 | }; |
---|
| 99 | |
---|
| 100 | $SIG{'HUP'} = sub { |
---|
| 101 | $needsync = 1; |
---|
| 102 | la_log LA_NOTICE, "SigHup received, synchronise now"; |
---|
| 103 | }; |
---|
| 104 | |
---|
[861] | 105 | sub process { |
---|
| 106 | my ($module, $baserev) = @_; |
---|
| 107 | my $pid = fork; |
---|
| 108 | if ($pid == 0) { |
---|
| 109 | $SIG{INT} = 'DEFAULT'; |
---|
| 110 | my $res = $syncm->process_module($module, $baserev); |
---|
| 111 | exit($res ? 0 : 1); |
---|
| 112 | } |
---|
[915] | 113 | my $retpid; |
---|
| 114 | while(($retpid = waitpid(-1, 0)) <= 0) {} |
---|
| 115 | local $SIG{HUP} = 'IGNORE'; |
---|
| 116 | if ($retpid) { |
---|
| 117 | my $res = $? >> 8; |
---|
| 118 | if ($res) { |
---|
| 119 | la_log LA_ERR, "Sync process exit with $res"; |
---|
| 120 | return; |
---|
[861] | 121 | } |
---|
| 122 | } |
---|
[915] | 123 | return 1; |
---|
[861] | 124 | } |
---|
| 125 | |
---|
| 126 | my $lasttout; |
---|
| 127 | my %revs; |
---|
[578] | 128 | while (1) { |
---|
| 129 | if ($needsync) { |
---|
[861] | 130 | my $levelsync = $needsync; |
---|
[578] | 131 | $needsync = 0; |
---|
[861] | 132 | { |
---|
| 133 | local $SIG{HUP} = 'IGNORE'; |
---|
| 134 | $syncm->updrev; |
---|
| 135 | # listing module to run in this loop |
---|
| 136 | $lasttout = scalar(time) if ($levelsync == 2); |
---|
[816] | 137 | } |
---|
[861] | 138 | foreach my $module ($syncm->list_modules()) { |
---|
| 139 | next if ($syncm->ini->val($module, 'lazy') && $levelsync < 2); |
---|
| 140 | my $res = process($module, $revs{$module} || 0); |
---|
[578] | 141 | if ($res) { |
---|
[861] | 142 | $revs{$module} = $syncm->dbrev; |
---|
| 143 | } |
---|
[578] | 144 | } |
---|
| 145 | } |
---|
| 146 | |
---|
| 147 | # waiting, to perform next sync |
---|
[861] | 148 | sleep(15) unless($needsync); |
---|
| 149 | # if it is not time yet: |
---|
| 150 | next if($lasttout + ($wait * 60) > scalar(time)); |
---|
| 151 | $needsync = 2; |
---|
[578] | 152 | } |
---|
| 153 | |
---|
[1023] | 154 | __END__ |
---|
| 155 | |
---|
| 156 | =head1 SEE ALSO |
---|
| 157 | |
---|
| 158 | L<la-sync-manager.ini>, L<latmos-accounts.ini> |
---|
| 159 | |
---|
| 160 | =head1 AUTHOR |
---|
| 161 | |
---|
| 162 | Olivier Thauvin, E<lt>olivier.thauvin@latmos.ipsl.frE<gt> |
---|
| 163 | |
---|
| 164 | =head1 COPYRIGHT AND LICENSE |
---|
| 165 | |
---|
| 166 | Copyright (C) 2008, 2009, 2010, 2011, 2012 CNRS SA/CETP/LATMOS |
---|
| 167 | |
---|
| 168 | This library is free software; you can redistribute it and/or modify |
---|
| 169 | it under the same terms as Perl itself, either Perl version 5.10.0 or, |
---|
| 170 | at your option, any later version of Perl 5 you may have available. |
---|
| 171 | |
---|
| 172 | =cut |
---|