Skip to content

Commit

Permalink
Clarify method in ScopeInitializer (#4640)
Browse files Browse the repository at this point in the history
  • Loading branch information
olivergrabinski authored Jan 11, 2024
1 parent 161273a commit 6f03f6d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import ch.epfl.bluebrain.nexus.delta.rdf.Vocabulary.contexts
import ch.epfl.bluebrain.nexus.delta.rdf.jsonld.context.{ContextValue, RemoteContextResolution}
import ch.epfl.bluebrain.nexus.delta.rdf.jsonld.encoder.JsonLdEncoder
import ch.epfl.bluebrain.nexus.delta.rdf.utils.JsonKeyOrdering
import ch.epfl.bluebrain.nexus.delta.routes.SupervisionRoutes.{allProjectsAreHealthy, healingSuccessful, unhealthyProjectsEncoder, SupervisionBundle}
import ch.epfl.bluebrain.nexus.delta.routes.SupervisionRoutes._
import ch.epfl.bluebrain.nexus.delta.sdk.acls.AclCheck
import ch.epfl.bluebrain.nexus.delta.sdk.acls.model.AclAddress
import ch.epfl.bluebrain.nexus.delta.sdk.directives.DeltaDirectives.emit
Expand Down Expand Up @@ -62,7 +62,7 @@ class SupervisionRoutes(
emit(
projectHealer
.heal(project)
.map(_ => healingSuccessful(project))
.map(_ => healingSuccessfulResponse(project))
.attemptNarrow[ProjectRejection]
)
}
Expand Down Expand Up @@ -91,7 +91,7 @@ object SupervisionRoutes {
Json.obj("status" := "Some projects are unhealthy.", "unhealthyProjects" := set)
}

private def healingSuccessful(project: ProjectRef) =
private def healingSuccessfulResponse(project: ProjectRef) =
Json.obj("message" := s"Project '$project' has been healed.")

}
Original file line number Diff line number Diff line change
Expand Up @@ -44,36 +44,46 @@ object ScopeInitializer {
override def initializeOrganization(
organizationResource: OrganizationResource
)(implicit caller: Subject): IO[Unit] =
scopeInitializations.toList
.parFoldMapA { init =>
init.onOrganizationCreation(organizationResource.value, caller).attempt
scopeInitializations
.runAll { init =>
init.onOrganizationCreation(organizationResource.value, caller)
}
.flatMap(IO.fromEither)
.adaptError { case e: ScopeInitializationFailed =>
OrganizationInitializationFailed(e)
}

override def initializeProject(
project: ProjectRef
)(implicit caller: Subject): IO[Unit] = {
scopeInitializations.toList
.parFoldMapA { init =>
scopeInitializations
.runAll { init =>
init
.onProjectCreation(project, caller)
.onError {
case e: ScopeInitializationFailed => errorStore.save(init.entityType, project, e)
case _ => IO.unit
}
.attempt
}
.flatMap(IO.fromEither)
.adaptError { case e: ScopeInitializationFailed =>
ProjectInitializationFailed(e)
}
}

}

implicit private class SetOps[A](set: Set[A]) {

/**
* Runs all the provided effects in parallel, but does not cancel the rest if one fails. In this implementation,
* the error being raised is the first one that occurs. This is needed because if we use parTraverse, the rest of
* the effects will be canceled if one fails.
* @param f
* effect to run on each element of the set
*/
def runAll(f: A => IO[Unit]): IO[Unit] =
set.toList.parFoldMapA(f(_).attempt).flatMap(IO.fromEither)
}

/** A constructor for tests that does not store initialization errors */
def withoutErrorStore(
scopeInitializations: Set[ScopeInitialization]
Expand Down

0 comments on commit 6f03f6d

Please sign in to comment.