Skip to content

Commit 50e49b7

Browse files
bartovalgrs
authored andcommitted
update readme and cleanup
1 parent bb32c25 commit 50e49b7

File tree

5 files changed

+46
-59
lines changed

5 files changed

+46
-59
lines changed

examples/rabbitMQ/Readme.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,7 @@ This guide will help you set up and use AMQP 1.0 with RabbitMQ, using the includ
3232

3333
3. **Install Node.js dependencies**:
3434

35-
```bash
36-
npm install rhea minimist
37-
```
35+
- remember to run npm install from the root
3836

3937
4. **Clone or download the example scripts** to your working directory.
4038

examples/rabbitMQ/options.js

Lines changed: 18 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,10 @@
44
* A lightweight wrapper around minimist that provides a more structured API
55
* for defining and parsing command-line options.
66
*/
7-
var util = require('util');
87
var minimist = require('minimist');
98

109
/**
1110
* Formats an option definition into a help text string
12-
*
13-
* @param {Object} optdef - Option definition object
14-
* @returns {String} Formatted description string for the option
1511
*/
1612
function describe(optdef) {
1713
var desc = ' --' + optdef.name;
@@ -21,32 +17,19 @@ function describe(optdef) {
2117
return desc;
2218
}
2319

24-
/**
25-
* Simple wrapper for console.log
26-
*
27-
* @param {String} s - String to print
28-
*/
29-
function print(s) {
30-
console.log(s);
31-
}
32-
3320
/**
3421
* Displays usage information for all options
35-
*
36-
* @param {Array} options - Array of option definition objects
37-
* @param {String} usage - Optional custom usage text
3822
*/
3923
function usage(options, usage) {
4024
console.log(usage || 'options:');
41-
options.map(describe).forEach(print);
25+
options.map(describe).forEach(function(desc) {
26+
console.log(desc);
27+
});
4228
}
4329

4430
/**
4531
* Converts an options object into an array of option definitions
4632
* Ensures each option has a name property (derived from the object key or alias)
47-
*
48-
* @param {Object} options - Options definition object
49-
* @returns {Array} Array of option definition objects
5033
*/
5134
function as_array(options) {
5235
var out = [];
@@ -70,9 +53,6 @@ function as_array(options) {
7053

7154
/**
7255
* Options constructor - Initializes the options parser
73-
*
74-
* @param {Array} options - Array of option definitions
75-
* @constructor
7656
*/
7757
function Options(options) {
7858
this.options = options;
@@ -87,7 +67,7 @@ function Options(options) {
8767

8868
// Convert our option definitions to minimist format
8969
this.options.forEach(function (definition) {
90-
// Set up aliases
70+
// Set up aliases
9171
if (definition.alias) {
9272
minimist_opts.alias[definition.name] = definition.alias;
9373
}
@@ -107,43 +87,38 @@ function Options(options) {
10787

10888
/**
10989
* Adds help functionality
110-
* If the specified option (default 'help') is present, displays usage and exits
111-
*
112-
* @param {String} name - Option name to trigger help (default: 'help')
113-
* @returns {Options} - This Options instance for chaining
90+
* If the specified option (default 'help') is present, displays usage and throws an error
11491
*/
11592
Options.prototype.help = function (name) {
11693
var field = name || 'help';
117-
if (this.argv[name]) {
94+
if (this.argv[field]) {
11895
usage(this.options, this.usage_text);
119-
process.exit(0);
96+
throw new Error('Help displayed');
12097
}
12198
return this;
12299
};
123100

124101
/**
125102
* Sets custom usage text for help display
126-
*
127-
* @param {String} usage - Custom usage message
128-
* @returns {Options} - This Options instance for chaining
129103
*/
130104
Options.prototype.usage = function (usage) {
131105
this.usage_text = usage;
132106
return this;
133107
};
134108

135-
/**
136-
* Module exports
137-
* Provides a factory function to create Options instances
138-
*/
139109
module.exports = {
140110
/**
141-
* Creates a new Options instance with the specified option definitions
142-
*
143-
* @param {Object} options - Object containing option definitions
144-
* @returns {Options} - Configured Options instance
145-
*/
111+
* Creates a new Options instance with the specified option definitions
112+
*/
146113
options: function (options) {
147-
return new Options(as_array(options));
114+
try {
115+
return new Options(as_array(options));
116+
} catch (error) {
117+
if (error.message === 'Help displayed') {
118+
// eslint-disable-next-line no-process-exit
119+
process.exit(0);
120+
}
121+
throw error;
122+
}
148123
},
149124
};

examples/rabbitMQ/rabbitmq_queue_setup.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
* Script for RabbitMQ queue configuration
33
*/
44
const http = require("http");
5-
const querystring = require("querystring");
65

76
var args = require("./options.js")
87
.options({

examples/rabbitMQ/rabbitmq_receiver.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
/*
22
* AMQP 1.0 receiver example for RabbitMQ
33
*/
4-
var container = require('rhea');
4+
5+
// var container = require('rhea');
6+
var container = require('../../lib/container.js'); // local rhea version
57

68
var args = require('./options.js')
79
.options({
@@ -59,7 +61,7 @@ var received = 0;
5961
var expected = args.count;
6062

6163
console.log('Connecting to RabbitMQ at %s:%s...', args.host, args.port);
62-
var connection = container.connect(connection_options);
64+
container.connect(connection_options);
6365

6466
// When the connection is established
6567
container.on('connection_open', function (context) {
@@ -69,7 +71,7 @@ container.on('connection_open', function (context) {
6971
var source = {
7072
address: args.node,
7173
};
72-
var receiver = context.connection.open_receiver({
74+
context.connection.open_receiver({
7375
source: source,
7476
credit_window: args.prefetch,
7577
autoaccept: args.auto_ack,
@@ -79,7 +81,7 @@ container.on('connection_open', function (context) {
7981
});
8082

8183
// When the receiver link is opened
82-
container.on('receiver_open', function (context) {
84+
container.on('receiver_open', function () {
8385
console.log('Receiver link established');
8486
console.log('Waiting for messages...');
8587
});
@@ -142,9 +144,8 @@ container.on('disconnected', function (context) {
142144
received,
143145
expected
144146
);
145-
process.exit(1);
147+
throw new Error('Incomplete message reception');
146148
} else {
147149
console.log('Exiting');
148-
process.exit(0);
149150
}
150151
});

examples/rabbitMQ/rabbitmq_sender.js

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* AMQP 1.0 sender example for RabbitMQ
33
*/
4-
var container = require('rhea');
4+
var container = require('../../lib/container.js'); // local rhea version
55

66
var args = require('./options.js')
77
.options({
@@ -55,12 +55,12 @@ var connection_options = {
5555
};
5656

5757
console.log('Connecting to RabbitMQ at %s:%s...', args.host, args.port);
58-
var connection = container.connect(connection_options);
58+
// Connect but don't store the returned object since we're not using it directly
59+
container.connect(connection_options);
5960

6061
var sent = 0;
6162
var confirmed = 0;
6263
var total = args.count;
63-
var sender;
6464

6565
// When the connection is established
6666
container.on('connection_open', function (context) {
@@ -74,7 +74,7 @@ container.on('connection_open', function (context) {
7474
address: args.node,
7575
capabilities: ['topic'],
7676
};
77-
sender = context.connection.open_sender({
77+
context.connection.open_sender({
7878
target: target,
7979
properties: {
8080
'routing-key': args.address,
@@ -86,7 +86,7 @@ container.on('connection_open', function (context) {
8686
target = {
8787
address: args.node,
8888
};
89-
sender = context.connection.open_sender({
89+
context.connection.open_sender({
9090
target: target,
9191
});
9292
}
@@ -156,7 +156,8 @@ container.on('disconnected', function (context) {
156156
console.log('Attempting to reconnect...');
157157
} else {
158158
console.log('Exiting');
159-
process.exit(0);
159+
// Instead of process.exit(0), throw an error that can be caught
160+
throw new Error('Disconnected and not reconnecting');
160161
}
161162
});
162163

@@ -187,3 +188,16 @@ function send_message(sender) {
187188
body: message_body,
188189
});
189190
}
191+
192+
// Main error handling
193+
process.on('uncaughtException', function(err) {
194+
// Handle the disconnection error gracefully
195+
if (err.message === 'Disconnected and not reconnecting') {
196+
// eslint-disable-next-line no-process-exit
197+
process.exit(0);
198+
} else {
199+
console.error('Uncaught exception:', err);
200+
// eslint-disable-next-line no-process-exit
201+
process.exit(1);
202+
}
203+
});

0 commit comments

Comments
 (0)