text: "use strict";
  /**
   * Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info.
   *
   * @author David Sehnal <david.sehnal@gmail.com>
   */
  Object.defineProperty(exports, "__esModule", { value: true });
  exports.runChild = exports.runMaster = void 0;
  var tslib_1 = require("tslib");
  var path = (0, tslib_1.__importStar)(require("path"));
  var cluster_1 = (0, tslib_1.__importDefault)(require("cluster"));
  var now_1 = require("../../../mol-util/now");
  var performance_monitor_1 = require("../../../mol-util/performance-monitor");
  var preprocess_1 = require("./preprocess");
  var property_provider_1 = require("../property-provider");
  function runMaster(config, entries) {
      var started = (0, now_1.now)();
      var progress = 0;
      var onMessage = function (msg) {
          if (msg.type === 'tick') {
              progress++;
              var elapsed = (0, now_1.now)() - started;
              console.log("[".concat(progress, "/").concat(entries.length, "] in ").concat(performance_monitor_1.PerformanceMonitor.format(elapsed), " (avg ").concat(performance_monitor_1.PerformanceMonitor.format(elapsed / progress), ")."));
          }
          else if (msg.type === 'error') {
              console.error("".concat(msg.id, ": ").concat(msg.error));
          }
      };
      if (entries.length === 1) {
          runSingle(entries[0], config, onMessage);
      }
      else {
          var parts = partitionArray(entries, config.numProcesses || 1);
          for (var _i = 0, parts_1 = parts; _i < parts_1.length; _i++) {
              var _ = parts_1[_i];
              var worker = cluster_1.default.fork();
              worker.on('message', onMessage);
          }
          var i = 0;
          for (var id in cluster_1.default.workers) {
              cluster_1.default.workers[id].send({ entries: parts[i++], config: config });
          }
      }
  }
  exports.runMaster = runMaster;
  function runChild() {
      var _this = this;
      process.on('message', function (_a) {
          var entries = _a.entries, config = _a.config;
          return (0, tslib_1.__awaiter)(_this, void 0, void 0, function () {
              var props, _i, entries_1, entry, e_1;
              return (0, tslib_1.__generator)(this, function (_b) {
                  switch (_b.label) {
                      case 0:
                          props = (0, property_provider_1.createModelPropertiesProvider)(config.customProperties);
                          _i = 0, entries_1 = entries;
                          _b.label = 1;
                      case 1:
                          if (!(_i < entries_1.length)) return [3 /*break*/, 7];
                          entry = entries_1[_i];
                          _b.label = 2;
                      case 2:
                          _b.trys.push([2, 4, , 5]);
                          return [4 /*yield*/, (0, preprocess_1.preprocessFile)(entry.source, props, entry.cif, entry.bcif)];
                      case 3:
                          _b.sent();
                          return [3 /*break*/, 5];
                      case 4:
                          e_1 = _b.sent();
                          process.send({ type: 'error', id: path.parse(entry.source).name, error: '' + e_1 });
                          return [3 /*break*/, 5];
                      case 5:
                          process.send({ type: 'tick' });
                          _b.label = 6;
                      case 6:
                          _i++;
                          return [3 /*break*/, 1];
                      case 7:
                          process.exit();
                          return [2 /*return*/];
                  }
              });
          });
      });
  }
  exports.runChild = runChild;
  function runSingle(entry, config, onMessage) {
      return (0, tslib_1.__awaiter)(this, void 0, void 0, function () {
          var props, e_2;
          return (0, tslib_1.__generator)(this, function (_a) {
              switch (_a.label) {
                  case 0:
                      props = (0, property_provider_1.createModelPropertiesProvider)(config.customProperties);
                      _a.label = 1;
                  case 1:
                      _a.trys.push([1, 3, , 4]);
                      return [4 /*yield*/, (0, preprocess_1.preprocessFile)(entry.source, props, entry.cif, entry.bcif)];
                  case 2:
                      _a.sent();
                      return [3 /*break*/, 4];
                  case 3:
                      e_2 = _a.sent();
                      onMessage({ type: 'error', id: path.parse(entry.source).name, error: '' + e_2 });
                      return [3 /*break*/, 4];
                  case 4:
                      onMessage({ type: 'tick' });
                      return [2 /*return*/];
              }
          });
      });
  }
  function partitionArray(xs, count) {
      var ret = [];
      var s = Math.ceil(xs.length / count);
      for (var i = 0; i < xs.length; i += s) {
          var bucket = [];
          for (var j = i, _j = Math.min(xs.length, i + s); j < _j; j++) {
              bucket.push(xs[j]);
          }
          ret.push(bucket);
      }
      return ret;
  }
  //# sourceMappingURL=parallel.js.map