Detect and override hooks of the same kind (#6842)

This change ensures that we can have at most one hook of the same action during shutdown.
Verified the change on a real project.

Closes #6767.
This commit is contained in:
Hubert Plociniczak 2023-05-29 09:25:30 +02:00 committed by GitHub
parent 6b7cf8e705
commit 86432b5ca5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 2 deletions

View File

@ -36,8 +36,9 @@ class ShutdownHookActivator[F[+_, +_]: Exec: CovariantFlatMap]
scheduled: List[UUID] = Nil
): Receive = {
case RegisterShutdownHook(projectId, hook) =>
val realHook = hook.asInstanceOf[ShutdownHook[F]]
val updated = hooks.updated(projectId, realHook :: hooks(projectId))
val realHook = hook.asInstanceOf[ShutdownHook[F]]
val uniqueHooks = hooks(projectId).filter(!_.isSameKind(realHook))
val updated = hooks.updated(projectId, realHook :: uniqueHooks)
context.become(running(updated, scheduled))
case ProjectClosed(projectId) =>

View File

@ -9,4 +9,7 @@ trait ShutdownHook[F[+_, +_]] {
*/
def execute(): F[Nothing, Unit]
/** Checks if the provided `hook`` refers to the same kind of action as `this`` */
def isSameKind(hook: ShutdownHook[F]): Boolean
}

View File

@ -47,4 +47,17 @@ class MoveProjectDirCmd[F[+_, +_]: CovariantFlatMap: ErrorChannel](
)
}
/** Returns the project ID to which this hook refers to */
def getProjectId(): UUID =
projectId
/** @inheritdoc */
override def isSameKind(hook: ShutdownHook[F]): Boolean = {
hook match {
case cmd: MoveProjectDirCmd[_] =>
projectId == cmd.getProjectId()
case _ =>
false
}
}
}