Skip to content

Commit

Permalink
Adding support to bind multiple listeners to backfill run events
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewZeitler committed Apr 24, 2024
1 parent f17a44d commit 9d68d55
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package app.cash.backfila.dashboard

import app.cash.backfila.service.BackfillRunListener
import app.cash.backfila.service.SlackHelper
import app.cash.backfila.service.persistence.BackfilaDb
import app.cash.backfila.service.persistence.BackfillState
Expand All @@ -19,7 +20,7 @@ import wisp.logging.getLogger
class BackfillStateToggler @Inject constructor(
@BackfilaDb private val transacter: Transacter,
private val queryFactory: Query.Factory,
private val slackHelper: SlackHelper,
private val backfillRunListeners: Set<BackfillRunListener>,
) {
fun toggleRunningState(id: Long, caller: MiskCaller, desiredState: BackfillState) {
val requiredCurrentState = when (desiredState) {
Expand Down Expand Up @@ -59,9 +60,13 @@ class BackfillStateToggler @Inject constructor(
}

if (desiredState == RUNNING) {
slackHelper.runStarted(Id(id), caller.principal)
for (listener in backfillRunListeners) {
listener.runStarted(Id(id), caller.principal)
}
} else {
slackHelper.runPaused(Id(id), caller.principal)
for (listener in backfillRunListeners) {
listener.runPaused(Id(id), caller.principal)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ class BackfilaServiceModule(
install(SlackModule(config.slack))
}

newMultibinder<BackfillRunListener>()
.addBinding()
.to(SlackHelper::class)

// TODO:mikepaw Require that the Admin Console is installed so it isn't forgotten.
// something along the lines of requireBinding but works for multibindings.
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package app.cash.backfila.service

import app.cash.backfila.service.persistence.DbBackfillRun
import misk.hibernate.Id
import misk.hibernate.load

interface BackfillRunListener {
fun runStarted(id: Id<DbBackfillRun>, user: String)
fun runPaused(id: Id<DbBackfillRun>, user: String)
fun runErrored(id: Id<DbBackfillRun>)
fun runCompleted(id: Id<DbBackfillRun>)
}
10 changes: 5 additions & 5 deletions service/src/main/kotlin/app/cash/backfila/service/SlackHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ class SlackHelper @Inject constructor(
private val slackClient: SlackClient,
private val backfilaConfig: BackfilaConfig,
private val deployment: Deployment,
) {
fun runStarted(id: Id<DbBackfillRun>, user: String) {
) : BackfillRunListener {
override fun runStarted(id: Id<DbBackfillRun>, user: String) {
val (message, channel) = transacter.transaction { session ->
val run = session.load(id)
val message = ":backfila_start:${dryRunEmoji(run)} ${nameAndId(run)} started by @$user"
Expand All @@ -24,7 +24,7 @@ class SlackHelper @Inject constructor(
slackClient.postMessage("Backfila", ":backfila:", message, channel)
}

fun runPaused(id: Id<DbBackfillRun>, user: String) {
override fun runPaused(id: Id<DbBackfillRun>, user: String) {
val (message, channel) = transacter.transaction { session ->
val run = session.load(id)
val message = ":backfila_pause:${dryRunEmoji(run)} ${nameAndId(run)} paused by @$user"
Expand All @@ -33,7 +33,7 @@ class SlackHelper @Inject constructor(
slackClient.postMessage("Backfila", ":backfila:", message, channel)
}

fun runErrored(id: Id<DbBackfillRun>) {
override fun runErrored(id: Id<DbBackfillRun>) {
val (message, channel) = transacter.transaction { session ->
val run = session.load(id)
val message = ":backfila_error:${dryRunEmoji(run)} ${nameAndId(run)} paused due to error"
Expand All @@ -42,7 +42,7 @@ class SlackHelper @Inject constructor(
slackClient.postMessage("Backfila", ":backfila:", message, channel)
}

fun runCompleted(id: Id<DbBackfillRun>) {
override fun runCompleted(id: Id<DbBackfillRun>) {
val (message, channel) = transacter.transaction { session ->
val run = session.load(id)
val message = ":backfila_complete:${dryRunEmoji(run)} ${nameAndId(run)} completed"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import app.cash.backfila.protos.clientservice.RunBatchRequest
import app.cash.backfila.protos.clientservice.RunBatchResponse
import app.cash.backfila.service.BackfilaConfig
import app.cash.backfila.service.BackfilaMetrics
import app.cash.backfila.service.SlackHelper
import app.cash.backfila.service.BackfillRunListener
import app.cash.backfila.service.persistence.BackfilaDb
import app.cash.backfila.service.persistence.BackfillState
import app.cash.backfila.service.persistence.DbBackfillRun
Expand Down Expand Up @@ -281,7 +281,9 @@ class BackfillRunner private constructor(
"Paused backfill ${logLabel()} due to too many consecutive failures: $failuresSinceSuccess"
}

factory.slackHelper.runErrored(backfillRunId)
for (listener in factory.backfillRunListeners) {
listener.runErrored(backfillRunId)
}

recordErrorEvent(exception, action, elapsed, backoffMs = null, paused = true)
}
Expand Down Expand Up @@ -410,7 +412,7 @@ class BackfillRunner private constructor(
val clock: Clock,
val queryFactory: Query.Factory,
val connectorProvider: ConnectorProvider,
val slackHelper: SlackHelper,
val backfillRunListeners: Set<BackfillRunListener>,
val loggingSetupProvider: BackfillRunnerLoggingSetupProvider,
val metrics: BackfilaMetrics,
val backfilaConfig: BackfilaConfig,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,9 @@ class BatchAwaiter(
}

if (runComplete) {
backfillRunner.factory.slackHelper.runCompleted(backfillRunner.backfillRunId)
for (listener in backfillRunner.factory.backfillRunListeners) {
listener.runCompleted(backfillRunner.backfillRunId)
}
}
}

Expand Down

0 comments on commit 9d68d55

Please sign in to comment.