Custom progress reporting for dependency downloader

This commit is contained in:
Aleksey Kladov
2017-10-10 15:23:02 +03:00
committed by ilmat192
parent f33df4c75e
commit 424a330f7e
2 changed files with 14 additions and 9 deletions
@@ -8,10 +8,18 @@ import java.nio.file.Files
import java.nio.file.StandardCopyOption
import java.util.concurrent.*
typealias ProgressCallback = (url: String, currentBytes: Long, totalBytes: Long) -> Unit
class DependencyDownloader(
var maxAttempts: Int = DEFAULT_MAX_ATTEMPTS,
var attemptIntervalMs: Long = DEFAULT_ATTEMPT_INTERVAL_MS
var attemptIntervalMs: Long = DEFAULT_ATTEMPT_INTERVAL_MS,
customProgressCallback: ProgressCallback? = null
) {
private val progressCallback = customProgressCallback ?: { url, currentBytes, totalBytes ->
print("\rDownloading dependency: $url (${currentBytes.humanReadable}/${totalBytes.humanReadable}). ")
}
val executor = ExecutorCompletionService<Unit>(Executors.newSingleThreadExecutor(object : ThreadFactory {
override fun newThread(r: Runnable?): Thread {
val thread = Thread(r)
@@ -81,10 +89,10 @@ class DependencyDownloader(
var result: Future<Unit>?
do {
updateProgressMsg(originalUrl.toString(), progress.currentBytes, totalBytes)
progressCallback(originalUrl.toString(), progress.currentBytes, totalBytes)
result = executor.poll(1, TimeUnit.SECONDS)
} while(result == null)
updateProgressMsg(originalUrl.toString(), progress.currentBytes, totalBytes)
progressCallback(originalUrl.toString(), progress.currentBytes, totalBytes)
try {
result.get()
@@ -188,10 +196,6 @@ class DependencyDownloader(
return "%.1f %siB".format(this / Math.pow(1024.0, exp.toDouble()), prefix)
}
private fun updateProgressMsg(url: String, currentBytes: Long, totalBytes: Long) {
print("\rDownloading dependency: $url (${currentBytes.humanReadable}/${totalBytes.humanReadable}). ")
}
companion object {
const val DEFAULT_MAX_ATTEMPTS = 10
const val DEFAULT_ATTEMPT_INTERVAL_MS = 3000L
@@ -64,7 +64,8 @@ class DependencyProcessor(dependenciesRoot: File,
homeDependencyCache: String = DEFAULT_HOME_DEPENDENCY_CACHE,
val airplaneMode: Boolean = false,
maxAttempts: Int = DependencyDownloader.DEFAULT_MAX_ATTEMPTS,
attemptIntervalMs: Long = DependencyDownloader.DEFAULT_ATTEMPT_INTERVAL_MS) {
attemptIntervalMs: Long = DependencyDownloader.DEFAULT_ATTEMPT_INTERVAL_MS,
customProgressCallback: ProgressCallback? = null) {
val dependenciesDirectory = dependenciesRoot.apply { mkdirs() }
val cacheDirectory = System.getProperty("user.home")?.let {
@@ -77,7 +78,7 @@ class DependencyProcessor(dependenciesRoot: File,
private var isInfoShown = false
// TOOO: Rename pause -> interval
private val downloader = DependencyDownloader(maxAttempts, attemptIntervalMs)
private val downloader = DependencyDownloader(maxAttempts, attemptIntervalMs, customProgressCallback)
private val extractor = DependencyExtractor()
private val archiveExtension get() = extractor.archiveExtension