prime-sdk 3.0.2 → 3.0.4

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": "prime-sdk",
3
- "version": "3.0.2",
3
+ "version": "3.0.4",
4
4
  "description": "swiss army knife",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -63,4 +63,8 @@ export class Sqs implements IQueue {
63
63
 
64
64
  return await this.sendCommand(deleteCommand);
65
65
  }
66
+
67
+ destroyClient(): void {
68
+ this.client.destroy();
69
+ }
66
70
  }
@@ -5,6 +5,7 @@ export interface IQueue {
5
5
  receiveMessages(): Promise<ReceiveMessageResult>;
6
6
  receiveMessagesV2(): Promise<ReceiveMessageResult>;
7
7
  setup({ waitTimeSeconds, bulk, queueUrl, attributeNames }): void;
8
+ destroyClient(): void;
8
9
  }
9
10
 
10
11
  export const IQueue = Symbol('IQueue');
@@ -16,7 +16,15 @@ export class Queue {
16
16
 
17
17
  private context: IConsumer = null;
18
18
 
19
- private inWork: boolean = false;
19
+ private processing: number = 0;
20
+
21
+ private readonly MAX_TIME_TO_WAIT_FOR_PROCESSING = 20000;
22
+
23
+ private stopped: boolean = false;
24
+
25
+ private abortController: AbortController = null;
26
+
27
+ private inWork: boolean = true;
20
28
 
21
29
  private waitTimeSeconds: number = 20;
22
30
 
@@ -40,6 +48,10 @@ export class Queue {
40
48
  this.waitTimeSeconds = waitTimeSeconds;
41
49
  this.attributeNames = attributeNames;
42
50
  this.version = version;
51
+ this.abortController = new AbortController();
52
+ this.stopped = false;
53
+ this.processing = 0;
54
+ this.inWork = true;
43
55
 
44
56
  this.broker.setup({
45
57
  waitTimeSeconds: this.waitTimeSeconds,
@@ -48,19 +60,24 @@ export class Queue {
48
60
  attributeNames: this.attributeNames,
49
61
  });
50
62
 
51
- while (!this.inWork) {
63
+ while (this.inWork) {
52
64
  await this.run(version);
53
65
  }
54
66
  }
55
67
 
56
68
  async run(version: number) {
57
- this.inWork = true;
58
-
59
- const response = await this.receiveMessages(version);
69
+ this.processing += 1;
60
70
 
61
71
  try {
72
+ const response = await this.receiveMessages(version);
73
+ const messages = response.Messages;
74
+
75
+ if (!messages) {
76
+ return;
77
+ }
78
+
62
79
  if (this.bulk === 1) {
63
- const message = response.Messages?.[0] || null;
80
+ const message = messages?.[0] || null;
64
81
 
65
82
  if (message) {
66
83
  await this.context.handleMessage(message);
@@ -68,20 +85,21 @@ export class Queue {
68
85
  await this.deleteMessage(message.ReceiptHandle);
69
86
  }
70
87
 
71
- this.inWork = false;
72
-
73
88
  return;
74
89
  }
75
90
 
76
- await this.context.handleMessage(response.Messages);
91
+ await this.context.handleMessage(messages);
77
92
 
78
- response.Messages.forEach(async (message: Message) => {
79
- await this.deleteMessage(message.ReceiptHandle);
80
- });
93
+ await Promise.all(
94
+ messages.map((message: Message) =>
95
+ this.deleteMessage(message.ReceiptHandle),
96
+ ),
97
+ );
81
98
  } catch (error) {
82
99
  await this.context.handleError(error);
100
+ } finally {
101
+ this.processing -= 1;
83
102
  }
84
- this.inWork = false;
85
103
  }
86
104
 
87
105
  async receiveMessages(version: number): Promise<ReceiveMessageResult> {
@@ -95,4 +113,26 @@ export class Queue {
95
113
  async deleteMessage(receiptHandle): Promise<unknown> {
96
114
  return await this.broker.deleteMessage(receiptHandle);
97
115
  }
116
+
117
+ async stop(): Promise<void> {
118
+ if (this.stopped) {
119
+ return;
120
+ }
121
+ const timeMax = new Date().getTime() + this.MAX_TIME_TO_WAIT_FOR_PROCESSING;
122
+
123
+ this.inWork = false;
124
+ this.stopped = true;
125
+ this.abortController?.abort();
126
+
127
+ while (this.processing > 0) {
128
+ const time = new Date().getTime();
129
+ if (time >= timeMax) break;
130
+ await this.sleep(500);
131
+ }
132
+ this.broker.destroyClient();
133
+ }
134
+
135
+ private sleep(ms: number) {
136
+ return new Promise((resolve) => setTimeout(resolve, ms));
137
+ }
98
138
  }
Binary file
package/.yarnrc.yml DELETED
@@ -1 +0,0 @@
1
- nodeLinker: node-modules