Skip to content

Commit

Permalink
DEF-3522 Fixed race condition when downloading sdk's (#102)
Browse files Browse the repository at this point in the history
  • Loading branch information
mathiaswking authored Sep 26, 2018
1 parent ae13f84 commit 83e6dda
Showing 1 changed file with 26 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.actuate.metrics.CounterService;
Expand All @@ -25,6 +26,7 @@
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.Comparator;

@Service
Expand Down Expand Up @@ -52,6 +54,24 @@ public class DefoldSdkService {
this.dynamoHome = System.getenv("DYNAMO_HOME") != null ? new File(System.getenv("DYNAMO_HOME")) : null;
}

// Helper function to move the SDK directories
public static void Move(Path source, Path target) {
try {
Files.move(source, target, StandardCopyOption.ATOMIC_MOVE);
} catch (IOException e) {
// If the target path suddenly exists, and the source path still exists,
// then we failed with the atomic move, and we assume another job succeeded with the download
if (Files.exists(source) && Files.exists(target)) {
LOGGER.info("Defold SDK version {} was downloaded by another job in the meantime", source.toString());
try {
FileUtils.deleteDirectory(source.toFile());
} catch (IOException e2) {
LOGGER.error("Failed to delete temp sdk directory {}: {}", source.toString(), e2.getMessage());
}
}
}
}

public File getSdk(String hash) throws IOException, URISyntaxException, ExtenderException {
long methodStart = System.currentTimeMillis();

Expand All @@ -72,9 +92,13 @@ public File getSdk(String hash) throws IOException, URISyntaxException, Extender
throw new ExtenderException(String.format("The given sdk does not exist: %s (%s)", hash, response.getStatusCode().toString()));
}

Files.createDirectories(sdkDirectory.toPath());
File tmpSdkDirectory = Files.createTempDirectory(baseSdkDirectory, hash).toFile(); // Either moved or deleted later by Move()

Files.createDirectories(tmpSdkDirectory.toPath());
InputStream body = response.getBody();
ZipUtils.unzip(body, sdkDirectory.toPath());
ZipUtils.unzip(body, tmpSdkDirectory.toPath());

Move(tmpSdkDirectory.toPath(), sdkDirectory.toPath());
}

// Delete old SDK:s
Expand Down

0 comments on commit 83e6dda

Please sign in to comment.