consumer-pgmq 1.0.5 → 2.0.0

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/README.md CHANGED
@@ -40,6 +40,7 @@ yarn add consumer-pgmq
40
40
  - poolSize: The number of consumers. PS: this is the number of consumers that will be created to consume the messages and
41
41
  if you use read consume type, the pool size is the number of messages will get at the same time.
42
42
  - timeMsWaitBeforeNextPolling: The time in milliseconds to wait before the next polling
43
+ - enabledPolling: The enabled polling. PS: if true, the consumer will poll the message, if false, the consumer will consume the message one time and stop. PS: is required to the versions more than 1.0.5
43
44
 
44
45
  ## Events
45
46
 
package/dist/consumer.js CHANGED
@@ -57,7 +57,7 @@ class Consumer extends events_1.EventEmitter {
57
57
  if (error) {
58
58
  throw error;
59
59
  }
60
- if (data.length === 0) {
60
+ if (data.length === 0 && this.options.enabledPolling) {
61
61
  setTimeout(() => this.pollMessage(), (this.options.timeMsWaitBeforeNextPolling || 1000) * 10);
62
62
  return;
63
63
  }
@@ -82,6 +82,9 @@ class Consumer extends events_1.EventEmitter {
82
82
  }
83
83
  }
84
84
  finally {
85
+ if (!this.options.enabledPolling) {
86
+ return;
87
+ }
85
88
  setTimeout(() => this.pollMessage(), this.options.timeMsWaitBeforeNextPolling || 1000);
86
89
  }
87
90
  }
@@ -27,17 +27,18 @@ async function start() {
27
27
  visibilityTime: 15,
28
28
  consumeType: "read",
29
29
  poolSize: 4,
30
- timeMsWaitBeforeNextPolling: 1000
30
+ timeMsWaitBeforeNextPolling: 1000,
31
+ enabledPolling: true
31
32
  },
32
33
  async function (message: { [key: string]: any }, signal): Promise<void> {
33
34
  try {
34
35
  console.log(message)
35
- const url = "https://jsonplaceholder.typicode.com/todos/1";
36
- await timersPromises.setTimeout(100, null, { signal });
37
- console.log("Fetching data...");
38
- const response = await fetch(url, { signal });
39
- const todo = await response.json();
40
- console.log("Todo:", todo);
36
+ // const url = "https://jsonplaceholder.typicode.com/todos/1";
37
+ // await timersPromises.setTimeout(100, null, { signal });
38
+ // console.log("Fetching data...");
39
+ // const response = await fetch(url, { signal });
40
+ // const todo = await response.json();
41
+ // console.log("Todo:", todo);
41
42
  } catch (error: any) {
42
43
  if (error.name === "AbortError") {
43
44
  console.log("Operation aborted");
@@ -49,9 +50,9 @@ async function start() {
49
50
  postgresQueueDriver
50
51
  );
51
52
 
52
- consumer.on('finish', (message: { [key: string]: any }) => {
53
- console.log('Consumed message =>', message);
54
- });
53
+ // consumer.on('finish', (message: { [key: string]: any }) => {
54
+ // console.log('Consumed message =>', message);
55
+ // });
55
56
 
56
57
  consumer.on("abort-error", (err) => {
57
58
  console.log("Abort error =>", err)
@@ -25,45 +25,39 @@ const supabaseQueueDriver = new SupabaseQueueDriver(
25
25
  import timersPromises from "node:timers/promises";
26
26
 
27
27
  async function start() {
28
- for (let i = 0; i < 200; i++) {
29
- await supabase.rpc("send", {
30
- queue_name: "subscriptions",
31
- message: { "message": `Message triggered at ${Date.now()}` }
32
- });
33
- }
34
- console.log("Total messages sent: ", 200)
28
+ // for (let i = 0; i < 200; i++) {
29
+ // await supabase.rpc("send", {
30
+ // queue_name: "subscriptions",
31
+ // message: { "message": `Message triggered at ${Date.now()}` }
32
+ // });
33
+ // }
34
+ // console.log("Total messages sent: ", 200)
35
35
 
36
36
  const consumer = new Consumer(
37
37
  {
38
38
  queueName: 'subscriptions',
39
39
  visibilityTime: 15,
40
40
  consumeType: "read",
41
- poolSize: 4,
42
- timeMsWaitBeforeNextPolling: 1000
41
+ poolSize: 8,
42
+ timeMsWaitBeforeNextPolling: 1000,
43
+ enabledPolling: false
43
44
  },
44
45
  async function (message: { [key: string]: any }, signal): Promise<void> {
45
46
  try {
47
+ if (message.error) {
48
+ throw new Error("Error in message")
49
+ }
46
50
  console.log(message)
47
- const url = "https://jsonplaceholder.typicode.com/todos/1";
48
- await timersPromises.setTimeout(100, null, { signal });
49
- console.log("Fetching data...");
50
- const response = await fetch(url, { signal });
51
- const todo = await response.json();
52
- console.log("Todo:", todo);
53
51
  } catch (error: any) {
54
- if (error.name === "AbortError") {
55
- console.log("Operation aborted");
56
- } else {
57
- console.error("Error:", error);
58
- }
52
+ throw error
59
53
  }
60
54
  },
61
55
  supabaseQueueDriver
62
56
  );
63
57
 
64
- consumer.on('finish', (message: { [key: string]: any }) => {
65
- console.log('Consumed message =>', message);
66
- });
58
+ // consumer.on('finish', (message: { [key: string]: any }) => {
59
+ // console.log('Consumed message =>', message);
60
+ // });
67
61
 
68
62
  consumer.on("abort-error", (err) => {
69
63
  console.log("Abort error =>", err)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "consumer-pgmq",
3
- "version": "1.0.5",
3
+ "version": "2.0.0",
4
4
  "description": "The consumer of Supabase pgmq",
5
5
  "main": "dist/index.js",
6
6
  "type": "commonjs",
package/src/consumer.ts CHANGED
@@ -92,7 +92,7 @@ class Consumer extends EventEmitter {
92
92
  throw error;
93
93
  }
94
94
 
95
- if (data.length === 0) {
95
+ if (data.length === 0 && this.options.enabledPolling) {
96
96
  setTimeout(
97
97
  () => this.pollMessage(),
98
98
  (this.options.timeMsWaitBeforeNextPolling || 1000) * 10
@@ -123,6 +123,9 @@ class Consumer extends EventEmitter {
123
123
  this.emit('error', err);
124
124
  }
125
125
  } finally {
126
+ if (!this.options.enabledPolling) {
127
+ return;
128
+ }
126
129
  setTimeout(() => this.pollMessage(), this.options.timeMsWaitBeforeNextPolling || 1000);
127
130
  }
128
131
  }
package/src/type.ts CHANGED
@@ -25,6 +25,11 @@ interface Options {
25
25
  * The time to wait before next polling. PS: the time in milliseconds
26
26
  */
27
27
  timeMsWaitBeforeNextPolling?: number;
28
+
29
+ /**
30
+ * The enabled polling. PS: if true, the consumer will poll the message
31
+ */
32
+ enabledPolling: boolean;
28
33
  }
29
34
 
30
35
  /**