@stemy/backend 6.1.3 → 6.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1449,6 +1449,9 @@ let JobManager = class JobManager {
1449
1449
  workerPull;
1450
1450
  zmqStats = {
1451
1451
  apiPush: {
1452
+ queued: 0,
1453
+ pending: 0,
1454
+ failed: 0,
1452
1455
  sent: 0,
1453
1456
  received: 0,
1454
1457
  lastSentAt: 0,
@@ -1461,6 +1464,9 @@ let JobManager = class JobManager {
1461
1464
  lastReceivedAt: 0
1462
1465
  },
1463
1466
  workerPush: {
1467
+ queued: 0,
1468
+ pending: 0,
1469
+ failed: 0,
1464
1470
  sent: 0,
1465
1471
  received: 0,
1466
1472
  lastSentAt: 0,
@@ -1486,16 +1492,13 @@ let JobManager = class JobManager {
1486
1492
  const job = this.resolveJobInstance(jobType, jobParams, uniqueId);
1487
1493
  const messageBridge = {
1488
1494
  sendMessage: (message, params) => {
1489
- params.uniqueId = uniqueId;
1490
- this.workerPush.then(sock => {
1491
- try {
1492
- sock.send([message, JSON.stringify(params)]);
1493
- this.zmqStats.workerPush.sent++;
1494
- this.zmqStats.workerPush.lastSentAt = Date.now();
1495
- }
1496
- catch (e) {
1497
- this.logger.log("job-manager", `ZMQ workerPush send failed: ${e.message}`);
1498
- }
1495
+ Promise.resolve().then(() => {
1496
+ return this.sendSocketMessage("workerPush", [
1497
+ message,
1498
+ JSON.stringify({ ...params, uniqueId })
1499
+ ]);
1500
+ }).catch(e => {
1501
+ this.logger.log("job-manager", `ZMQ workerPush send failed: ${e.message}`);
1499
1502
  });
1500
1503
  }
1501
1504
  };
@@ -1556,15 +1559,8 @@ let JobManager = class JobManager {
1556
1559
  const host = this.config.resolve("zmqRemoteHost");
1557
1560
  const pushHost = `${host}:${this.config.resolve("zmqBackPort")}`;
1558
1561
  const pullHost = `${host}:${this.config.resolve("zmqPort")}`;
1559
- this.workerPush = this.workerPush || new Promise(resolve => {
1560
- const sock = socket("push");
1561
- sock.connect(pushHost);
1562
- this.logger.log("job-manager", `Worker producer connected to: ${pushHost}`);
1563
- resolve(sock);
1564
- });
1565
- this.workerPull = this.workerPull || new Promise(resolve => {
1566
- const sock = socket("pull");
1567
- sock.connect(pullHost);
1562
+ this.workerPush = this.workerPush || this.connectSocket("workerPush", "push", pushHost);
1563
+ this.workerPull = this.workerPull || this.connectSocket("workerPull", "pull", pullHost).then(sock => {
1568
1564
  sock.on("message", async (name, args, uniqId) => {
1569
1565
  this.zmqStats.workerPull.received++;
1570
1566
  this.zmqStats.workerPull.lastReceivedAt = Date.now();
@@ -1587,13 +1583,15 @@ let JobManager = class JobManager {
1587
1583
  this.logger.log("job-manager", `Failed to start job: ${e.message}`);
1588
1584
  }
1589
1585
  });
1590
- this.logger.log("job-manager", `Worker consumer connected to: ${pullHost}`);
1591
- resolve(sock);
1586
+ return sock;
1592
1587
  });
1593
- await Promise.allSettled([
1594
- this.workerPush,
1595
- this.workerPull,
1596
- ]);
1588
+ try {
1589
+ await Promise.all([this.workerPush, this.workerPull]);
1590
+ }
1591
+ catch (e) {
1592
+ this.logger.log("job-manager", `ZMQ worker initialization failed: ${e.message}`);
1593
+ throw e;
1594
+ }
1597
1595
  }
1598
1596
  tryResolve(jobType, params) {
1599
1597
  const jobName = getConstructorName(jobType);
@@ -1618,35 +1616,98 @@ let JobManager = class JobManager {
1618
1616
  return this.tryResolveAndInit(jobType, params);
1619
1617
  }
1620
1618
  tryResolveAndInit(jobType, params) {
1621
- this.apiPush = this.apiPush || new Promise(resolve => {
1622
- const port = this.config.resolve("zmqPort");
1623
- const sock = socket("push");
1624
- sock.bind(`tcp://0.0.0.0:${port}`, () => {
1625
- this.logger.log("job-manager", `API producer bound to port: ${port}`);
1626
- resolve(sock);
1619
+ const jobName = this.tryResolve(jobType, params);
1620
+ this.apiPush = this.apiPush || this.bindSocket("apiPush", "push", `tcp://0.0.0.0:${this.config.resolve("zmqPort")}`);
1621
+ this.apiPull = this.apiPull || this.bindSocket("apiPull", "pull", `tcp://0.0.0.0:${this.config.resolve("zmqBackPort")}`).then(sock => {
1622
+ sock.on("message", (name, args) => {
1623
+ this.zmqStats.apiPull.received++;
1624
+ this.zmqStats.apiPull.lastReceivedAt = Date.now();
1625
+ const message = name.toString("utf8");
1626
+ const params = JSON.parse(args?.toString("utf8") || "{}");
1627
+ const paramTypes = Object.keys(params).reduce((res, key) => {
1628
+ res[key] = getType(params[key]);
1629
+ return res;
1630
+ }, {});
1631
+ this.logger.log("job-manager", `Received a message from worker: "${colorize(message, ConsoleColor.FgCyan)}" with args: ${jsonHighlight(paramTypes)}\n\n`);
1632
+ this.messages.next({ message, params });
1627
1633
  });
1634
+ return sock;
1628
1635
  });
1629
- this.apiPull = this.apiPull || new Promise(resolve => {
1630
- const backPort = this.config.resolve("zmqBackPort");
1631
- const sock = socket("pull");
1632
- sock.bind(`tcp://0.0.0.0:${backPort}`, () => {
1633
- sock.on("message", (name, args) => {
1634
- this.zmqStats.apiPull.received++;
1635
- this.zmqStats.apiPull.lastReceivedAt = Date.now();
1636
- const message = name.toString("utf8");
1637
- const params = JSON.parse(args?.toString("utf8") || "{}");
1638
- const paramTypes = Object.keys(params).reduce((res, key) => {
1639
- res[key] = getType(params[key]);
1640
- return res;
1641
- }, {});
1642
- this.logger.log("job-manager", `Received a message from worker: "${colorize(message, ConsoleColor.FgCyan)}" with args: ${jsonHighlight(paramTypes)}\n\n`);
1643
- this.messages.next({ message, params });
1644
- });
1645
- this.logger.log("job-manager", `API consumer bound to port: ${backPort}`);
1636
+ return jobName;
1637
+ }
1638
+ createSocket(name, type) {
1639
+ const sock = socket(type);
1640
+ sock.setsockopt("heartbeat_ivl", 5_000);
1641
+ sock.setsockopt("heartbeat_timeout", 15_000);
1642
+ sock.setsockopt("heartbeat_ttl", 15_000);
1643
+ sock.on("error", (error) => {
1644
+ this.logger.log("job-manager", `ZMQ ${name} error: ${error.message}`);
1645
+ });
1646
+ [
1647
+ "connect", "connect_delay", "connect_retry", "listen", "bind_error",
1648
+ "accept", "accept_error", "close", "close_error", "disconnect"
1649
+ ].forEach(event => {
1650
+ sock.on(event, (value, address, error) => {
1651
+ this.logger.log("job-manager", `ZMQ ${name} ${event}: ${address}` +
1652
+ (value === undefined ? "" : ` (value=${value})`) +
1653
+ (error ? `: ${error.message}` : ""));
1654
+ });
1655
+ });
1656
+ sock.monitor();
1657
+ return sock;
1658
+ }
1659
+ bindSocket(name, type, address) {
1660
+ return new Promise((resolve, reject) => {
1661
+ const sock = this.createSocket(name, type);
1662
+ sock.bind(address, (error) => {
1663
+ if (error) {
1664
+ this.logger.log("job-manager", `ZMQ ${name} bind failed at ${address}: ${error.message}`);
1665
+ sock.close();
1666
+ reject(error);
1667
+ return;
1668
+ }
1669
+ this.logger.log("job-manager", `ZMQ ${name} bound to: ${address}`);
1646
1670
  resolve(sock);
1647
1671
  });
1648
1672
  });
1649
- return this.tryResolve(jobType, params);
1673
+ }
1674
+ async connectSocket(name, type, address) {
1675
+ const sock = this.createSocket(name, type);
1676
+ try {
1677
+ sock.connect(address);
1678
+ this.logger.log("job-manager", `ZMQ ${name} connecting to: ${address}`);
1679
+ return sock;
1680
+ }
1681
+ catch (e) {
1682
+ sock.close();
1683
+ throw e;
1684
+ }
1685
+ }
1686
+ async sendSocketMessage(name, frames) {
1687
+ const stats = this.zmqStats[name];
1688
+ stats.queued++;
1689
+ stats.pending++;
1690
+ try {
1691
+ const sock = await this[name];
1692
+ await new Promise((resolve, reject) => {
1693
+ sock.send(frames, 0, (error) => {
1694
+ if (error) {
1695
+ reject(error);
1696
+ return;
1697
+ }
1698
+ resolve();
1699
+ });
1700
+ });
1701
+ stats.sent++;
1702
+ stats.lastSentAt = Date.now();
1703
+ }
1704
+ catch (e) {
1705
+ stats.failed++;
1706
+ throw e;
1707
+ }
1708
+ finally {
1709
+ stats.pending--;
1710
+ }
1650
1711
  }
1651
1712
  resolveJobInstance(jobType, params, uniqueId = "") {
1652
1713
  const container = this.container.createChildContainer();
@@ -1659,11 +1720,9 @@ let JobManager = class JobManager {
1659
1720
  }
1660
1721
  async sendToWorkers(jobName, params) {
1661
1722
  const uniqueId = new ObjectId$1().toHexString();
1662
- const sock = await this.apiPush;
1663
1723
  try {
1664
- sock.send([jobName, JSON.stringify(params), uniqueId]);
1665
- this.zmqStats.apiPush.sent++;
1666
- this.zmqStats.apiPush.lastSentAt = Date.now();
1724
+ await Promise.all([this.apiPush, this.apiPull]);
1725
+ await this.sendSocketMessage("apiPush", [jobName, JSON.stringify(params), uniqueId]);
1667
1726
  }
1668
1727
  catch (e) {
1669
1728
  this.logger.log("job-manager", `ZMQ API PUSH send failed: ${e.message}`);
@@ -1682,9 +1741,9 @@ let JobManager = class JobManager {
1682
1741
  };
1683
1742
  this.logger.log("job-manager", [
1684
1743
  "ZMQ diagnostics:",
1685
- `apiPush(sent=${this.zmqStats.apiPush.sent}, lastSent=${age(this.zmqStats.apiPush.lastSentAt)})`,
1744
+ `apiPush(queued=${this.zmqStats.apiPush.queued}, pending=${this.zmqStats.apiPush.pending}, failed=${this.zmqStats.apiPush.failed}, sentToZmq=${this.zmqStats.apiPush.sent}, lastSent=${age(this.zmqStats.apiPush.lastSentAt)})`,
1686
1745
  `apiPull(received=${this.zmqStats.apiPull.received}, lastReceived=${age(this.zmqStats.apiPull.lastReceivedAt)})`,
1687
- `workerPush(sent=${this.zmqStats.workerPush.sent}, lastSent=${age(this.zmqStats.workerPush.lastSentAt)})`,
1746
+ `workerPush(queued=${this.zmqStats.workerPush.queued}, pending=${this.zmqStats.workerPush.pending}, failed=${this.zmqStats.workerPush.failed}, sentToZmq=${this.zmqStats.workerPush.sent}, lastSent=${age(this.zmqStats.workerPush.lastSentAt)})`,
1688
1747
  `workerPull(received=${this.zmqStats.workerPull.received}, lastReceived=${age(this.zmqStats.workerPull.lastReceivedAt)})`
1689
1748
  ].join(" | "));
1690
1749
  }, 30_000);