Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow custom IDs to be passed into sendMessage #130

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ Parameters:
* `qname` (String)
* `message` (String)
* `delay` (Number): *optional* *(Default: queue settings)* The time in seconds that the delivery of the message will be delayed. Allowed values: 0-9999999 (around 115 days)
* `id` (String): *optional* *(Default: auto-generated)* Must be 32 characters from the alphabet `{A-Z, a-z, 0-9, :}`

Returns:

Expand Down
9 changes: 5 additions & 4 deletions _src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -504,10 +504,11 @@ class RedisSMQ extends EventEmitter {
if (this._validate(options, ["qname"],cb) === false)
return

this._getQueue(options.qname, true, (err, q) => {
this._getQueue(options.qname, options["id"] == null, (err, q) => {
if (err) { this._handleError(cb, err); return; }
// Now that we got the default queue settings
options.delay = options.delay != null ? options.delay : q.delay;
options.id = options.id != null ? options.id : q.uid;

if (this._validate(options, ["delay"],cb) === false)
return;
Expand All @@ -525,8 +526,8 @@ class RedisSMQ extends EventEmitter {
// Ready to store the message
const key = `${this.redisns}${options.qname}`;
const mc = [
["zadd", key, q.ts + options.delay * 1000, q.uid],
["hset", `${key}:Q`, q.uid, options.message],
["zadd", key, q.ts + options.delay * 1000, options.id],
["hset", `${key}:Q`, options.id, options.message],
["hincrby", `${key}:Q`, "totalsent", 1]
];

Expand All @@ -541,7 +542,7 @@ class RedisSMQ extends EventEmitter {
if (this.realtime) {
this.redis.publish(`${this.redisns}rt:${options.qname}`, resp[3]);
}
cb(null, q.uid);
cb(null, options.id);
});
});
}
Expand Down
10 changes: 10 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,16 @@ declare namespace RedisSMQ {
* @memberof SendMessageOptions
*/
delay?: number;

/**
* *(Default: auto-generated)*
* The internal ID to use for this message.
* Allowed values: 32 character strings from the alphabet `{A-Z, a-z, 0-9, :}`
*
* @type {string}
* @memberOf SendMessageOptions
*/
id?: string;
}

export interface ReceiveMessageOptions extends BaseOptions {
Expand Down
9 changes: 5 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -339,12 +339,13 @@ class RedisSMQ extends EventEmitter {
this.sendMessage = (options, cb) => {
if (this._validate(options, ["qname"], cb) === false)
return;
this._getQueue(options.qname, true, (err, q) => {
this._getQueue(options.qname, options["id"] == null, (err, q) => {
if (err) {
this._handleError(cb, err);
return;
}
options.delay = options.delay != null ? options.delay : q.delay;
options.id = options.id != null ? options.id : q.uid;
if (this._validate(options, ["delay"], cb) === false)
return;
if (typeof options.message !== "string") {
Expand All @@ -357,8 +358,8 @@ class RedisSMQ extends EventEmitter {
}
const key = `${this.redisns}${options.qname}`;
const mc = [
["zadd", key, q.ts + options.delay * 1000, q.uid],
["hset", `${key}:Q`, q.uid, options.message],
["zadd", key, q.ts + options.delay * 1000, options.id],
["hset", `${key}:Q`, options.id, options.message],
["hincrby", `${key}:Q`, "totalsent", 1]
];
if (this.realtime) {
Expand All @@ -372,7 +373,7 @@ class RedisSMQ extends EventEmitter {
if (this.realtime) {
this.redis.publish(`${this.redisns}rt:${options.qname}`, resp[3]);
}
cb(null, q.uid);
cb(null, options.id);
});
});
};
Expand Down
6 changes: 4 additions & 2 deletions test/test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -440,10 +440,12 @@ describe 'Redis-Simple-Message-Queue Test', ->
return

it 'Send message 3', (done) ->
rsmq.sendMessage {qname: queue1.name, message:"Booo!!"}, (err, resp) ->
mid = "f64510ecd3ee4555af745fbdd551b538"
rsmq.sendMessage {qname: queue1.name, message:"Booo!!", id: mid}, (err, resp) ->
should.not.exist(err)
resp.should.equal(mid)
q1m3=
id: resp
id: mid
message: "Booo!!"
done()
return
Expand Down
Loading