wsp-ms-core 1.0.78-beta-2 → 1.0.78-beta-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.
package/dist/index.d.ts CHANGED
@@ -579,6 +579,21 @@ declare class MysqlConnection implements DatabaseConnection<string, any[], RowDa
579
579
  close(): Promise<void>;
580
580
  }
581
581
 
582
+ declare class DefaultMysqlOutboxRunner {
583
+ private readonly uowFactory;
584
+ private readonly eventManager;
585
+ private readonly interval;
586
+ private readonly maxEvents;
587
+ private errors;
588
+ constructor(uowFactory: BasicUnitOfWorkFactory, eventManager: EventManager);
589
+ private addError;
590
+ private logErrors;
591
+ private sleep;
592
+ private processOutboxRecord;
593
+ private process;
594
+ start(): Promise<void>;
595
+ }
596
+
582
597
  interface ExchangeRatesProps {
583
598
  base: Currency;
584
599
  rates: Record<string, number>;
@@ -603,4 +618,4 @@ declare class StringVars {
603
618
  }): string;
604
619
  }
605
620
 
606
- export { BaseEntity, BaseEvent, BaseObject, BasicUnitOfWork, BasicUnitOfWorkFactory, Country, Currency, DatabaseConnection, DatabaseConnector, DateTime, DomainEntity, DomainError, DomainEvent, Email, ErrorManager, ErrorManagerHandleResult, ErrorTemplate, EventBus, EventBusMysqlRepository, EventBusRepository, EventManager, EventManagerConnection, ExchangeRates, FatalError, HttpController, HttpHealthCheckController, HttpNotFoundController, HttpRequest, HttpResponse, IntegrationEvent, InternalError, KafkaManager, Language, Logger, MysqlConnection, MysqlConnector, OutboxRecord, PaymentGateway, PaymentStatus, Price, ProcessStatus, RouteCallback, RoutesCallbackList, StringVars, UUID, UnitOfWork, UploadedFile, UsageError, ValueObject, adaptExpressErrorHandler, adaptExpressRoute };
621
+ export { BaseEntity, BaseEvent, BaseObject, BasicUnitOfWork, BasicUnitOfWorkFactory, Country, Currency, DatabaseConnection, DatabaseConnector, DateTime, DefaultMysqlOutboxRunner, DomainEntity, DomainError, DomainEvent, Email, ErrorManager, ErrorManagerHandleResult, ErrorTemplate, EventBus, EventBusMysqlRepository, EventBusRepository, EventManager, EventManagerConnection, ExchangeRates, FatalError, HttpController, HttpHealthCheckController, HttpNotFoundController, HttpRequest, HttpResponse, IntegrationEvent, InternalError, KafkaManager, Language, Logger, MysqlConnection, MysqlConnector, OutboxRecord, PaymentGateway, PaymentStatus, Price, ProcessStatus, RouteCallback, RoutesCallbackList, StringVars, UUID, UnitOfWork, UploadedFile, UsageError, ValueObject, adaptExpressErrorHandler, adaptExpressRoute };
package/dist/index.js CHANGED
@@ -1524,7 +1524,7 @@ var _MysqlConnector = class _MysqlConnector {
1524
1524
  password: process.env.DB_PASSWORD,
1525
1525
  database: process.env.DB_DATABASE,
1526
1526
  dateStrings: true,
1527
- connectionLimit: Number(process.env.DB_POOL_SIZE) || _MysqlConnector.DEFAULT_POOL_SIZE,
1527
+ connectionLimit: Number(process.env.DB_POOL_SIZE ?? _MysqlConnector.DEFAULT_POOL_SIZE),
1528
1528
  decimalNumbers: true
1529
1529
  });
1530
1530
  }
@@ -1591,6 +1591,64 @@ var MysqlConnection = class {
1591
1591
  }
1592
1592
  };
1593
1593
 
1594
+ // src/infrastructure/runners/default-mysql-outbox-runner.ts
1595
+ var DefaultMysqlOutboxRunner = class {
1596
+ constructor(uowFactory, eventManager) {
1597
+ this.uowFactory = uowFactory;
1598
+ this.eventManager = eventManager;
1599
+ this.interval = Number(process.env.OUTBOX_RUNNER_INTERVAL_MS || 5e3);
1600
+ this.maxEvents = Number(process.env.OUTBOX_RUNNER_MAX_EVENTS || 20);
1601
+ }
1602
+ addError(error) {
1603
+ this.errors.push(error);
1604
+ }
1605
+ logErrors() {
1606
+ if (this.errors.length > 0) {
1607
+ console.error(this.errors);
1608
+ this.errors = [];
1609
+ }
1610
+ }
1611
+ async sleep() {
1612
+ return new Promise((r) => setTimeout(r, this.maxEvents));
1613
+ }
1614
+ async processOutboxRecord(e, eventBusRepository) {
1615
+ try {
1616
+ e.markProcessing();
1617
+ await eventBusRepository.update(e);
1618
+ await this.eventManager.send(e.topic, e.payload);
1619
+ e.markProcessed();
1620
+ } catch (error) {
1621
+ const type = String(error.type);
1622
+ e.markWithError(type);
1623
+ throw new Error(type);
1624
+ } finally {
1625
+ await eventBusRepository.update(e);
1626
+ }
1627
+ }
1628
+ async process() {
1629
+ const uow = await this.uowFactory.create();
1630
+ const eventBusRepository = new EventBusMysqlRepository(uow.connection);
1631
+ await uow.execute(async () => {
1632
+ const records = await eventBusRepository.listPending(this.maxEvents);
1633
+ for (let record of records) {
1634
+ try {
1635
+ await this.processOutboxRecord(record, eventBusRepository);
1636
+ } catch (error) {
1637
+ this.addError({ eventUuid: record.eventUuid.value, error: error.toString() });
1638
+ }
1639
+ }
1640
+ this.logErrors();
1641
+ });
1642
+ }
1643
+ async start() {
1644
+ console.log("[outbox-runner]: start");
1645
+ for (; ; ) {
1646
+ await this.process();
1647
+ await this.sleep();
1648
+ }
1649
+ }
1650
+ };
1651
+
1594
1652
  // src/utils/ExchangeRates.ts
1595
1653
  var ExchangeRates = class _ExchangeRates extends BaseObject {
1596
1654
  constructor(props) {
@@ -1650,6 +1708,7 @@ export {
1650
1708
  Country,
1651
1709
  Currency,
1652
1710
  DateTime,
1711
+ DefaultMysqlOutboxRunner,
1653
1712
  DomainEntity,
1654
1713
  DomainError,
1655
1714
  DomainEvent,