-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ feat: Implement reload endpoint. (#249)
* ✨ feat: Implement reload endpoint. * ♻️ refactor: Implement mapping clearing in more memory efficient way. * 🐛 build: Fix import error.
- Loading branch information
1 parent
2621866
commit 364d2a0
Showing
9 changed files
with
220 additions
and
28 deletions.
There are no files selected for viewing
62 changes: 62 additions & 0 deletions
62
tofhir-server/src/main/scala/io/tofhir/server/endpoint/ReloadEndpoint.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,62 @@ | ||
package io.tofhir.server.endpoint | ||
|
||
import akka.http.scaladsl.model.StatusCodes | ||
import akka.http.scaladsl.server.Directives.{complete, get, pathEndOrSingleSlash, pathPrefix} | ||
import akka.http.scaladsl.server.Route | ||
import com.typesafe.scalalogging.LazyLogging | ||
import io.tofhir.engine.Execution.actorSystem.dispatcher | ||
import io.tofhir.server.common.model.ToFhirRestCall | ||
import io.tofhir.server.endpoint.ReloadEndpoint.SEGMENT_RELOAD | ||
import io.tofhir.server.repository.job.JobFolderRepository | ||
import io.tofhir.server.repository.mapping.ProjectMappingFolderRepository | ||
import io.tofhir.server.repository.mappingContext.MappingContextFolderRepository | ||
import io.tofhir.server.repository.schema.SchemaFolderRepository | ||
import io.tofhir.server.repository.terminology.TerminologySystemFolderRepository | ||
import io.tofhir.server.service.ReloadService | ||
import io.tofhir.server.service.db.FolderDBInitializer | ||
|
||
/** | ||
* Endpoint to reload resources from the file system. | ||
* */ | ||
class ReloadEndpoint(mappingRepository: ProjectMappingFolderRepository, | ||
schemaRepository: SchemaFolderRepository, | ||
mappingJobRepository: JobFolderRepository, | ||
mappingContextRepository: MappingContextFolderRepository, | ||
terminologySystemFolderRepository: TerminologySystemFolderRepository, | ||
folderDBInitializer: FolderDBInitializer) extends LazyLogging { | ||
|
||
val reloadService: ReloadService = new ReloadService( | ||
mappingRepository, | ||
schemaRepository, | ||
mappingJobRepository, | ||
mappingContextRepository, | ||
terminologySystemFolderRepository, | ||
folderDBInitializer | ||
) | ||
|
||
def route(request: ToFhirRestCall): Route = { | ||
pathPrefix(SEGMENT_RELOAD) { | ||
pathEndOrSingleSlash { | ||
reloadResources | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Route to reload all resources | ||
* @return | ||
*/ | ||
private def reloadResources: Route = { | ||
get { | ||
complete { | ||
reloadService.reloadResources() map { _ => | ||
StatusCodes.NoContent | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
object ReloadEndpoint { | ||
val SEGMENT_RELOAD = "reload" | ||
} |
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
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
37 changes: 37 additions & 0 deletions
37
tofhir-server/src/main/scala/io/tofhir/server/service/ReloadService.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,37 @@ | ||
package io.tofhir.server.service | ||
|
||
import io.tofhir.server.repository.job.JobFolderRepository | ||
import io.tofhir.server.repository.mapping.ProjectMappingFolderRepository | ||
import io.tofhir.server.repository.mappingContext.MappingContextFolderRepository | ||
import io.tofhir.server.repository.schema.SchemaFolderRepository | ||
import io.tofhir.server.repository.terminology.TerminologySystemFolderRepository | ||
import io.tofhir.engine.Execution.actorSystem.dispatcher | ||
import io.tofhir.server.service.db.FolderDBInitializer | ||
|
||
import scala.concurrent.Future | ||
|
||
/** | ||
* Service for reloading resources from the file system. | ||
*/ | ||
class ReloadService(mappingRepository: ProjectMappingFolderRepository, | ||
schemaRepository: SchemaFolderRepository, | ||
mappingJobRepository: JobFolderRepository, | ||
mappingContextRepository: MappingContextFolderRepository, | ||
terminologySystemFolderRepository: TerminologySystemFolderRepository, | ||
folderDBInitializer: FolderDBInitializer) { | ||
|
||
/** | ||
* Reload all resources. | ||
* @return | ||
*/ | ||
def reloadResources(): Future[Unit] = { | ||
Future{ | ||
mappingRepository.reloadMappingDefinitions() | ||
schemaRepository.reloadSchemaDefinitions() | ||
mappingJobRepository.reloadJobDefinitions() | ||
mappingContextRepository.reloadMappingContextDefinitions() | ||
terminologySystemFolderRepository.reloadTerminologySystems() | ||
folderDBInitializer.init() | ||
} | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
tofhir-server/src/test/scala/io/tofhir/server/endpoint/ReloadingEndpointTest.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,47 @@ | ||
package io.tofhir.server.endpoint | ||
|
||
import akka.http.scaladsl.model.StatusCodes | ||
import io.tofhir.engine.util.FileUtils | ||
import io.tofhir.server.BaseEndpointTest | ||
import io.tofhir.server.model.Project | ||
import io.onfhir.definitions.common.model.Json4sSupport.formats | ||
import io.tofhir.server.repository.project.ProjectFolderRepository | ||
import io.tofhir.server.util.FileOperations | ||
import org.json4s.{JArray, JObject, JString} | ||
import org.json4s.jackson.{JsonMethods, Serialization} | ||
|
||
import java.io.FileWriter | ||
|
||
class ReloadingEndpointTest extends BaseEndpointTest { | ||
|
||
"The service" should { | ||
|
||
"should reload projects successfully after updating project file" in { | ||
// Read projects and update description fields from projects.json | ||
val projectsFile = FileUtils.getPath(ProjectFolderRepository.PROJECTS_JSON).toFile | ||
val parsedProjects = FileOperations.readFileIntoJson(projectsFile).asInstanceOf[JArray].arr.map(p => p.asInstanceOf[JObject]) | ||
val reloadedProjects = parsedProjects.map(project => { | ||
project.mapField { | ||
case ("description", _) => ("description", JString("reloaded")) | ||
case otherwise => otherwise | ||
} | ||
}) | ||
val fw = new FileWriter(projectsFile) | ||
try fw.write(Serialization.writePretty(reloadedProjects)) finally fw.close() | ||
|
||
// Trigger reload endpoint | ||
Get(s"/${webServerConfig.baseUri}/${ReloadEndpoint.SEGMENT_RELOAD}") ~> route ~> check { | ||
status shouldEqual StatusCodes.NoContent | ||
|
||
// Check project endpoint for whether project descriptions are updated | ||
Get(s"/${webServerConfig.baseUri}/${ProjectEndpoint.SEGMENT_PROJECTS}") ~> route ~> check { | ||
status shouldEqual StatusCodes.OK | ||
val projects: Seq[Project] = JsonMethods.parse(responseAs[String]).extract[Seq[Project]] | ||
projects.foreach(project => { | ||
project.description.get shouldEqual "reloaded" | ||
}) | ||
} | ||
} | ||
} | ||
} | ||
} |