Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl -w
- use strict;
- use warnings;
- use FindBin;
- use lib $FindBin::Bin;
- use Lib;
- use const ':constants';
- my $daemon = Proc::Daemon->new;
- Proc::Daemon::Init(
- { work_dir => WORK_DIR,
- child_STDOUT => "+>>".DEBUG_FILE."",
- child_STDERR => "+>>".ERROR_LOG_FILE."",
- pid_file => "".LOG_DIR."/remote_imap.pid"
- }
- );
- if (my $pid = Proc::PID::File->running({dir=>LOG_DIR, verify=>1})) {
- &debug(1, $pid, "Already running!\n");
- exit 0;
- }
- $SIG{'INT'} = \&cleanupAndShutdown;
- $SIG{'TERM'} = \&cleanupAndShutdown;
- $SIG{'HUP'} = \&restartChildren;
- my ($dbname,$dbuser,$dbpass, $dbhost, $dbport) = (DB_NAME, DB_LOGIN, DB_PASSWORD, DB_HOST, DB_PORT);
- my $dbh = DBI->connect("dbi:Pg:dbname=$dbname;host=$dbhost;port=$dbport", $dbuser, $dbpass,
- { RaiseError => 0,
- AutoCommit => 1,
- PrintError => 0,})
- or print $DBI::errstr;
- my $p_pid = $$;
- my @kids = (0 .. DAEMON_MAX_CHILD_PROCESSES-1);
- my (@childs, $kid_pid, $status);
- foreach $kid_pid (@kids) {
- $kid_pid = $daemon->Init(
- { work_dir => WORK_DIR,
- exec_command => "perl remote_imapd.pl $p_pid",
- });
- push @childs, $kid_pid;
- }
- $| = 1;
- while(1) {
- foreach $kid_pid (@kids) {
- $status = $daemon->Status($kid_pid);
- if (!$status){
- my $index = 0;
- $index++ until $childs[$index] eq $kid_pid;
- splice(@childs, $index, 1);
- $kid_pid = $daemon->Init(
- { work_dir => WORK_DIR,
- exec_command => "perl remote_imapd.pl $p_pid",
- });
- push @childs, $kid_pid;
- }
- }
- usleep(DAEMON_DELAY);
- if ($dbh->ping() != 1) {
- $dbh = DBI->connect("dbi:Pg:dbname=$dbname;host=$dbhost;port=$dbport", $dbuser, $dbpass,
- { RaiseError => 0,
- AutoCommit => 1,
- PrintError => 0,})
- or print $DBI::errstr;
- }
- &processing_out_users_remote($dbh, $kid_pid, @childs);
- }
- sub processing_out_users_remote {
- my ($dbh, $pid, @childs) = @_;
- ## Work with DB
- }
- sub cleanupAndShutdown {
- my ($sigName) = @_;
- kill 15, @childs;
- &debug(1, $p_pid, "I kill my child's @childs and die\n");
- exit;
- }
- sub restartChildren {
- my ($sigName) = @_;
- kill 15, @childs;
- &debug(1, $p_pid, "I restart my child's @childs\n");
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement