View difference between Paste ID: KNB7HP4j and xP0kHvXH
SHOW: | | - or go back to the newest paste.
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-
	my $now = &now;
69+
	## Work with DB
70-
	my @params;
70+
71-
	my @workers = @childs;
71+
72-
	$params[1] = $pid;
72+
73-
	$params[2] = DB_USERS_REMOTE_STATUS_NEW;
73+
74-
	$params[3] = REMOTE_MAIL_COUNT;
74+
75-
	$params[4] = DB_USERS_REMOTE_STATUS_PROCESSING;
75+
76-
	$params[5] = REMOTE_MAX_ERROR_COUNT;
76+
77-
	my $q = "(select id, status, process_id, error_count from ".DB_USERS_REMOTE." where status=? and is_active in (?,?) and login_type=? and remote_type=?)
77+
78-
		  union all
78+
79-
		 (select id, status, process_id, error_count from ".DB_USERS_REMOTE." where status=? and is_active in (?,?) and login_type=? and remote_type=? and date_get < ? order by date_get asc limit ?)";
79+
80-
	my $sth = $dbh->prepare($q);
80+
81-
	my $rv = $sth->execute($params[4], 1, 2, 2, 2, $params[2], 1, 2, 2, 2, $now, $params[3]) or print $DBI::errstr;
81+
82-
	while (my ($id, $status, $process_id, $error_count) = $sth->fetchrow_array) {
82+
83-
		if ($status == 2) {
83+
84-
			if (grep(/^$process_id$/, @workers)) {
84+