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.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,15 +1855,76 @@ 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
1911
  constructor(uowFactory, eventManager) {
1912
+ this.topics = [];
1862
1913
  this.errors = [];
1863
1914
  this.uowFactory = uowFactory;
1864
1915
  this.eventManager = eventManager;
1865
1916
  this.interval = Number(process.env.OUTBOX_RUNNER_INTERVAL_MS || 5e3);
1866
1917
  this.maxEvents = Number(process.env.OUTBOX_RUNNER_MAX_EVENTS || 20);
1867
1918
  }
1919
+ addError(error) {
1920
+ this.errors.push(error);
1921
+ }
1922
+ logErrors() {
1923
+ if (this.errors.length > 0) {
1924
+ console.error(this.errors);
1925
+ this.errors = [];
1926
+ }
1927
+ }
1868
1928
  async saveEvent(e, repository) {
1869
1929
  let record = new InboxRecord(
1870
1930
  UUID.create(),
@@ -1884,6 +1944,30 @@ var DefaultMysqlInboxRunner = class {
1884
1944
  await repository.update(record);
1885
1945
  }
1886
1946
  }
1947
+ subscribeTo(topic) {
1948
+ this.topics.push(topic);
1949
+ }
1950
+ async process() {
1951
+ const uow = await this.uowFactory.create();
1952
+ const inboxRepository = new InboxMysqlRepository(uow.connection);
1953
+ await uow.execute(async () => {
1954
+ for (let topic of this.topics) {
1955
+ try {
1956
+ this.eventManager.route(topic, async (e) => {
1957
+ await this.saveEvent(e, inboxRepository);
1958
+ });
1959
+ } catch (error) {
1960
+ this.addError(error.toString());
1961
+ }
1962
+ }
1963
+ });
1964
+ }
1965
+ async start() {
1966
+ console.log("[outbox-runner]: start");
1967
+ for (; ; ) {
1968
+ await this.process();
1969
+ }
1970
+ }
1887
1971
  };
1888
1972
 
1889
1973
  // src/utils/ExchangeRates.ts