-
Notifications
You must be signed in to change notification settings - Fork 0
/
seedSQSWriterStream.js
55 lines (42 loc) · 1.17 KB
/
seedSQSWriterStream.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
const AWS=require('aws-sdk');
const stream = require('stream');
const seedSQSWriterStream = class seedSQSWriterStream extends stream.Writable {
send(callback) {
while (this.mybuffer.length >= 4) {
var seed = String(this.mybuffer.slice(0,4).readInt32LE());
var params = {
DelaySeconds: 0,
MessageAttributes: {
"seed": {
DataType: "Number",
StringValue: seed,
}
},
MessageBody: "Random seed " + seed,
MessageGroupId: "1",
QueueUrl: process.env.SQS_QUEUE_URL
};
console.log(params);
this.sqs.sendMessage(params, function(err, data) {
if (err) {
console.log("Error", err);
} else {
console.log("Success", data.MessageId);
}
});
this.mybuffer = this.mybuffer.slice(4);
}
callback(null);
}
constructor(queue_url) {
super();
this.mybuffer = new Buffer.from('');
this.sqs = new AWS.SQS();
this.queue_url = queue_url;
}
_write(chunk, encoding, callback) {
this.mybuffer = Buffer.concat([this.mybuffer,chunk]);
this.send(callback);
}
}
module.exports = seedSQSWriterStream;