RT# 79780 - added a 60 second pause when there is a connection failure, then retry...
authorChristopher Burger <burgerc@freeside.biz>
Tue, 13 Mar 2018 14:20:06 +0000 (10:20 -0400)
committerChristopher Burger <burgerc@freeside.biz>
Tue, 13 Mar 2018 14:20:06 +0000 (10:20 -0400)
FS/bin/freeside-ipifony-download

index 10faa74..09e83ea 100644 (file)
@@ -104,16 +104,7 @@ if ( $opt{P} =~ /^(\d+)$/ ) {
 }
 
 # for now assume SFTP download as the only method
-print STDERR "Connecting to $sftpuser\@$host...\n" if $opt{v};
-
-my $sftp = Net::SFTP::Foreign->new(
-  host      => $host,
-  user      => $sftpuser,
-  port      => $port,
-  # for now we don't support passwords. use authorized_keys.
-  timeout   => 30,
-  #more      => ($opt{v} ? '-v' : ''),
-);
+my $sftp = sftp_connect($host, $sftpuser, $port);
 die "failed to connect to '$sftpuser\@$host'\n(".$sftp->error.")\n"
   if $sftp->error;
 
@@ -289,6 +280,33 @@ Finished!
 ";
 }
 
+sub sftp_connect {
+  my ($host, $sftpuser, $port) = @_;
+  my $sftp;
+  my $connection_tries = 1;
+
+  while (1) {
+      print STDERR "Connecting to $sftpuser\@$host try number $connection_tries...\n" if $opt{v};
+      $sftp = Net::SFTP::Foreign->new(
+        host      => $host,
+        user      => $sftpuser,
+        port      => $port,
+        # for now we don't support passwords. use authorized_keys.
+        timeout   => 30,
+        #more      => ($opt{v} ? '-v' : ''),
+      );
+
+      if ($sftp->error && $connection_tries < 1200) {
+        $connection_tries++;
+        print STDERR "Connection failed to $sftpuser\@$host trying again in 60 sec...\n" if $opt{v};
+        sleep 60;
+      }
+      else { last; }
+  }
+
+  return $sftp
+}
+
 =head1 NAME
 
 freeside-ipifony-download - Download and import invoice items from IPifony.