@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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@monque/core",
3
- "version": "1.5.1",
3
+ "version": "1.5.2",
4
4
  "description": "MongoDB-backed job scheduler with atomic locking, exponential backoff, and cron scheduling",
5
5
  "author": "Maurice de Bruyn <debruyn.maurice@gmail.com>",
6
6
  "repository": {
@@ -82,7 +82,7 @@
82
82
  "@vitest/coverage-v8": "^4.0.18",
83
83
  "fishery": "^2.4.0",
84
84
  "mongodb": "^7.1.0",
85
- "tsdown": "^0.21.0",
85
+ "tsdown": "^0.21.1",
86
86
  "vitest": "^4.0.18"
87
87
  }
88
88
  }
@@ -56,6 +56,8 @@ export class ChangeStreamHandler {
56
56
  return;
57
57
  }
58
58
 
59
+ this.clearReconnectTimer();
60
+
59
61
  try {
60
62
  // Create change stream with pipeline to filter relevant events
61
63
  const pipeline = [
@@ -160,15 +162,8 @@ export class ChangeStreamHandler {
160
162
  // Fall back to polling-only mode
161
163
  this.usingChangeStreams = false;
162
164
 
163
- if (this.reconnectTimer) {
164
- clearTimeout(this.reconnectTimer);
165
- this.reconnectTimer = null;
166
- }
167
-
168
- if (this.changeStream) {
169
- this.changeStream.close().catch(() => {});
170
- this.changeStream = null;
171
- }
165
+ this.clearReconnectTimer();
166
+ this.closeChangeStream();
172
167
 
173
168
  this.ctx.emit('changestream:fallback', {
174
169
  reason: `Exhausted ${this.maxReconnectAttempts} reconnection attempts: ${error.message}`,
@@ -181,23 +176,50 @@ export class ChangeStreamHandler {
181
176
  const delay = 2 ** (this.reconnectAttempts - 1) * 1000;
182
177
 
183
178
  // Clear any existing reconnect timer before scheduling a new one
184
- if (this.reconnectTimer) {
185
- clearTimeout(this.reconnectTimer);
179
+ this.clearReconnectTimer();
180
+
181
+ if (!this.ctx.isRunning()) {
182
+ return;
186
183
  }
187
184
 
188
185
  this.reconnectTimer = setTimeout(() => {
189
- this.reconnectTimer = null;
190
- if (this.ctx.isRunning()) {
191
- // Close existing change stream before reconnecting
192
- if (this.changeStream) {
193
- this.changeStream.close().catch(() => {});
194
- this.changeStream = null;
195
- }
196
- this.setup();
197
- }
186
+ this.clearReconnectTimer();
187
+ this.reconnect();
198
188
  }, delay);
199
189
  }
200
190
 
191
+ private reconnect(): void {
192
+ if (!this.ctx.isRunning()) {
193
+ return;
194
+ }
195
+
196
+ this.closeChangeStream();
197
+
198
+ if (!this.ctx.isRunning()) {
199
+ return;
200
+ }
201
+
202
+ this.setup();
203
+ }
204
+
205
+ private clearReconnectTimer(): void {
206
+ if (!this.reconnectTimer) {
207
+ return;
208
+ }
209
+
210
+ clearTimeout(this.reconnectTimer);
211
+ this.reconnectTimer = null;
212
+ }
213
+
214
+ private closeChangeStream(): void {
215
+ if (!this.changeStream) {
216
+ return;
217
+ }
218
+
219
+ this.changeStream.close().catch(() => {});
220
+ this.changeStream = null;
221
+ }
222
+
201
223
  /**
202
224
  * Close the change stream cursor and emit closed event.
203
225
  */
@@ -209,10 +231,7 @@ export class ChangeStreamHandler {
209
231
  }
210
232
 
211
233
  // Clear reconnection timer
212
- if (this.reconnectTimer) {
213
- clearTimeout(this.reconnectTimer);
214
- this.reconnectTimer = null;
215
- }
234
+ this.clearReconnectTimer();
216
235
 
217
236
  if (this.changeStream) {
218
237
  try {