Skip to content

Commit

Permalink
fix: wait for file content to become available
Browse files Browse the repository at this point in the history
  • Loading branch information
jbee committed Nov 14, 2023
1 parent d797796 commit d0794b8
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
import java.time.Duration;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Optional;
Expand Down Expand Up @@ -100,6 +101,10 @@ public interface FileResourceService {
@Nonnull
InputStream getFileResourceContent(FileResource fileResource) throws ConflictException;

@Nonnull
InputStream getFileResourceContent(FileResource fileResource, Duration timeout)
throws ConflictException;

/** Copy fileResource content to outputStream and Return File content length */
void copyFileResourceContent(FileResource fileResource, OutputStream outputStream)
throws IOException, NoSuchElementException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
*/
package org.hisp.dhis.fileresource;

import static java.lang.System.currentTimeMillis;
import static java.time.Duration.ofMillis;
import static java.time.Duration.ofSeconds;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
Expand Down Expand Up @@ -241,14 +245,26 @@ public void deleteFileResource(FileResource fileResource) {
@Override
@Nonnull
public InputStream getFileResourceContent(FileResource fileResource) throws ConflictException {
return getFileResourceContent(fileResource, ofSeconds(10));
}

@Nonnull
@Override
public InputStream getFileResourceContent(FileResource fileResource, java.time.Duration timeout)
throws ConflictException {
String key = fileResource.getStorageKey();
InputStream content = fileResourceContentStore.getFileResourceContent(key);
boolean exists = fileResourceContentStore.fileResourceContentExists(key);
long since = currentTimeMillis();
while (content == null && !timeout.minus(ofMillis(currentTimeMillis() - since)).isNegative()) {
try {
Thread.sleep(50);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
content = fileResourceContentStore.getFileResourceContent(key);
}
if (content == null)
throw new ConflictException(
"File resource exists but content input stream was null (exists? "
+ (exists ? "yes" : "no")
+ ")");
throw new ConflictException("File resource exists but content input stream was null");
return content;
}

Expand Down

0 comments on commit d0794b8

Please sign in to comment.