Skip to content

Commit

Permalink
fix:jobs reloading while workflow is running
Browse files Browse the repository at this point in the history
  • Loading branch information
cunla committed Apr 24, 2024
1 parent 38a3ddd commit 1e3cd22
Showing 1 changed file with 24 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,53 +4,54 @@ import com.dsoftware.ghmanager.api.GhApiRequestExecutor
import com.dsoftware.ghmanager.api.GithubApi
import com.dsoftware.ghmanager.api.model.Job
import com.dsoftware.ghmanager.api.model.WorkflowRunJobs
import com.intellij.collaboration.async.CompletableFutureUtil.submitIOTask
import com.intellij.collaboration.util.ProgressIndicatorsProvider
import com.intellij.openapi.Disposable
import com.intellij.openapi.diagnostic.thisLogger
import com.intellij.openapi.progress.ProgressManager
import com.intellij.util.EventDispatcher
import org.jetbrains.plugins.github.api.GithubApiRequest
import org.jetbrains.plugins.github.exceptions.GithubStatusCodeException
import org.jetbrains.plugins.github.util.LazyCancellableBackgroundProcessValue
import java.io.IOException
import java.util.EventListener
import java.util.concurrent.CompletableFuture
import kotlin.properties.ReadOnlyProperty

open class DataProvider<T>(
private val requestExecutor: GhApiRequestExecutor,
private val githubApiRequest: GithubApiRequest<T>,
) {
private val runChangesEventDispatcher = EventDispatcher.create(DataProviderChangeListener::class.java)

private val processBackgroundProcess: LazyCancellableBackgroundProcessValue<T> =
LazyCancellableBackgroundProcessValue.create(ProgressManager.getInstance()) {
try {
LOG.info("Executing ${githubApiRequest.url}")
val request = githubApiRequest
val response = requestExecutor.execute(it, request)
response
} catch (e: GithubStatusCodeException) {
LOG.warn("Error when getting $githubApiRequest.url: status code ${e.statusCode}: ${e.message}")
throw e
} catch (ioe: IOException) {
LOG.warn("Error when getting $githubApiRequest.url: $ioe")
throw ioe
}
}
val processValue by backgroundProcessValue(processBackgroundProcess)

private fun <T> backgroundProcessValue(backingValue: LazyCancellableBackgroundProcessValue<T>)
: ReadOnlyProperty<Any?, CompletableFuture<T>> =
ReadOnlyProperty { _, _ -> backingValue.value }
var processValue = processRequest()

fun url(): String = githubApiRequest.url

fun reload() {
LOG.debug("Reloading data for ${githubApiRequest.url}")
processBackgroundProcess.drop()
if (processValue.isDone) {
processValue = processRequest()
}
runChangesEventDispatcher.multicaster.changed()
}

private fun processRequest(): CompletableFuture<T> {
return ProgressManager.getInstance()
.submitIOTask(ProgressIndicatorsProvider(), true) {
try {
LOG.info("Executing ${githubApiRequest.url}")
val request = githubApiRequest
val response = requestExecutor.execute(it, request)
response
} catch (e: GithubStatusCodeException) {
LOG.warn("Error when getting $githubApiRequest.url: status code ${e.statusCode}: ${e.message}")
throw e
} catch (ioe: IOException) {
LOG.warn("Error when getting $githubApiRequest.url: $ioe")
throw ioe
}
}
}

interface DataProviderChangeListener : EventListener {
fun changed() {}
}
Expand Down

0 comments on commit 1e3cd22

Please sign in to comment.