Skip to content

Commit

Permalink
Re-instantiate GithubApiClient regularly
Browse files Browse the repository at this point in the history
Our application typically crashes after some time (>1 day) when running
in production, similarly to what we experienced with the ApolloClient
some time ago. We hope that re-instantiating the client will solvee
this issue. If not, we will have to do some form of try-catch within
the coroutines, for instance resulting in a reboot.
  • Loading branch information
hermanwh committed Jun 12, 2024
1 parent 7a6f228 commit 09e7314
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions src/main/kotlin/no/digipost/github/monitoring/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,15 @@ suspend fun main(): Unit = coroutineScope {
.register(prometheusMeterRegistry)

val apolloClientFactory = cachedApolloClientFactory(githubToken)
val githubApiClient = GithubApiClient(githubToken)
val githubApiClientFactory = cachedGithubApiClientFactory(githubToken)
val slackClient = slackWebhookUrl?.let{ SlackClient(it) }

launch {
while (isActive) {
try {
withTimeout(TIMOUT_PUBLISH_VULNS) {
val timeMillis = measureTimeMillis {
publish(apolloClientFactory.invoke(), githubApiClient, slackClient, severityLimitForNotifications, multiGaugeRepoVulnCount, multiGaugeContainerScan, multiGaugeInfoScore)
publish(apolloClientFactory.invoke(), githubApiClientFactory.invoke(), slackClient, severityLimitForNotifications, multiGaugeRepoVulnCount, multiGaugeContainerScan, multiGaugeInfoScore)
}
logger.info("Henting av repos med sårbarheter tok ${timeMillis}ms")
}
Expand All @@ -96,6 +96,28 @@ suspend fun main(): Unit = coroutineScope {
.startMetricsServer(prometheusMeterRegistry, 9610)
}

fun cachedGithubApiClientFactory(token: String): () -> GithubApiClient {

val fakt: (String) -> GithubApiClient = { t: String ->
GithubApiClient(t)
}

val age = AtomicLong(System.currentTimeMillis());
var client = fakt(token);

return {
if (System.currentTimeMillis() - age.get() < 1000 * 60 * 60 * 10) {
println("Cachet GithubApiClient")
client
} else {
println("Lager ny GithubApiClient")
client = fakt(token)
age.set(System.currentTimeMillis())
client
}
}
}

fun cachedApolloClientFactory(token: String): () -> ApolloClient {

val fakt: (String) -> ApolloClient = { t: String ->
Expand Down

0 comments on commit 09e7314

Please sign in to comment.