<commit_before>package org.folio.okapi.deployment;

import com.codahale.metrics.Timer;
import io.vertx.core.Handler;
import io.vertx.core.Vertx;
import io.vertx.core.logging.Logger;
import io.vertx.core.logging.LoggerFactory;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.folio.okapi.bean.DeploymentDescriptor;
import org.folio.okapi.bean.EnvEntry;
import org.folio.okapi.bean.NodeDescriptor;
import org.folio.okapi.util.ModuleHandle;
import org.folio.okapi.bean.Ports;
import org.folio.okapi.bean.LaunchDescriptor;
import org.folio.okapi.discovery.DiscoveryManager;
import org.folio.okapi.util.DropwizardHelper;
import org.folio.okapi.util.ModuleHandleFactory;
import static org.folio.okapi.common.ErrorType.*;
import org.folio.okapi.common.ExtendedAsyncResult;
import org.folio.okapi.common.Failure;
import org.folio.okapi.common.Success;
import org.folio.okapi.env.EnvManager;

/**
 * Manages deployment of modules. This actually spawns processes and allocates
 * ports for modules that are to be run.
 */
public class DeploymentManager {

  private final Logger logger = LoggerFactory.getLogger("okapi");
  private final LinkedHashMap<String, DeploymentDescriptor> list = new LinkedHashMap<>();
  private final Vertx vertx;
  private final Ports ports;
  private final String host;
  private final DiscoveryManager dm;
  private final EnvManager em;
  private final int listenPort;
  private final String nodeName;

  public DeploymentManager(Vertx vertx, DiscoveryManager dm, EnvManager em,
    String host, Ports ports, int listenPort, String nodeName) {
    this.dm = dm;
    this.em = em;
    this.vertx = vertx;
    this.host = host;
    this.listenPort = listenPort;
    this.ports = ports;
    this.nodeName = nodeName;
  }

  public void init(Handler<ExtendedAsyncResult<Void>> fut) {
    NodeDescriptor nd = new NodeDescriptor();
    nd.setUrl("http://" + host + ":" + listenPort);
    nd.setNodeId(host);
    nd.setNodeName(nodeName);
    dm.addNode(nd, fut);
  }

  public void shutdown(Handler<ExtendedAsyncResult<Void>> fut) {
    shutdownR(list.keySet().iterator(), fut);
  }

  private void shutdownR(Iterator<String> it, Handler<ExtendedAsyncResult<Void>> fut) {
    if (!it.hasNext()) {
      logger.info("All modules shut down");
      fut.handle(new Success<>());
    } else {
      DeploymentDescriptor md = list.get(it.next());
      ModuleHandle mh = md.getModuleHandle();
      mh.stop(future -> shutdownR(it, fut));
    }
  }

  public void deploy(DeploymentDescriptor md1, Handler<ExtendedAsyncResult<DeploymentDescriptor>> fut) {
    String id = md1.getInstId();
    if (id != null && list.containsKey(id)) {
      fut.handle(new Failure<>(USER, "already deployed: " + id));
      return;
    }
    String srvc = md1.getSrvcId();
    if (srvc == null) {
      fut.handle(new Failure<>(USER, "Needs srvcId"));
      return;
    }
    Timer.Context tim = DropwizardHelper.getTimerContext("deploy." + srvc + ".deploy");

    int usePort = ports.get();
    if (usePort == -1) {
      fut.handle(new Failure<>(INTERNAL, "all ports in use"));
      tim.close();
      return;
    }
    String url = "http://" + host + ":" + usePort;

    if (id == null) {<commit_msg>Remove unnecessary "all modules shut" messages<commit_after>package org.folio.okapi.deployment;

import com.codahale.metrics.Timer;
import io.vertx.core.Handler;
import io.vertx.core.Vertx;
import io.vertx.core.logging.Logger;
import io.vertx.core.logging.LoggerFactory;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.folio.okapi.bean.DeploymentDescriptor;
import org.folio.okapi.bean.EnvEntry;
import org.folio.okapi.bean.NodeDescriptor;
import org.folio.okapi.util.ModuleHandle;
import org.folio.okapi.bean.Ports;
import org.folio.okapi.bean.LaunchDescriptor;
import org.folio.okapi.discovery.DiscoveryManager;
import org.folio.okapi.util.DropwizardHelper;
import org.folio.okapi.util.ModuleHandleFactory;
import static org.folio.okapi.common.ErrorType.*;
import org.folio.okapi.common.ExtendedAsyncResult;
import org.folio.okapi.common.Failure;
import org.folio.okapi.common.Success;
import org.folio.okapi.env.EnvManager;

/**
 * Manages deployment of modules. This actually spawns processes and allocates
 * ports for modules that are to be run.
 */
public class DeploymentManager {

  private final Logger logger = LoggerFactory.getLogger("okapi");
  private final LinkedHashMap<String, DeploymentDescriptor> list = new LinkedHashMap<>();
  private final Vertx vertx;
  private final Ports ports;
  private final String host;
  private final DiscoveryManager dm;
  private final EnvManager em;
  private final int listenPort;
  private final String nodeName;

  public DeploymentManager(Vertx vertx, DiscoveryManager dm, EnvManager em,
    String host, Ports ports, int listenPort, String nodeName) {
    this.dm = dm;
    this.em = em;
    this.vertx = vertx;
    this.host = host;
    this.listenPort = listenPort;
    this.ports = ports;
    this.nodeName = nodeName;
  }

  public void init(Handler<ExtendedAsyncResult<Void>> fut) {
    NodeDescriptor nd = new NodeDescriptor();
    nd.setUrl("http://" + host + ":" + listenPort);
    nd.setNodeId(host);
    nd.setNodeName(nodeName);
    dm.addNode(nd, fut);
  }

  public void shutdown(Handler<ExtendedAsyncResult<Void>> fut) {
    shutdownR(list.keySet().iterator(), 0, fut);
  }

  private void shutdownR(Iterator<String> it, int count,
    Handler<ExtendedAsyncResult<Void>> fut) {
    if (!it.hasNext()) {
      if (count != 0) {
        logger.info("All " + count + " modules shut down");
      }
      fut.handle(new Success<>());
    } else {
      DeploymentDescriptor md = list.get(it.next());
      ModuleHandle mh = md.getModuleHandle();
      logger.debug("Shutting down " + md.getSrvcId());
      mh.stop(future -> shutdownR(it, count + 1, fut));
    }
  }

  public void deploy(DeploymentDescriptor md1, Handler<ExtendedAsyncResult<DeploymentDescriptor>> fut) {
    String id = md1.getInstId();
    if (id != null && list.containsKey(id)) {
      fut.handle(new Failure<>(USER, "already deployed: " + id));
      return;
    }
    String srvc = md1.getSrvcId();
    if (srvc == null) {
      fut.handle(new Failure<>(USER, "Needs srvcId"));
      return;
    }
    Timer.Context tim = DropwizardHelper.getTimerContext("deploy." + srvc + ".deploy");

    int usePort = ports.get();
    if (usePort == -1) {
      fut.handle(new Failure<>(INTERNAL, "all ports in use"));
      tim.close();
      return;
    }
    String url = "http://" + host + ":" + usePort;

    if (id == null) {
{'fraction_non_alphanumeric': 0.12728068895051817, 'fraction_numerical': 0.0016056050211647934, 'mean_word_length': 3.8595744680851065, 'pattern_counts': {'":': 4, '<': 33, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3111800', 'n_tokens_mistral': 2199, 'n_tokens_neox': 2147, 'n_words': 511}