@koala42/redis-highway 0.1.8 → 0.1.9

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.
@@ -5,14 +5,19 @@ export declare abstract class BatchWorker<T extends Record<string, unknown>> {
5
5
  protected streamName: string;
6
6
  protected batchSize: number;
7
7
  protected concurrency: number;
8
+ protected maxFetchSize: number;
8
9
  protected maxRetries: number;
9
10
  protected blockTimeMs: number;
10
11
  private isRunning;
11
12
  private activeCount;
12
- private readonly events;
13
13
  private keys;
14
+ private blockingRedis;
15
+ private readonly events;
14
16
  private readonly consumerId;
15
- constructor(redis: Redis, groupName: string, streamName: string, batchSize?: number, concurrency?: number, maxRetries?: number, blockTimeMs?: number);
17
+ constructor(redis: Redis, groupName: string, streamName: string, batchSize?: number, // How many jobs are passed to the process function (max)
18
+ concurrency?: number, // How many concurrent loops should run
19
+ maxFetchSize?: number, // How many jobs are fetched at once from redis stream
20
+ maxRetries?: number, blockTimeMs?: number);
16
21
  start(): Promise<void>;
17
22
  stop(): Promise<void>;
18
23
  private fetchLoop;
@@ -7,16 +7,21 @@ const stream_message_entity_1 = require("./stream-message-entity");
7
7
  const lua_1 = require("./lua");
8
8
  const uuid_1 = require("uuid");
9
9
  class BatchWorker {
10
- constructor(redis, groupName, streamName, batchSize = 10, concurrency = 1, maxRetries = 3, blockTimeMs = 2000) {
10
+ constructor(redis, groupName, streamName, batchSize = 10, // How many jobs are passed to the process function (max)
11
+ concurrency = 1, // How many concurrent loops should run
12
+ maxFetchSize = 20, // How many jobs are fetched at once from redis stream
13
+ maxRetries = 3, blockTimeMs = 2000) {
11
14
  this.redis = redis;
12
15
  this.groupName = groupName;
13
16
  this.streamName = streamName;
14
17
  this.batchSize = batchSize;
15
18
  this.concurrency = concurrency;
19
+ this.maxFetchSize = maxFetchSize;
16
20
  this.maxRetries = maxRetries;
17
21
  this.blockTimeMs = blockTimeMs;
18
22
  this.isRunning = false;
19
23
  this.activeCount = 0;
24
+ this.blockingRedis = null;
20
25
  this.events = new events_1.EventEmitter();
21
26
  this.consumerId = (0, uuid_1.v7)();
22
27
  if (batchSize < 1) {
@@ -38,6 +43,7 @@ class BatchWorker {
38
43
  throw e;
39
44
  }
40
45
  }
46
+ this.blockingRedis = this.redis.duplicate();
41
47
  this.fetchLoop();
42
48
  }
43
49
  async stop() {
@@ -54,9 +60,13 @@ class BatchWorker {
54
60
  await new Promise((resolve) => this.events.once('job_finished', resolve));
55
61
  continue;
56
62
  }
57
- const itemsCount = freeSlots * this.batchSize;
63
+ const missingItemsCount = freeSlots * this.batchSize;
64
+ const itemsCount = missingItemsCount > this.maxFetchSize ? this.maxFetchSize : missingItemsCount;
58
65
  try {
59
- const results = await this.redis.xreadgroup('GROUP', this.groupName, this.getConsumerName(), 'COUNT', itemsCount, 'BLOCK', this.blockTimeMs, 'STREAMS', this.streamName, '>');
66
+ if (!this.blockingRedis) {
67
+ throw new Error('Blocking Redis connection missing');
68
+ }
69
+ const results = await this.blockingRedis.xreadgroup('GROUP', this.groupName, this.getConsumerName(), 'COUNT', itemsCount, 'BLOCK', this.blockTimeMs, 'STREAMS', this.streamName, '>');
60
70
  if (!results) {
61
71
  continue;
62
72
  }
@@ -67,8 +77,10 @@ class BatchWorker {
67
77
  }
68
78
  }
69
79
  catch (err) {
70
- console.error(`[${this.groupName}] Fetch Error: `, err);
71
- await new Promise((resolve) => setTimeout(resolve, 1000));
80
+ if (this.isRunning) { // Quicker grace shutdown
81
+ console.error(`[${this.groupName}] Fetch Error: `, err);
82
+ await new Promise((resolve) => setTimeout(resolve, 1000));
83
+ }
72
84
  }
73
85
  }
74
86
  }
@@ -201,9 +213,7 @@ class BatchWorker {
201
213
  for (const msg of messages) {
202
214
  const statusKey = this.keys.getJobStatusKey(msg.messageUuid);
203
215
  const dataKey = this.keys.getJobDataKey(msg.messageUuid);
204
- pipeline.eval(lua_1.LUA_FINALIZE_COMPLEX, 3, statusKey, dataKey, this.streamName, // Keys
205
- this.groupName, timestamp, msg.streamMessageId // args
206
- );
216
+ pipeline.eval(lua_1.LUA_FINALIZE_COMPLEX, 3, statusKey, dataKey, this.streamName, this.groupName, timestamp, msg.streamMessageId);
207
217
  }
208
218
  await pipeline.exec();
209
219
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@koala42/redis-highway",
3
- "version": "0.1.8",
3
+ "version": "0.1.9",
4
4
  "description": "High performance redis queue",
5
5
  "license": "MIT",
6
6
  "author": {