wsp-ms-core 1.0.87 → 1.0.89

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.cjs CHANGED
@@ -1812,7 +1812,6 @@ var DefaultMysqlOutboxRunner = class {
1812
1812
  async sleep() {
1813
1813
  return new Promise((r) => setTimeout(r, this.maxEvents));
1814
1814
  }
1815
- // HACER QUE PUEDA MANDAR UN OBJETO NO UN STRING POR KAFKA
1816
1815
  async processOutboxRecord(e, eventBusRepository) {
1817
1816
  try {
1818
1817
  e.markProcessing();
@@ -1856,14 +1855,63 @@ var DefaultMysqlOutboxRunner = class {
1856
1855
  }
1857
1856
  };
1858
1857
 
1858
+ // src/infrastructure/inbox/InboxMysqlRepository.ts
1859
+ var InboxMysqlRepository = class {
1860
+ constructor(connection) {
1861
+ this.connection = connection;
1862
+ }
1863
+ recordToRowValues(record) {
1864
+ return [
1865
+ record.eventUuid.value,
1866
+ record.tenantUuid.value,
1867
+ record.topic,
1868
+ record.producer,
1869
+ record.payload,
1870
+ record.status.value,
1871
+ record.attempts,
1872
+ record.errorMessage,
1873
+ record.lastAttempt?.value ?? null,
1874
+ record.processedAt?.value ?? null,
1875
+ record.createdAt.value
1876
+ ];
1877
+ }
1878
+ async create(record) {
1879
+ const values = this.recordToRowValues(record);
1880
+ await this.connection.query(
1881
+ `INSERT INTO events_inbox (event_uuid, tenant_uuid, topic, producer,
1882
+ payload, status, attempts, error_message, last_attempt, processed_at, created_at)
1883
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
1884
+ values
1885
+ );
1886
+ }
1887
+ async update(record) {
1888
+ const values = [record.status.value, record.attempts, record.errorMessage, record.processedAt?.value, record.lastAttempt?.value, record.eventUuid.value];
1889
+ await this.connection.query(
1890
+ `UPDATE events_inbox
1891
+ SET status = ?,
1892
+ attempts = ?,
1893
+ error_message = ?,
1894
+ processed_at = ?,
1895
+ last_attempt = ?
1896
+ WHERE event_uuid = ?`,
1897
+ values
1898
+ );
1899
+ }
1900
+ async listPending(limit) {
1901
+ const result = await this.connection.query(
1902
+ `SELECT * FROM events_inbox WHERE status IN ('PENDING','FAILED') AND events_inbox.processed_at IS NULL LIMIT ${limit}`,
1903
+ []
1904
+ );
1905
+ return result.length > 0 ? result.map((r) => InboxRecord.reconstitute(r)) : [];
1906
+ }
1907
+ };
1908
+
1859
1909
  // src/infrastructure/runners/default-mysql-inbox-runner.ts
1860
1910
  var DefaultMysqlInboxRunner = class {
1861
- constructor(uowFactory, eventManager) {
1862
- this.errors = [];
1863
- this.uowFactory = uowFactory;
1911
+ constructor(databaseConnector, eventManager) {
1912
+ this.topics = [];
1913
+ this.databaseConnector = databaseConnector;
1864
1914
  this.eventManager = eventManager;
1865
- this.interval = Number(process.env.OUTBOX_RUNNER_INTERVAL_MS || 5e3);
1866
- this.maxEvents = Number(process.env.OUTBOX_RUNNER_MAX_EVENTS || 20);
1867
1915
  }
1868
1916
  async saveEvent(e, repository) {
1869
1917
  let record = new InboxRecord(
@@ -1884,6 +1932,31 @@ var DefaultMysqlInboxRunner = class {
1884
1932
  await repository.update(record);
1885
1933
  }
1886
1934
  }
1935
+ subscribeTo(topic) {
1936
+ this.topics.push(topic);
1937
+ }
1938
+ async process() {
1939
+ for (let topic of this.topics) {
1940
+ try {
1941
+ this.eventManager.onSubscribe((data) => {
1942
+ console.log(data);
1943
+ });
1944
+ this.eventManager.onMessage((data) => {
1945
+ console.log(data);
1946
+ });
1947
+ this.eventManager.route(topic, async (e) => {
1948
+ await this.saveEvent(e, new InboxMysqlRepository(await this.databaseConnector.getConnection()));
1949
+ });
1950
+ } catch (error) {
1951
+ console.log(error.toString());
1952
+ }
1953
+ }
1954
+ await this.eventManager.start();
1955
+ }
1956
+ async start() {
1957
+ console.log("[inbox-runner]: start");
1958
+ await this.process();
1959
+ }
1887
1960
  };
1888
1961
 
1889
1962
  // src/utils/ExchangeRates.ts