@jetit/publisher 1.7.0 → 1.7.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": "@jetit/publisher",
3
- "version": "1.7.0",
3
+ "version": "1.7.2",
4
4
  "type": "commonjs",
5
5
  "dependencies": {
6
6
  "@jetit/id": "0.0.11",
@@ -8,7 +8,7 @@
8
8
  "rxjs": "^7.8.0"
9
9
  },
10
10
  "peerDependencies": {
11
- "tslib": "2.5.0"
11
+ "tslib": "^2.3.0"
12
12
  },
13
13
  "main": "./src/index.js",
14
14
  "types": "./src/index.d.ts"
@@ -61,6 +61,11 @@ class ScheduledProcessor {
61
61
  const streamName = `${eventData.eventName}:${consumerGroup}`;
62
62
  transaction.xadd(streamName, '*', 'data', JSON.stringify(eventData));
63
63
  }
64
+ if (eventData.repeatInterval) {
65
+ const nextEventTime = currentTime + eventData.repeatInterval;
66
+ const nextEventString = JSON.stringify(Object.assign({}, eventData));
67
+ transaction.zadd('se', nextEventTime, nextEventString);
68
+ }
64
69
  transaction.publish(eventData.eventName, '');
65
70
  yield transaction.exec();
66
71
  }
@@ -52,6 +52,8 @@ export declare class Streams {
52
52
  *
53
53
  * @param scheduledTime - The Date object representing the future time when the event should be published.
54
54
  * @param eventData - The event data object, containing the event name and its associated data.
55
+ * @param uniquePerInstance - Have only one scheduled job per type
56
+ * @param repeatInterval - The duration for which to reschedule the job in ms
55
57
  *
56
58
  * @throws Error - Throws an error if the scheduled time is in the past.
57
59
  *
@@ -67,7 +69,7 @@ export declare class Streams {
67
69
  *
68
70
  * await streams.scheduledPublish(futureTime, eventData);
69
71
  */
70
- scheduledPublish<T, const TName extends string = string>(scheduledTime: Date, eventData: EventData<T, TName>, uniquePerInstance?: boolean): Promise<void>;
72
+ scheduledPublish<TData = unknown, TName extends string = string>(scheduledTime: Date, eventData: PublishData<TData, TName>, uniquePerInstance?: boolean, repeatInterval?: number): Promise<void>;
71
73
  /**
72
74
  * Listens for events with the given name and returns an Observable that emits an EventData<T> object
73
75
  * each time a new event is received.
@@ -116,32 +116,17 @@ class Streams {
116
116
  throw new Error('Publisher Error');
117
117
  });
118
118
  }
119
+ else
120
+ console.log(`PUBLISHER: Event publish failed for event ${data.eventName}, reason: no consumers ${consumerGroups}`);
119
121
  });
120
122
  }
121
- /**
122
- * Schedules an event to be published at a specified future time. Thee event gets published if the
123
- * differnece between the current time and the scheduled time is less than 500ms.
124
- *
125
- * @param scheduledTime - The Date object representing the future time when the event should be published.
126
- * @param eventData - The event data object, containing the event name and its associated data.
127
- *
128
- * @throws Error - Throws an error if the scheduled time is in the past.
129
- *
130
- * @example
131
- *
132
- * const streams = new Streams('app-service');
133
- *
134
- * const futureTime = new Date(Date.now() + 10000); // 10 seconds from now
135
- * const eventData: EventData<string> = {
136
- * eventName: 'order.created',
137
- * data: 'Order data'
138
- * };
139
- *
140
- * await streams.scheduledPublish(futureTime, eventData);
141
- */
142
- scheduledPublish(scheduledTime, eventData, uniquePerInstance = false) {
123
+ scheduledPublish(scheduledTime, eventData, uniquePerInstance = false, repeatInterval = 0) {
143
124
  return tslib_1.__awaiter(this, void 0, void 0, function* () {
144
125
  const currentTime = new Date();
126
+ delete eventData.repeatInterval;
127
+ if (repeatInterval > 0) {
128
+ eventData.repeatInterval = repeatInterval;
129
+ }
145
130
  if (scheduledTime < currentTime) {
146
131
  throw new Error('PUBLISHER: Cannot schedule an event in the past');
147
132
  }
@@ -212,8 +197,8 @@ class Streams {
212
197
  pipeline.sadd(key, eventName);
213
198
  pipeline.sadd(`${eventName}`, this.consumerGroupName);
214
199
  pipeline.set(setKeyForK8sHandling, this.consumerGroupName);
215
- const [, createConsumerStatus, , ,] = (yield pipeline.exec());
216
- console.log(`PUBLISHER: Consumer Registered and created with ${this.instanceId} under ${this.consumerGroupName} with the ${createConsumerStatus[1]} consumers`);
200
+ const [, createConsumerStatus, addToCGSet, addToFlushSet] = (yield pipeline.exec());
201
+ console.log(`PUBLISHER: Consumer Registered and created with ${this.instanceId} under ${this.consumerGroupName} with ${createConsumerStatus[1]} consumers and with the following status ${JSON.stringify({ addToCGSet, addToFlushSet })}`);
217
202
  return createConsumerStatus[1] === 0 || createConsumerStatus[1] === 1;
218
203
  });
219
204
  }
@@ -279,7 +264,7 @@ class Streams {
279
264
  })}`);
280
265
  });
281
266
  eventStreamClient.on('message', () => tslib_1.__awaiter(this, void 0, void 0, function* () {
282
- console.log(`PUBLISHER: Stream Notification Recieved for event ${eventName}`);
267
+ console.log(`PUBLISHER: Stream Notification Received for event ${eventName}`);
283
268
  yield processMessage(this.redisGroups);
284
269
  }));
285
270
  this.scanAndClaimAUnclaimedMessage(streamName)
@@ -4,6 +4,7 @@ export type EventData<TData, TName extends string> = {
4
4
  eventId?: string;
5
5
  createdAt?: number;
6
6
  republishEvent?: string;
7
+ repeatInterval?: number;
7
8
  };
8
9
  export type PublishData<TData, TName extends string> = {
9
10
  data: TData;