Skip to content

Commit

Permalink
Refactor close function and handle errors properly
Browse files Browse the repository at this point in the history
This commit refactors the close function for producers and consumers by removing the unnecessary assignment to 'self'. With this refactor, we'll directly use the 'node' object to close the consumer, producer, or client. Furthermore, it improves error handling in the producer by using 'node.error' instead of 'node.debug', which is more appropriate for displaying error messages.
  • Loading branch information
ng-galien committed Jan 1, 2024
1 parent 76f00b5 commit 48b9585
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 6 deletions.
4 changes: 4 additions & 0 deletions src/pulsar-config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
module.exports = function(RED) {
function PulsarConfigNode(n) {
RED.nodes.createNode(this,n);
const node = this;
node.on('close', function() {
node.client && node.client.close();
});
try {
const Pulsar = require('pulsar-client');
this.client = new Pulsar.Client({
Expand Down
3 changes: 1 addition & 2 deletions src/pulsar-consumer.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,7 @@ module.exports = function(RED) {
node.producerConfig = producerConfig;

node.on('close', async function() {
const self = this;
self.consumer && await self.consumer.close();
node.consumer && await node.consumer.close();
});
node.status({fill: "red", shape: "dot", text: "disconnected"});

Expand Down
7 changes: 3 additions & 4 deletions src/pulsar-producer.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,8 @@ module.exports = function (RED) {
}
node.producerConfig = producerConfig;

node.on('close', async function() {
const self = this;
self.producer && await self.producer.close();
node.on('close', function() {
node.producer && node.producer.close();
});
node.status({fill: "red", shape: "dot", text: "disconnected"});

Expand Down Expand Up @@ -104,7 +103,7 @@ module.exports = function (RED) {
};
node.send(message);
}).catch(e => {
node.debug('Error creating producer: ' + e);
node.error('Error creating producer: ' + e);
node.status({fill: "red", shape: "dot", text: "Connection error"});
});

Expand Down

0 comments on commit 48b9585

Please sign in to comment.