-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a job to heal projects at startup (#5221)
* Add a job to heal projects at startup --------- Co-authored-by: Simon Dumas <[email protected]>
- Loading branch information
Showing
10 changed files
with
76 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...ta/sdk/projects/model/ProjectHealer.scala → ...us/delta/sdk/projects/ProjectHealer.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 1 addition & 2 deletions
3
...a/sdk/projects/model/ProjectsHealth.scala → ...s/delta/sdk/projects/ProjectsHealth.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
.../sdk/src/main/scala/ch/epfl/bluebrain/nexus/delta/sdk/projects/job/ProjectHealthJob.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package ch.epfl.bluebrain.nexus.delta.sdk.projects.job | ||
|
||
import cats.effect.IO | ||
import cats.effect.std.Env | ||
import ch.epfl.bluebrain.nexus.delta.kernel.Logger | ||
import ch.epfl.bluebrain.nexus.delta.sdk.projects.{ProjectHealer, Projects} | ||
import ch.epfl.bluebrain.nexus.delta.sourcing.model.ProjectRef | ||
import fs2.Stream | ||
|
||
trait ProjectHealthJob | ||
|
||
object ProjectHealthJob extends ProjectHealthJob { | ||
private val logger = Logger[ProjectHealthJob] | ||
|
||
def healTrigger: IO[Boolean] = | ||
Env[IO].get("HEAL_PROJECTS").map(_.getOrElse("false").toBoolean) | ||
|
||
private[job] def run(currentProjects: Stream[IO, ProjectRef], projectHealer: ProjectHealer): IO[Unit] = | ||
currentProjects | ||
.evalMap { projectRef => | ||
projectHealer.heal(projectRef).recoverWith { err => | ||
logger.error(err)(s"Project '$projectRef' could not be heal because of : ${err.getMessage}.") | ||
} | ||
} | ||
.compile | ||
.drain | ||
|
||
def apply(projects: Projects, projectHealer: ProjectHealer): IO[ProjectHealthJob.type] = | ||
healTrigger | ||
.flatMap { | ||
case true => { | ||
logger.info("Starting Nexus automatic project healing.") >> | ||
run(projects.currentRefs, projectHealer) >> | ||
logger.info("Nexus automatic healing has completed.") | ||
} | ||
case false => logger.info("Nexus automatic project healingi is disabled.") | ||
} | ||
.as(ProjectHealthJob) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
...src/test/scala/ch/epfl/bluebrain/nexus/delta/sdk/projects/job/ProjectHealthJobSuite.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package ch.epfl.bluebrain.nexus.delta.sdk.projects.job | ||
|
||
import cats.effect.IO | ||
import cats.effect.Ref | ||
import ch.epfl.bluebrain.nexus.delta.sdk.projects.ProjectHealer | ||
import ch.epfl.bluebrain.nexus.delta.sourcing.model.ProjectRef | ||
import ch.epfl.bluebrain.nexus.testkit.mu.NexusSuite | ||
import fs2.Stream | ||
|
||
class ProjectHealthJobSuite extends NexusSuite { | ||
|
||
test("Healing should be called for the different projects") { | ||
val projects = Set(ProjectRef.unsafe("org", "proj"), ProjectRef.unsafe("org2", "proj2")) | ||
for { | ||
ref <- Ref.of[IO, Set[ProjectRef]](Set.empty) | ||
projectHealer = new ProjectHealer { | ||
override def heal(project: ProjectRef): IO[Unit] = ref.update(_ + project) | ||
} | ||
stream = Stream.iterable(projects) | ||
_ <- ProjectHealthJob.run(stream, projectHealer) | ||
_ <- ref.get.assertEquals(projects) | ||
} yield () | ||
|
||
} | ||
|
||
} |