Browse Source

[Dovecot] Various fixes for imapsync_cron, new imapsync version

André 7 years ago
parent
commit
1115b826f9

+ 1 - 0
data/Dockerfiles/dovecot/Dockerfile

@@ -51,6 +51,7 @@ RUN apt-get update && apt-get -y --no-install-recommends install \
   libterm-readkey-perl \
   libtest-pod-perl \
   libtest-simple-perl \
+  libtry-tiny-perl \
   libunicode-string-perl \
   libproc-processtable-perl \
   liburi-perl \

+ 1 - 0
data/Dockerfiles/dovecot/docker-entrypoint.sh

@@ -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
 
 # 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)
 [[ ! -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
+ 265 - 176
data/Dockerfiles/dovecot/imapsync


+ 64 - 15
data/Dockerfiles/dovecot/imapsync_cron.pl

@@ -7,6 +7,8 @@ use Data::Dumper qw(Dumper);
 use IPC::Run 'run';
 use String::Util 'trim';
 use File::Temp;
+use Try::Tiny;
+use sigtrap 'handler' => \&sig_handler, qw(INT TERM KILL QUIT);
 
 my $t = Proc::ProcessTable->new;
 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;
 }
 
+sub qqw($) { split /\s+/, $_[0] }
+
 $DBNAME = '';
 $DBUSER = '';
 $DBPASS = '';
@@ -29,11 +33,45 @@ $dbh = DBI->connect($dsn, $DBUSER, $DBPASS, {
   mysql_auto_reconnect => 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"; 
 my $creds = <$file>; 
 close $file;
 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();
 my $row;
 
@@ -56,6 +94,10 @@ while ($row = $sth->fetchrow_arrayref()) {
   $automap             = @$row[14];
   $skipcrossduplicates = @$row[15];
   $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->bind_param( 1, ${id} );
@@ -70,22 +112,20 @@ while ($row = $sth->fetchrow_arrayref()) {
   print $passfile1 "$password1\n";
   print $passfile2 trim($master_pass) . "\n";
 
-  run [ "/usr/local/bin/imapsync",
-	"--timeout1", "600",
+  my @custom_params_a = qqw($custom_params);
+  my $custom_params_ref = \@custom_params_a;
+
+  my $generated_cmds = [ "/usr/local/bin/imapsync",
 	"--tmpdir", "/tmp",
-	"--subscribeall",
 	"--nofoldersizes",
-	"--skipsize",
-	"--buffersize", "8192000",
-	"--split1", "3000",
-	"--split2", "3000",
-	"--fastio1",
-	"--fastio2",
+	($timeout1 gt "0" ? () : ('--timeout1', $timeout1)),
+	($timeout2 gt "0" ? () : ('--timeout2', $timeout2)),
 	($exclude eq ""	? () : ("--exclude", $exclude)),
 	($subfolder2 eq "" ? () : ('--subfolder2', $subfolder2)),
 	($maxage eq "0" ? () : ('--maxage', $maxage)),
 	($maxbytespersecond eq "0" ? () : ('--maxbytespersecond', $maxbytespersecond)),
 	($delete2duplicates	ne "1" ? () : ('--delete2duplicates')),
+	($subscribeall	ne "1" ? () : ('--subscribeall')),
 	($delete1	ne "1" ? () : ('--delete')),
   ($delete2 ne "1" ? () : ('--delete2')),
   ($automap ne "1" ? () : ('--automap')),
@@ -98,12 +138,21 @@ while ($row = $sth->fetchrow_arrayref()) {
 	"--host2", "localhost",
 	"--user2", $user2 . '*' . trim($master_user),
 	"--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();

Some files were not shown because too many files changed in this diff