-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #158 from hasadna/fix-gtfs-files-leak
Fix gtfs files leak
- Loading branch information
Showing
20 changed files
with
739 additions
and
1,125 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
siri/gtfs_reader/src/main/java/il/org/hasadna/siri_client/gtfs/crud/FtpClientService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package il.org.hasadna.siri_client.gtfs.crud; | ||
|
||
import il.org.hasadna.siri_client.gtfs.main.GtfsCollectorConfiguration; | ||
import java.io.BufferedOutputStream; | ||
import java.io.FileOutputStream; | ||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
import java.nio.file.Path; | ||
import java.util.concurrent.TimeUnit; | ||
import net.jodah.failsafe.Failsafe; | ||
import net.jodah.failsafe.RetryPolicy; | ||
import org.apache.commons.net.ftp.FTP; | ||
import org.apache.commons.net.ftp.FTPClient; | ||
import org.apache.commons.net.ftp.FTPReply; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
public class FtpClientService { | ||
|
||
private static Logger logger = LoggerFactory.getLogger(FtpClientService.class); | ||
|
||
public Path downloadFileWithRetry(String ftpServer, String remoteFileName, Path localFilePath) throws DownloadFailedException { | ||
|
||
try { | ||
RetryPolicy retryPolicy = new RetryPolicy() | ||
.retryOn(DownloadFailedException.class) | ||
.withBackoff(1, 30, TimeUnit.MINUTES) | ||
.withMaxRetries(5); | ||
|
||
return Failsafe.with(retryPolicy).get(() -> downloadFile(ftpServer, remoteFileName, localFilePath)); | ||
|
||
} catch (Exception e) { | ||
throw new DownloadFailedException(e); | ||
} | ||
} | ||
|
||
private Path downloadFile(String ftpServer, String remoteFileName, Path localFilePath) throws DownloadFailedException { | ||
OutputStream localFileOutputStream = null; | ||
|
||
try { | ||
logger.info("connect to ftp..."); | ||
FTPClient ftpClient = connect(ftpServer); | ||
localFileOutputStream = new BufferedOutputStream(new FileOutputStream(localFilePath.toFile())); | ||
|
||
logger.info("start downloading from ftp..."); | ||
|
||
if (!ftpClient.retrieveFile(remoteFileName, localFileOutputStream)) { | ||
logger.error("retrieveFile returned false, fileName={}", remoteFileName); | ||
throw new DownloadFailedException("Failed to download the file: " + remoteFileName); | ||
} | ||
|
||
return localFilePath; | ||
|
||
} catch (IOException ex) { | ||
logger.error("failed to retrieve file from ftp", ex); | ||
throw new DownloadFailedException("Failed to download the file: " + remoteFileName); | ||
|
||
} finally { | ||
logger.info(" ... done"); | ||
|
||
if (localFileOutputStream != null) { | ||
try { | ||
localFileOutputStream.close(); | ||
} catch (IOException e) { | ||
logger.error("failed to close local file output stream", e); | ||
} | ||
} | ||
} | ||
|
||
} | ||
|
||
private FTPClient connect(String host) throws IOException { | ||
FTPClient ftpClient = new FTPClient(); | ||
ftpClient.setConnectTimeout((int)GtfsCollectorConfiguration.getGtfsConnectTimeoutMilliseconds()); | ||
ftpClient.connect(host); | ||
ftpClient.login("anonymous", ""); | ||
ftpClient.enterLocalPassiveMode(); | ||
ftpClient.setFileType(FTP.BINARY_FILE_TYPE); | ||
|
||
if (!FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) { | ||
throw new IOException("Faild to connect to: " + host); | ||
} | ||
|
||
return ftpClient; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
217 changes: 0 additions & 217 deletions
217
siri/gtfs_reader/src/main/java/il/org/hasadna/siri_client/gtfs/crud/GtfsFtp.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.