Advertisement
Guest User

remote_imap.pl

a guest
Dec 19th, 2014
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/perl -w
  2. use strict;  
  3. use warnings;
  4. use FindBin;              
  5. use lib $FindBin::Bin;  
  6. use Lib;
  7. use const ':constants';
  8.  
  9. my $daemon = Proc::Daemon->new;
  10. Proc::Daemon::Init(
  11.     {   work_dir     => WORK_DIR,
  12.         child_STDOUT => "+>>".DEBUG_FILE."",
  13.         child_STDERR => "+>>".ERROR_LOG_FILE."",
  14.         pid_file     => "".LOG_DIR."/remote_imap.pid"
  15.     }
  16. );
  17. if (my $pid = Proc::PID::File->running({dir=>LOG_DIR, verify=>1})) {
  18.   &debug(1, $pid, "Already running!\n");
  19.   exit 0;
  20. }
  21.  
  22. $SIG{'INT'} = \&cleanupAndShutdown;
  23. $SIG{'TERM'} = \&cleanupAndShutdown;
  24. $SIG{'HUP'} = \&restartChildren;
  25. my ($dbname,$dbuser,$dbpass, $dbhost, $dbport) = (DB_NAME, DB_LOGIN, DB_PASSWORD, DB_HOST, DB_PORT);
  26. my $dbh = DBI->connect("dbi:Pg:dbname=$dbname;host=$dbhost;port=$dbport", $dbuser, $dbpass,
  27.             { RaiseError => 0,
  28.             AutoCommit => 1,
  29.             PrintError => 0,})
  30.     or print $DBI::errstr;
  31. my $p_pid = $$;
  32. my @kids = (0 .. DAEMON_MAX_CHILD_PROCESSES-1);
  33. my (@childs, $kid_pid, $status);
  34. foreach $kid_pid (@kids) {
  35.     $kid_pid = $daemon->Init(
  36.             {   work_dir     => WORK_DIR,
  37.                 exec_command => "perl remote_imapd.pl $p_pid",
  38.             });
  39.          push @childs, $kid_pid;
  40. }
  41. $| = 1;
  42. while(1) {
  43.     foreach $kid_pid (@kids) {
  44.         $status = $daemon->Status($kid_pid);
  45.         if (!$status){
  46.             my $index = 0;
  47.             $index++ until $childs[$index] eq $kid_pid;
  48.             splice(@childs, $index, 1);
  49.             $kid_pid = $daemon->Init(
  50.             {   work_dir     => WORK_DIR,
  51.                 exec_command => "perl remote_imapd.pl $p_pid",
  52.             });
  53.          push @childs, $kid_pid;
  54.         }
  55.     }
  56.     usleep(DAEMON_DELAY);
  57.     if ($dbh->ping() != 1) {
  58.          $dbh = DBI->connect("dbi:Pg:dbname=$dbname;host=$dbhost;port=$dbport", $dbuser, $dbpass,
  59.             { RaiseError => 0,
  60.             AutoCommit => 1,
  61.             PrintError => 0,})
  62.         or print $DBI::errstr;
  63.     }
  64.     &processing_out_users_remote($dbh, $kid_pid, @childs);
  65. }
  66.  
  67. sub processing_out_users_remote {
  68.     my ($dbh, $pid, @childs) = @_;
  69.     ## Work with DB
  70. }
  71.  
  72. sub cleanupAndShutdown {
  73.     my ($sigName) = @_;
  74.     kill 15, @childs;
  75.     &debug(1, $p_pid, "I kill my child's @childs and die\n");
  76.     exit;    
  77. }
  78.  
  79. sub restartChildren {
  80.     my ($sigName) = @_;
  81.     kill 15, @childs;
  82.     &debug(1, $p_pid, "I restart my child's @childs\n");
  83.     return 1;
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement