Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(server): allow to refresh generated artifacts on demand #1598

Merged
merged 1 commit into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/bindings-server.main.kts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ workflow(
cleanMavenLocal()

run(
name = "Execute the script using the bindings from the serve - with /binding",
name = "Execute the script using the bindings from the server - with /binding",
command = """
mv .github/workflows/test-script-consuming-jit-bindings-old.main.do-not-compile.kts .github/workflows/test-script-consuming-jit-bindings-old.main.kts
.github/workflows/test-script-consuming-jit-bindings-old.main.kts
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/bindings-server.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ jobs:
name: 'Clean Maven Local to fetch required POMs again'
run: 'rm -rf ~/.m2/repository/'
- id: 'step-5'
name: 'Execute the script using the bindings from the serve - with /binding'
name: 'Execute the script using the bindings from the server - with /binding'
run: |-
mv .github/workflows/test-script-consuming-jit-bindings-old.main.do-not-compile.kts .github/workflows/test-script-consuming-jit-bindings-old.main.kts
.github/workflows/test-script-consuming-jit-bindings-old.main.kts
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import io.github.typesafegithub.workflows.mavenbinding.buildVersionArtifacts
import io.github.typesafegithub.workflows.shared.internal.getGithubToken
import io.ktor.http.ContentType
import io.ktor.http.HttpStatusCode
import io.ktor.server.application.ApplicationCall
import io.ktor.server.application.call
import io.ktor.server.application.install
import io.ktor.server.engine.embeddedServer
Expand Down Expand Up @@ -58,15 +59,30 @@ fun main() {
metadata()
}

route("/refresh") {
Vampire marked this conversation as resolved.
Show resolved Hide resolved
route("{owner}/{name}/{version}/{file}") {
artifact(bindingsCache, refresh = true)
}

route("{owner}/{name}/{file}") {
metadata(refresh = true)
}
krzema12 marked this conversation as resolved.
Show resolved Hide resolved
}

get("/status") {
call.respondText("OK")
}
}
}.start(wait = true)
}

private fun Route.metadata() {
private fun Route.metadata(refresh: Boolean = false) {
get {
if (refresh && !deliverOnRefreshRoute) {
call.respondText(text = "Not found", status = HttpStatusCode.NotFound)
return@get
}

val owner = call.parameters["owner"]!!
val name = call.parameters["name"]!!
val file = call.parameters["file"]!!
Expand All @@ -88,29 +104,18 @@ private fun Route.metadata() {
}
}

private fun Route.artifact(bindingsCache: Cache<ActionCoords, Result<Map<String, Artifact>>>) {
private fun Route.artifact(
bindingsCache: Cache<ActionCoords, Result<Map<String, Artifact>>>,
refresh: Boolean = false,
) {
get {
val owner = call.parameters["owner"]!!
val name = call.parameters["name"]!!
val version = call.parameters["version"]!!
val actionCoords =
ActionCoords(
owner = owner,
name = name,
version = version,
)
println("➡️ Requesting ${actionCoords.prettyPrint}")
val bindingArtifacts =
bindingsCache
.get(actionCoords) {
actionCoords.buildVersionArtifacts()?.let {
Result.success(it)
} ?: Result.failure(object : Throwable() {})
}.getOrNull()

val bindingArtifacts = call.toBindingArtifacts(bindingsCache, refresh)
if (bindingArtifacts == null) {
call.respondText("Not found", status = HttpStatusCode.NotFound)
return@get
} else if (refresh && !deliverOnRefreshRoute) {
call.respondText(text = "OK")
return@get
}

val file = call.parameters["file"]!!
Expand All @@ -131,24 +136,8 @@ private fun Route.artifact(bindingsCache: Cache<ActionCoords, Result<Map<String,
}

head {
val owner = call.parameters["owner"]!!
val name = call.parameters["name"]!!
val version = call.parameters["version"]!!
val bindingArtifacts = call.toBindingArtifacts(bindingsCache, refresh)
val file = call.parameters["file"]!!
val actionCoords =
ActionCoords(
owner = owner,
name = name,
version = version,
)
val bindingArtifacts =
bindingsCache
.get(actionCoords) {
actionCoords.buildVersionArtifacts()?.let {
Result.success(it)
} ?: Result.failure(object : Throwable() {})
}.getOrNull()

if (bindingArtifacts == null) {
call.respondText("Not found", status = HttpStatusCode.NotFound)
return@head
Expand All @@ -160,3 +149,36 @@ private fun Route.artifact(bindingsCache: Cache<ActionCoords, Result<Map<String,
}
}
}

private suspend fun ApplicationCall.toBindingArtifacts(
bindingsCache: Cache<ActionCoords, Result<Map<String, Artifact>>>,
refresh: Boolean,
): Map<String, Artifact>? {
val owner = parameters["owner"]!!
val name = parameters["name"]!!
val version = parameters["version"]!!
val actionCoords =
ActionCoords(
owner = owner,
name = name,
version = version,
)
println("➡️ Requesting ${actionCoords.prettyPrint}")
val bindingArtifacts =
if (refresh) {
actionCoords.buildVersionArtifacts().also {
bindingsCache.put(actionCoords, Result.of(it))
}
} else {
bindingsCache
.get(actionCoords) { Result.of(actionCoords.buildVersionArtifacts()) }
.getOrNull()
}
return bindingArtifacts
}

private fun Result.Companion.failure(): Result<Nothing> = failure(object : Throwable() {})

private fun <T> Result.Companion.of(value: T?): Result<T> = value?.let { success(it) } ?: failure()

private val deliverOnRefreshRoute = System.getenv("GWKT_DELIVER_ON_REFRESH").toBoolean()
Loading