[Dovecot] Various fixes for imapsync_cron, new imapsync version
parent
5cc09055a7
commit
1115b826f9
|
@ -51,6 +51,7 @@ RUN apt-get update && apt-get -y --no-install-recommends install \
|
||||||
libterm-readkey-perl \
|
libterm-readkey-perl \
|
||||||
libtest-pod-perl \
|
libtest-pod-perl \
|
||||||
libtest-simple-perl \
|
libtest-simple-perl \
|
||||||
|
libtry-tiny-perl \
|
||||||
libunicode-string-perl \
|
libunicode-string-perl \
|
||||||
libproc-processtable-perl \
|
libproc-processtable-perl \
|
||||||
liburi-perl \
|
liburi-perl \
|
||||||
|
|
|
@ -136,6 +136,7 @@ touch /etc/crontab /etc/cron.*/*
|
||||||
[[ -f /usr/local/var/run/dovecot/master.pid ]] && rm /usr/local/var/run/dovecot/master.pid
|
[[ -f /usr/local/var/run/dovecot/master.pid ]] && rm /usr/local/var/run/dovecot/master.pid
|
||||||
|
|
||||||
# Clean stopped imapsync jobs
|
# Clean stopped imapsync jobs
|
||||||
|
rm -f /tmp/imapsync_busy.lock
|
||||||
IMAPSYNC_TABLE=$(mysql -h mysql-mailcow -u ${DBUSER} -p${DBPASS} ${DBNAME} -e "SHOW TABLES LIKE 'imapsync'" -Bs)
|
IMAPSYNC_TABLE=$(mysql -h mysql-mailcow -u ${DBUSER} -p${DBPASS} ${DBNAME} -e "SHOW TABLES LIKE 'imapsync'" -Bs)
|
||||||
[[ ! -z ${IMAPSYNC_TABLE} ]] && mysql -h mysql-mailcow -u ${DBUSER} -p${DBPASS} ${DBNAME} -e "UPDATE imapsync SET is_running='0'"
|
[[ ! -z ${IMAPSYNC_TABLE} ]] && mysql -h mysql-mailcow -u ${DBUSER} -p${DBPASS} ${DBNAME} -e "UPDATE imapsync SET is_running='0'"
|
||||||
|
|
||||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -7,6 +7,8 @@ use Data::Dumper qw(Dumper);
|
||||||
use IPC::Run 'run';
|
use IPC::Run 'run';
|
||||||
use String::Util 'trim';
|
use String::Util 'trim';
|
||||||
use File::Temp;
|
use File::Temp;
|
||||||
|
use Try::Tiny;
|
||||||
|
use sigtrap 'handler' => \&sig_handler, qw(INT TERM KILL QUIT);
|
||||||
|
|
||||||
my $t = Proc::ProcessTable->new;
|
my $t = Proc::ProcessTable->new;
|
||||||
my $imapsync_running = grep { $_->{cmndline} =~ /^\/usr\/bin\/perl \/usr\/local\/bin\/imapsync\s/ } @{$t->table};
|
my $imapsync_running = grep { $_->{cmndline} =~ /^\/usr\/bin\/perl \/usr\/local\/bin\/imapsync\s/ } @{$t->table};
|
||||||
|
@ -16,6 +18,8 @@ if ($imapsync_running eq 1)
|
||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sub qqw($) { split /\s+/, $_[0] }
|
||||||
|
|
||||||
$DBNAME = '';
|
$DBNAME = '';
|
||||||
$DBUSER = '';
|
$DBUSER = '';
|
||||||
$DBPASS = '';
|
$DBPASS = '';
|
||||||
|
@ -29,11 +33,45 @@ $dbh = DBI->connect($dsn, $DBUSER, $DBPASS, {
|
||||||
mysql_auto_reconnect => 1,
|
mysql_auto_reconnect => 1,
|
||||||
mysql_enable_utf8mb4 => 1
|
mysql_enable_utf8mb4 => 1
|
||||||
});
|
});
|
||||||
|
sub sig_handler {
|
||||||
|
# Send die to force exception in "run"
|
||||||
|
die "sig_handler received signal, preparing to exit...\n";
|
||||||
|
};
|
||||||
|
|
||||||
open my $file, '<', "/etc/sogo/sieve.creds";
|
open my $file, '<', "/etc/sogo/sieve.creds";
|
||||||
my $creds = <$file>;
|
my $creds = <$file>;
|
||||||
close $file;
|
close $file;
|
||||||
my ($master_user, $master_pass) = split /:/, $creds;
|
my ($master_user, $master_pass) = split /:/, $creds;
|
||||||
my $sth = $dbh->prepare("SELECT id, user1, user2, host1, authmech1, password1, exclude, port1, enc1, delete2duplicates, maxage, subfolder2, delete1, delete2, automap, skipcrossduplicates, maxbytespersecond FROM imapsync WHERE active = 1 AND is_running = 0 AND (UNIX_TIMESTAMP(NOW()) - UNIX_TIMESTAMP(last_run) > mins_interval * 60 OR last_run IS NULL) ORDER BY last_run");
|
my $sth = $dbh->prepare("SELECT id,
|
||||||
|
user1,
|
||||||
|
user2,
|
||||||
|
host1,
|
||||||
|
authmech1,
|
||||||
|
password1,
|
||||||
|
exclude,
|
||||||
|
port1,
|
||||||
|
enc1,
|
||||||
|
delete2duplicates,
|
||||||
|
maxage,
|
||||||
|
subfolder2,
|
||||||
|
delete1,
|
||||||
|
delete2,
|
||||||
|
automap,
|
||||||
|
skipcrossduplicates,
|
||||||
|
maxbytespersecond,
|
||||||
|
custom_params,
|
||||||
|
subscribeall,
|
||||||
|
timeout1,
|
||||||
|
timeout2
|
||||||
|
FROM imapsync
|
||||||
|
WHERE active = 1
|
||||||
|
AND is_running = 0
|
||||||
|
AND (
|
||||||
|
UNIX_TIMESTAMP(NOW()) - UNIX_TIMESTAMP(last_run) > mins_interval * 60
|
||||||
|
OR
|
||||||
|
last_run IS NULL)
|
||||||
|
ORDER BY last_run");
|
||||||
|
|
||||||
$sth->execute();
|
$sth->execute();
|
||||||
my $row;
|
my $row;
|
||||||
|
|
||||||
|
@ -56,6 +94,10 @@ while ($row = $sth->fetchrow_arrayref()) {
|
||||||
$automap = @$row[14];
|
$automap = @$row[14];
|
||||||
$skipcrossduplicates = @$row[15];
|
$skipcrossduplicates = @$row[15];
|
||||||
$maxbytespersecond = @$row[16];
|
$maxbytespersecond = @$row[16];
|
||||||
|
$custom_params = @$row[17];
|
||||||
|
$subscribeall = @$row[18];
|
||||||
|
$timeout1 = @$row[19];
|
||||||
|
$timeout2 = @$row[20];
|
||||||
|
|
||||||
$is_running = $dbh->prepare("UPDATE imapsync SET is_running = 1 WHERE id = ?");
|
$is_running = $dbh->prepare("UPDATE imapsync SET is_running = 1 WHERE id = ?");
|
||||||
$is_running->bind_param( 1, ${id} );
|
$is_running->bind_param( 1, ${id} );
|
||||||
|
@ -70,22 +112,20 @@ while ($row = $sth->fetchrow_arrayref()) {
|
||||||
print $passfile1 "$password1\n";
|
print $passfile1 "$password1\n";
|
||||||
print $passfile2 trim($master_pass) . "\n";
|
print $passfile2 trim($master_pass) . "\n";
|
||||||
|
|
||||||
run [ "/usr/local/bin/imapsync",
|
my @custom_params_a = qqw($custom_params);
|
||||||
"--timeout1", "600",
|
my $custom_params_ref = \@custom_params_a;
|
||||||
|
|
||||||
|
my $generated_cmds = [ "/usr/local/bin/imapsync",
|
||||||
"--tmpdir", "/tmp",
|
"--tmpdir", "/tmp",
|
||||||
"--subscribeall",
|
|
||||||
"--nofoldersizes",
|
"--nofoldersizes",
|
||||||
"--skipsize",
|
($timeout1 gt "0" ? () : ('--timeout1', $timeout1)),
|
||||||
"--buffersize", "8192000",
|
($timeout2 gt "0" ? () : ('--timeout2', $timeout2)),
|
||||||
"--split1", "3000",
|
|
||||||
"--split2", "3000",
|
|
||||||
"--fastio1",
|
|
||||||
"--fastio2",
|
|
||||||
($exclude eq "" ? () : ("--exclude", $exclude)),
|
($exclude eq "" ? () : ("--exclude", $exclude)),
|
||||||
($subfolder2 eq "" ? () : ('--subfolder2', $subfolder2)),
|
($subfolder2 eq "" ? () : ('--subfolder2', $subfolder2)),
|
||||||
($maxage eq "0" ? () : ('--maxage', $maxage)),
|
($maxage eq "0" ? () : ('--maxage', $maxage)),
|
||||||
($maxbytespersecond eq "0" ? () : ('--maxbytespersecond', $maxbytespersecond)),
|
($maxbytespersecond eq "0" ? () : ('--maxbytespersecond', $maxbytespersecond)),
|
||||||
($delete2duplicates ne "1" ? () : ('--delete2duplicates')),
|
($delete2duplicates ne "1" ? () : ('--delete2duplicates')),
|
||||||
|
($subscribeall ne "1" ? () : ('--subscribeall')),
|
||||||
($delete1 ne "1" ? () : ('--delete')),
|
($delete1 ne "1" ? () : ('--delete')),
|
||||||
($delete2 ne "1" ? () : ('--delete2')),
|
($delete2 ne "1" ? () : ('--delete2')),
|
||||||
($automap ne "1" ? () : ('--automap')),
|
($automap ne "1" ? () : ('--automap')),
|
||||||
|
@ -98,12 +138,21 @@ while ($row = $sth->fetchrow_arrayref()) {
|
||||||
"--host2", "localhost",
|
"--host2", "localhost",
|
||||||
"--user2", $user2 . '*' . trim($master_user),
|
"--user2", $user2 . '*' . trim($master_user),
|
||||||
"--passfile2", $passfile2->filename,
|
"--passfile2", $passfile2->filename,
|
||||||
'--no-modulesversion'], ">", \my $stdout;
|
'--no-modulesversion'];
|
||||||
|
|
||||||
|
try {
|
||||||
|
run [@$generated_cmds, @$custom_params_ref], '&>', \my $stdout;
|
||||||
|
$update = $dbh->prepare("UPDATE imapsync SET returned_text = ?, last_run = NOW(), is_running = 0 WHERE id = ?");
|
||||||
|
$update->bind_param( 1, ${stdout} );
|
||||||
|
$update->bind_param( 2, ${id} );
|
||||||
|
$update->execute();
|
||||||
|
} catch {
|
||||||
|
$update = $dbh->prepare("UPDATE imapsync SET returned_text = 'Could not start or finish imapsync', last_run = NOW(), is_running = 0 WHERE id = ?");
|
||||||
|
$update->bind_param( 1, ${id} );
|
||||||
|
$update->execute();
|
||||||
|
$lockmgr->unlock($lock_file);
|
||||||
|
};
|
||||||
|
|
||||||
$update = $dbh->prepare("UPDATE imapsync SET returned_text = ?, last_run = NOW(), is_running = 0 WHERE id = ?");
|
|
||||||
$update->bind_param( 1, ${stdout} );
|
|
||||||
$update->bind_param( 2, ${id} );
|
|
||||||
$update->execute();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$sth->finish();
|
$sth->finish();
|
||||||
|
|
Loading…
Reference in New Issue