wsp-ms-core 1.0.85 → 1.0.87-beta-1.0

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.
package/dist/index.d.mts CHANGED
@@ -645,9 +645,15 @@ declare class DefaultMysqlInboxRunner {
645
645
  private readonly eventManager;
646
646
  private readonly interval;
647
647
  private readonly maxEvents;
648
+ private topics;
648
649
  private errors;
649
650
  constructor(uowFactory: BasicUnitOfWorkFactory, eventManager: EventManager);
651
+ private addError;
652
+ private logErrors;
650
653
  private saveEvent;
654
+ subscribeTo(topic: string): void;
655
+ process(): Promise<void>;
656
+ start(): Promise<void>;
651
657
  }
652
658
 
653
659
  interface ExchangeRatesProps {
package/dist/index.d.ts CHANGED
@@ -645,9 +645,15 @@ declare class DefaultMysqlInboxRunner {
645
645
  private readonly eventManager;
646
646
  private readonly interval;
647
647
  private readonly maxEvents;
648
+ private topics;
648
649
  private errors;
649
650
  constructor(uowFactory: BasicUnitOfWorkFactory, eventManager: EventManager);
651
+ private addError;
652
+ private logErrors;
650
653
  private saveEvent;
654
+ subscribeTo(topic: string): void;
655
+ process(): Promise<void>;
656
+ start(): Promise<void>;
651
657
  }
652
658
 
653
659
  interface ExchangeRatesProps {
package/dist/index.js CHANGED
@@ -1739,7 +1739,6 @@ var DefaultMysqlOutboxRunner = class {
1739
1739
  async sleep() {
1740
1740
  return new Promise((r) => setTimeout(r, this.maxEvents));
1741
1741
  }
1742
- // HACER QUE PUEDA MANDAR UN OBJETO NO UN STRING POR KAFKA
1743
1742
  async processOutboxRecord(e, eventBusRepository) {
1744
1743
  try {
1745
1744
  e.markProcessing();
@@ -1783,15 +1782,76 @@ var DefaultMysqlOutboxRunner = class {
1783
1782
  }
1784
1783
  };
1785
1784
 
1785
+ // src/infrastructure/inbox/InboxMysqlRepository.ts
1786
+ var InboxMysqlRepository = class {
1787
+ constructor(connection) {
1788
+ this.connection = connection;
1789
+ }
1790
+ recordToRowValues(record) {
1791
+ return [
1792
+ record.eventUuid.value,
1793
+ record.tenantUuid.value,
1794
+ record.topic,
1795
+ record.producer,
1796
+ record.payload,
1797
+ record.status.value,
1798
+ record.attempts,
1799
+ record.errorMessage,
1800
+ record.lastAttempt?.value ?? null,
1801
+ record.processedAt?.value ?? null,
1802
+ record.createdAt.value
1803
+ ];
1804
+ }
1805
+ async create(record) {
1806
+ const values = this.recordToRowValues(record);
1807
+ await this.connection.query(
1808
+ `INSERT INTO events_inbox (event_uuid, tenant_uuid, topic, producer,
1809
+ payload, status, attempts, error_message, last_attempt, processed_at, created_at)
1810
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
1811
+ values
1812
+ );
1813
+ }
1814
+ async update(record) {
1815
+ const values = [record.status.value, record.attempts, record.errorMessage, record.processedAt?.value, record.lastAttempt?.value, record.eventUuid.value];
1816
+ await this.connection.query(
1817
+ `UPDATE events_inbox
1818
+ SET status = ?,
1819
+ attempts = ?,
1820
+ error_message = ?,
1821
+ processed_at = ?,
1822
+ last_attempt = ?
1823
+ WHERE event_uuid = ?`,
1824
+ values
1825
+ );
1826
+ }
1827
+ async listPending(limit) {
1828
+ const result = await this.connection.query(
1829
+ `SELECT * FROM events_inbox WHERE status IN ('PENDING','FAILED') AND events_inbox.processed_at IS NULL LIMIT ${limit}`,
1830
+ []
1831
+ );
1832
+ return result.length > 0 ? result.map((r) => InboxRecord.reconstitute(r)) : [];
1833
+ }
1834
+ };
1835
+
1786
1836
  // src/infrastructure/runners/default-mysql-inbox-runner.ts
1787
1837
  var DefaultMysqlInboxRunner = class {
1788
1838
  constructor(uowFactory, eventManager) {
1839
+ this.topics = [];
1789
1840
  this.errors = [];
1790
1841
  this.uowFactory = uowFactory;
1791
1842
  this.eventManager = eventManager;
1792
1843
  this.interval = Number(process.env.OUTBOX_RUNNER_INTERVAL_MS || 5e3);
1793
1844
  this.maxEvents = Number(process.env.OUTBOX_RUNNER_MAX_EVENTS || 20);
1794
1845
  }
1846
+ addError(error) {
1847
+ this.errors.push(error);
1848
+ }
1849
+ logErrors() {
1850
+ if (this.errors.length > 0) {
1851
+ console.error(this.errors);
1852
+ this.errors = [];
1853
+ }
1854
+ }
1795
1855
  async saveEvent(e, repository) {
1796
1856
  let record = new InboxRecord(
1797
1857
  UUID.create(),
@@ -1811,6 +1871,30 @@ var DefaultMysqlInboxRunner = class {
1811
1871
  await repository.update(record);
1812
1872
  }
1813
1873
  }
1874
+ subscribeTo(topic) {
1875
+ this.topics.push(topic);
1876
+ }
1877
+ async process() {
1878
+ const uow = await this.uowFactory.create();
1879
+ const inboxRepository = new InboxMysqlRepository(uow.connection);
1880
+ await uow.execute(async () => {
1881
+ for (let topic of this.topics) {
1882
+ try {
1883
+ this.eventManager.route(topic, async (e) => {
1884
+ await this.saveEvent(e, inboxRepository);
1885
+ });
1886
+ } catch (error) {
1887
+ this.addError(error.toString());
1888
+ }
1889
+ }
1890
+ });
1891
+ }
1892
+ async start() {
1893
+ console.log("[outbox-runner]: start");
1894
+ for (; ; ) {
1895
+ await this.process();
1896
+ }
1897
+ }
1814
1898
  };
1815
1899
 
1816
1900
  // src/utils/ExchangeRates.ts