-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'dev' into trip-sharing
- Loading branch information
Showing
27 changed files
with
1,163 additions
and
253 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
80 changes: 80 additions & 0 deletions
80
src/main/java/org/opentripplanner/middleware/connecteddataplatform/IntervalUploadFiles.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,80 @@ | ||
package org.opentripplanner.middleware.connecteddataplatform; | ||
|
||
import org.opentripplanner.middleware.bugsnag.BugsnagReporter; | ||
import org.opentripplanner.middleware.utils.FileUtils; | ||
import org.opentripplanner.middleware.utils.S3Exception; | ||
import org.opentripplanner.middleware.utils.S3Utils; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
|
||
import static org.opentripplanner.middleware.connecteddataplatform.ConnectedDataManager.CONNECTED_DATA_PLATFORM_S3_BUCKET_NAME; | ||
import static org.opentripplanner.middleware.connecteddataplatform.ConnectedDataManager.ZIP_FILE_EXTENSION; | ||
|
||
/** | ||
* Helper class for upload job file handling. | ||
* When used in try-with-resource blocks, temp files will be automatically deleted. | ||
*/ | ||
public class IntervalUploadFiles implements AutoCloseable { | ||
private static final Logger LOG = LoggerFactory.getLogger(IntervalUploadFiles.class); | ||
|
||
private final boolean isTest; | ||
|
||
private final String zipFileName; | ||
|
||
private final String tempZipFile; | ||
|
||
private final String tempDataFile; | ||
|
||
public IntervalUploadFiles(String filePrefix, String coreExtension, boolean isTest) { | ||
this.isTest = isTest; | ||
|
||
zipFileName = String.join(".", filePrefix, ZIP_FILE_EXTENSION); | ||
String tempFileFolder = FileUtils.getTempDirectory().getAbsolutePath(); | ||
tempZipFile = String.join(File.separator, tempFileFolder, zipFileName); | ||
tempDataFile = String.join(File.separator, tempFileFolder, String.join(".", filePrefix, coreExtension)); | ||
} | ||
|
||
public String getTempDataFile() { | ||
return tempDataFile; | ||
} | ||
|
||
/** | ||
* Compress and upload the data file. | ||
*/ | ||
public void compressAndUpload(String folder) throws IOException { | ||
FileUtils.addSingleFileToZip(tempDataFile, tempZipFile); | ||
try { | ||
S3Utils.putObject( | ||
CONNECTED_DATA_PLATFORM_S3_BUCKET_NAME, | ||
String.format("%s/%s", folder, zipFileName), | ||
new File(tempZipFile) | ||
); | ||
} catch (S3Exception e) { | ||
String message = String.format("Error uploading (%s) to S3", zipFileName); | ||
LOG.error(message); | ||
BugsnagReporter.reportErrorToBugsnag(message, e); | ||
} | ||
} | ||
|
||
@Override | ||
public void close() throws IOException { | ||
// Delete the temporary files here, to cover S3 upload success or failure. | ||
try { | ||
LOG.info("Deleting zip file {}.", tempZipFile); | ||
FileUtils.deleteFile(tempDataFile); | ||
if (!isTest) { | ||
FileUtils.deleteFile(tempZipFile); | ||
} else { | ||
LOG.warn("In test mode, temp zip file {} not deleted. This is expected to be deleted by the calling test.", | ||
tempZipFile | ||
); | ||
} | ||
} catch (IOException e) { | ||
LOG.error("Failed to delete temp files", e); | ||
throw e; | ||
} | ||
} | ||
} |
Oops, something went wrong.