How to spawn verticles that will be undeployed after they've done their work

145 Views Asked by At

I'm currently working on my first vert.x service and I'm not really confident with the way how my deployment is implemented.

I have several verticles to accept requests from other services and delegate a bunch of work to other verticles. How can I either spin up a verticle of each task and kill it after the work is done or programmatically scale the verticles up the number of tasks.

I'm currently spawning ten verticles at the startup and spread those tasks over the verticles using the event bus.

Main Verticle

class MainVerticle : CoroutineVerticle() {

  private val databaseConfig by lazy { config.getJsonObject("migration") }

  override suspend fun start() {
    vertx.deployVerticle(AdministrationVerticle::class.java, DeploymentOptions().setConfig(config))
    vertx.deployVerticle(UpdateVerticle::class.java, DeploymentOptions().setConfig(config))
  }
}

Administration Verticle - HTTP Handler

private suspend fun handleRolloutRequest(ctx: RoutingContext) {
  val version = ctx.request().getParam("version")?.toInt() ?: findLatestVersion()
  val systems = findOutdatedSystems(version)

  for (system in systems) {
    vertx.eventBus().send("rollout", system.id, options)
  }

  ctx.response().end()
}

Update Verticle

override suspend fun start() {
  client.connectAwait(mqttServerConfig.getInteger("port"), mqttServerConfig.getString("hostname"))

  vertx.eventBus().consumer<String>("rollout") {
    launch(vertx.dispatcher()) {
      // actual rollout
    }
  }
}
1

There are 1 best solutions below

0
Alexey Soshin On BEST ANSWER

deployVerticle takes a callback as it's last argument. Result of this callback is deploymentId:

vertx.deployVerticle("your.class", res -> {
    if (res.succeeded()) {
        String deploymentID = res.result();
        ...
    }
}

Theoretically, you could use this to undeploy your verticles after they completed their tasks.

Practically, I would keep a constant number of them, and scale on the container level instead.