zyket 1.2.16 → 1.2.17

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": "zyket",
3
- "version": "1.2.16",
3
+ "version": "1.2.17",
4
4
  "main": "index.js",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1"
@@ -1,8 +1,25 @@
1
- module.exports = class Worker {
2
- name;
3
- queueName = null;
4
-
5
- constructor(name) {
6
- this.name = name;
7
- }
8
- }
1
+ module.exports = class Worker {
2
+ name;
3
+ queueName = null;
4
+
5
+ // Per-instance configuration. Controls how many BullMQ workers are spawned
6
+ // for this class and what extra info each one receives:
7
+ // - number -> spawns N identical instances (instance = {} for each)
8
+ // - array -> spawns one instance per entry, each entry passed to handle()
9
+ // as `instance` (e.g. [{ proxy: '...' }, { proxy: '...' }])
10
+ // - function({ container }) -> resolved at boot, must return a number/array
11
+ // (e.g. load the proxy list from env/DB dynamically)
12
+ // - empty -> a single default instance
13
+ instances = [];
14
+
15
+ // BullMQ Worker options (e.g. { concurrency: 5 }). Can be:
16
+ // - object -> applied to every instance
17
+ // - function({ container, instance, index }) -> resolved at boot per
18
+ // instance (sync or async), so options can be computed
19
+ // dynamically (per-proxy concurrency, env-driven values, ...)
20
+ options = {};
21
+
22
+ constructor(name) {
23
+ this.name = name;
24
+ }
25
+ }
@@ -9,6 +9,7 @@ module.exports = class BullMQ extends Service {
9
9
  #container
10
10
  queues = {};
11
11
  queuesEvents = {};
12
+ workers = [];
12
13
 
13
14
  constructor(container) {
14
15
  super("queues");
@@ -35,13 +36,40 @@ module.exports = class BullMQ extends Service {
35
36
  this.#container.get('logger').warn(`Queue ${wkr.queueName} not found for worker ${wkr.name}, skipping...`);
36
37
  continue;
37
38
  }
38
- new BullWorker(
39
- wkr.queueName,
40
- async (job) => wkr.handle({ container: this.#container, job }),
41
- this.#connection()
42
- );
43
- this.#container.get('logger').info(`Worker ${wkr.name} for queue ${wkr.queueName} initialized`);
39
+
40
+ const instances = await this.#resolveInstances(wkr);
41
+ for (const [index, instance] of instances.entries()) {
42
+ const options = await this.#resolveOptions(wkr, instance, index);
43
+ const bullWorker = new BullWorker(
44
+ wkr.queueName,
45
+ async (job) => wkr.handle({ container: this.#container, job, instance, index }),
46
+ { ...this.#connection(), ...options }
47
+ );
48
+ this.workers.push(bullWorker);
49
+ }
50
+ this.#container.get('logger').info(`Worker ${wkr.name} for queue ${wkr.queueName} initialized with ${instances.length} instance(s)`);
51
+ }
52
+ }
53
+
54
+ async #resolveInstances(wkr) {
55
+ let instances = wkr.instances;
56
+ if (typeof instances === 'function') {
57
+ instances = await instances({ container: this.#container });
44
58
  }
59
+ if (typeof instances === 'number' && instances > 0) {
60
+ return Array.from({ length: instances }, () => ({}));
61
+ }
62
+ if (Array.isArray(instances) && instances.length) {
63
+ return instances;
64
+ }
65
+ return [{}];
66
+ }
67
+
68
+ async #resolveOptions(wkr, instance, index) {
69
+ const options = typeof wkr.options === 'function'
70
+ ? await wkr.options({ container: this.#container, instance, index })
71
+ : wkr.options;
72
+ return options || {};
45
73
  }
46
74
 
47
75
  async addJob(queueName, jobName, data, opts = {}, waitForCompletion = false) {