@monque/core 1.5.1 → 1.5.2

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/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # @monque/core
2
2
 
3
+ ## 1.5.2
4
+
5
+ ### Patch Changes
6
+
7
+ - [#227](https://github.com/ueberBrot/monque/pull/227) [`e9208ca`](https://github.com/ueberBrot/monque/commit/e9208ca5c985d84d023161560d5d3ba195394fe1) Thanks [@ueberBrot](https://github.com/ueberBrot)! - Prevent change stream reconnection attempts from running after the scheduler stops. This clears pending reconnect timers during shutdown and adds coverage for the stop-during-backoff scenario.
8
+
3
9
  ## 1.5.1
4
10
 
5
11
  ### Patch Changes
package/dist/index.cjs CHANGED
@@ -755,6 +755,7 @@ var ChangeStreamHandler = class {
755
755
  */
756
756
  setup() {
757
757
  if (!this.ctx.isRunning()) return;
758
+ this.clearReconnectTimer();
758
759
  try {
759
760
  this.changeStream = this.ctx.collection.watch([{ $match: { $or: [{ operationType: "insert" }, {
760
761
  operationType: "update",
@@ -814,30 +815,35 @@ var ChangeStreamHandler = class {
814
815
  this.reconnectAttempts++;
815
816
  if (this.reconnectAttempts > this.maxReconnectAttempts) {
816
817
  this.usingChangeStreams = false;
817
- if (this.reconnectTimer) {
818
- clearTimeout(this.reconnectTimer);
819
- this.reconnectTimer = null;
820
- }
821
- if (this.changeStream) {
822
- this.changeStream.close().catch(() => {});
823
- this.changeStream = null;
824
- }
818
+ this.clearReconnectTimer();
819
+ this.closeChangeStream();
825
820
  this.ctx.emit("changestream:fallback", { reason: `Exhausted ${this.maxReconnectAttempts} reconnection attempts: ${error.message}` });
826
821
  return;
827
822
  }
828
823
  const delay = 2 ** (this.reconnectAttempts - 1) * 1e3;
829
- if (this.reconnectTimer) clearTimeout(this.reconnectTimer);
824
+ this.clearReconnectTimer();
825
+ if (!this.ctx.isRunning()) return;
830
826
  this.reconnectTimer = setTimeout(() => {
831
- this.reconnectTimer = null;
832
- if (this.ctx.isRunning()) {
833
- if (this.changeStream) {
834
- this.changeStream.close().catch(() => {});
835
- this.changeStream = null;
836
- }
837
- this.setup();
838
- }
827
+ this.clearReconnectTimer();
828
+ this.reconnect();
839
829
  }, delay);
840
830
  }
831
+ reconnect() {
832
+ if (!this.ctx.isRunning()) return;
833
+ this.closeChangeStream();
834
+ if (!this.ctx.isRunning()) return;
835
+ this.setup();
836
+ }
837
+ clearReconnectTimer() {
838
+ if (!this.reconnectTimer) return;
839
+ clearTimeout(this.reconnectTimer);
840
+ this.reconnectTimer = null;
841
+ }
842
+ closeChangeStream() {
843
+ if (!this.changeStream) return;
844
+ this.changeStream.close().catch(() => {});
845
+ this.changeStream = null;
846
+ }
841
847
  /**
842
848
  * Close the change stream cursor and emit closed event.
843
849
  */
@@ -846,10 +852,7 @@ var ChangeStreamHandler = class {
846
852
  clearTimeout(this.debounceTimer);
847
853
  this.debounceTimer = null;
848
854
  }
849
- if (this.reconnectTimer) {
850
- clearTimeout(this.reconnectTimer);
851
- this.reconnectTimer = null;
852
- }
855
+ this.clearReconnectTimer();
853
856
  if (this.changeStream) {
854
857
  try {
855
858
  await this.changeStream.close();