Skip to content

Commit aa4c6c4

Browse files
committed
Add the connection retry method
1 parent 1faf6ff commit aa4c6c4

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

src/main/java/org/embulk/output/sftp/SftpFileOutput.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public class SftpFileOutput
3434
private final String userInfo;
3535
private final String host;
3636
private final int port;
37+
private final int maxConnectionRetry;
3738
private final String pathPrefix;
3839
private final String sequenceFormat;
3940
private final String fileNameExtension;
@@ -98,6 +99,7 @@ private FileSystemOptions initializeFsOptions(PluginTask task)
9899
this.fsOptions = initializeFsOptions(task);
99100
this.host = task.getHost();
100101
this.port = task.getPort();
102+
this.maxConnectionRetry = task.getMaxConnectionRetry();
101103
this.pathPrefix = task.getPathPrefix();
102104
this.sequenceFormat = task.getSequenceFormat();
103105
this.fileNameExtension = task.getFileNameExtension();
@@ -203,6 +205,26 @@ private String getOutputFilePath()
203205
private FileObject newSftpFile(URI sftpUri)
204206
throws FileSystemException
205207
{
206-
return manager.resolveFile(sftpUri.toString(), fsOptions);
208+
int count = 0;
209+
while (true) {
210+
try {
211+
return manager.resolveFile(sftpUri.toString(), fsOptions);
212+
}
213+
catch (FileSystemException e) {
214+
if (++count == maxConnectionRetry) {
215+
throw e;
216+
}
217+
logger.warn("failed to connect sftp server: " + e.getMessage(), e);
218+
219+
try {
220+
Thread.sleep(count * 1000); // milliseconds
221+
}
222+
catch (InterruptedException e1) {
223+
// Ignore this exception
224+
logger.warn(e.getMessage(), e);
225+
}
226+
logger.warn("retry to connect sftp server: " + count + " times");
227+
}
228+
}
207229
}
208230
}

src/main/java/org/embulk/output/sftp/SftpFileOutputPlugin.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ public interface PluginTask
5353
@ConfigDefault("600") // 10 minutes
5454
public int getSftpConnectionTimeout();
5555

56+
@Config("max_connection_retry")
57+
@ConfigDefault("5") // 5 times retry to connect sftp server if failed.
58+
public int getMaxConnectionRetry();
59+
5660
@Config("path_prefix")
5761
public String getPathPrefix();
5862

0 commit comments

Comments
 (0)