Backend module lifecycle

Module deployment flow

Module deployment flow
For enabling module to FOLIO system it needs next steps:

  • Register Module descriptor into OKAPI using its API
  • Deploy module by Deployment descriptor using OKAPI API
  • Enable module to particular tenant using OKAPI API

After this steps module will be ready to use for particular tenant. It is possible to enable one instance of the module to many tenants.

Adding a hooks on lifecycle phases

Before deployment hook

It is possible to add custom code that will run once before the application is deployed by implementing the InitAPIs interface. You must implement the init(Vertx vertx, Context context, Handler<AsyncResult<Boolean>> resultHandler). Only one implementation per module is supported. Currently the implementation should sit in the org.folio.rest.impl package in the implementing project. The implementation will run during verticle deployment. The verticle will not complete deployment until the init() completes. The init() function can do anything basically, but it must call back the Handler.

public class InitAPIs implements InitAPI {

public void init(Vertx vertx, Context context, Handler<AsyncResult<Boolean>> resultHandler){
try {
resultHandler.handle(io.vertx.core.Future.succeededFuture(true));
} catch (Exception e) {
e.printStackTrace();
resultHandler.handle(io.vertx.core.Future.failedFuture(e.getMessage()));
}
}
}

After deployment hook 

It is possible to add custom code that will be run immediately after the verticle running the module is deployed. It is same for InitAPI but run after RestVerticle is successfully deployed.

public class PostDeployVerticleIml implements PostDeployVerticle {

@Override
public void init(Vertx vertx, Context context, Handler<AsyncResult<Boolean>> handler) {
    try {
resultHandler.handle(io.vertx.core.Future.succeededFuture(true));
} catch (Exception e) {
e.printStackTrace();
resultHandler.handle(io.vertx.core.Future.failedFuture(e.getMessage()));
}
 }
}

TenantAPI

Enable module for tenant

After enabling module for tenant using OKAPI API by POST request to /_/tenant/{tenantId}/modules, it call module's /_/tenant endpoint. This endpoint create schema at database if it not exist and run custom SQL snippets if there are registered at schema file.

When OKAPI create POST request to module's /_/tenant endpoint, TenantAPI of module start looking for schema.json. At schema.json it is possible to define tables, views and scripts. Full description you can find here.

Example of schema.json

{
"tables": [
{
"tableName": "module_table",
 "generateId": false,
"pkColumnName": "_id",
"withMetadata": false,
"withAuditing": false,
"customSnippetPath": "CUSTOM_SQL_SNIPPET.sql"
}
],
"scripts": [
{
"run": "after",
"snippet": "SELECT * FROM module_table;"
},
{
"run": "before",
"snippet": "SELECT * FROM module_table;"
},
]
}

Module deployment lifecycle sequence diagram