@sera4/essentia 1.0.23 → 1.0.25
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 +1 -1
- package/package.tar.gz +0 -0
- package/queue/index.js +17 -19
- package/queue/publisher.js +15 -20
- package/queue/subscriber.js +9 -2
package/package.json
CHANGED
package/package.tar.gz
CHANGED
|
Binary file
|
package/queue/index.js
CHANGED
|
@@ -149,31 +149,29 @@ class S4Queue {
|
|
|
149
149
|
* @param {Object} msg a JSON object as the message.
|
|
150
150
|
*/
|
|
151
151
|
async publishDirectMessage(exchange, key, msg) {
|
|
152
|
-
|
|
153
|
-
const p = this.pubs[exchange];
|
|
152
|
+
const p = this.pubs[exchange];
|
|
154
153
|
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
154
|
+
if (!key) {
|
|
155
|
+
reject(new Error(`Direct message require a key`));
|
|
156
|
+
}
|
|
158
157
|
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
158
|
+
if (!p) {
|
|
159
|
+
reject(new Error(`No PUB for ${exchange} found`));
|
|
160
|
+
}
|
|
162
161
|
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
162
|
+
if (p.type !== 'direct') {
|
|
163
|
+
reject(new Error(`PUB ${exchange} is not direct`));
|
|
164
|
+
}
|
|
166
165
|
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
}
|
|
166
|
+
let content = msg;
|
|
167
|
+
if (content.constructor !== ({}).constructor) {
|
|
168
|
+
// if not in JSON format, let's make it so
|
|
169
|
+
content = {
|
|
170
|
+
msg: msg
|
|
173
171
|
}
|
|
172
|
+
}
|
|
174
173
|
|
|
175
|
-
|
|
176
|
-
});
|
|
174
|
+
return await p.publish({key, content});
|
|
177
175
|
}
|
|
178
176
|
|
|
179
177
|
/**
|
package/queue/publisher.js
CHANGED
|
@@ -59,29 +59,24 @@ class Publisher {
|
|
|
59
59
|
* A tracking uuid for the message will be returned.
|
|
60
60
|
*/
|
|
61
61
|
async publish(msg) {
|
|
62
|
-
|
|
62
|
+
if (!this.channel) {
|
|
63
|
+
throw(new Error(`No channel for ${this.name} to publish`));
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const parcel = {
|
|
67
|
+
tracking_id: uuidv4(),
|
|
68
|
+
data: msg.content
|
|
69
|
+
}
|
|
70
|
+
logger.debug("Publishing message", parcel);
|
|
63
71
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
}
|
|
72
|
+
const content = Buffer.from(JSON.stringify(parcel));
|
|
73
|
+
const res = await this.channel.publish(this.exchange, msg.key, content, {persistent: true});
|
|
67
74
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
data: msg.content
|
|
72
|
-
}
|
|
73
|
-
const content = Buffer.from(JSON.stringify(parcel));
|
|
75
|
+
if (!res) {
|
|
76
|
+
throw(new Error("Channel write buffer is full"));
|
|
77
|
+
}
|
|
74
78
|
|
|
75
|
-
|
|
76
|
-
if (err) {
|
|
77
|
-
logger.error(`An error occured publishing message to ${this.name}`);
|
|
78
|
-
reject(err);
|
|
79
|
-
} else {
|
|
80
|
-
logger.debug("Message delivered:", msg);
|
|
81
|
-
resolve(content.tracking_id);
|
|
82
|
-
}
|
|
83
|
-
});
|
|
84
|
-
});
|
|
79
|
+
return parcel.tracking_id;
|
|
85
80
|
}
|
|
86
81
|
}
|
|
87
82
|
|
package/queue/subscriber.js
CHANGED
|
@@ -63,11 +63,18 @@ class Subscriber {
|
|
|
63
63
|
|
|
64
64
|
this.channel.consume(this.queue, (msg) => {
|
|
65
65
|
const { fields, content } = msg;
|
|
66
|
-
|
|
66
|
+
let payload = null;
|
|
67
|
+
let error = null;
|
|
68
|
+
try {
|
|
69
|
+
payload = JSON.parse(content.toString());
|
|
70
|
+
} catch (e) {
|
|
71
|
+
logger.error("Failed to parse incomming message:", e);
|
|
72
|
+
error = e;
|
|
73
|
+
}
|
|
67
74
|
logger.debug(`Got message from exchange '${fields.exchange}' with key '${fields.routingKey}'`);
|
|
68
75
|
logger.debug("Content", payload);
|
|
69
76
|
this.callbacks.forEach(cb => {
|
|
70
|
-
cb(payload);
|
|
77
|
+
cb(payload, error);
|
|
71
78
|
});
|
|
72
79
|
this.channel.ack(msg);
|
|
73
80
|
}, { noAck: false });
|