-
Notifications
You must be signed in to change notification settings - Fork 0
/
producer.js
81 lines (71 loc) · 1.89 KB
/
producer.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
const { SQSClient, SendMessageCommand } = require("@aws-sdk/client-sqs");
const { LambdaClient, InvokeCommand } = require("@aws-sdk/client-lambda");
const client = new SQSClient({
region: process.env.AWS_REGION
});
const lambdaClient = new LambdaClient({
region: process.env.AWS_REGION
});
const maxLambdas = 10
let lambdasInvoked = 0;
function rand(length) {
var result = '';
var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
var charactersLength = characters.length;
for (var i = 0; i < length; i++) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return result;
}
const main = async (event) => {
let statusCode = 200;
let message;
if (event.requestContext.condition === 'Success') {
console.log("Success");
lambdasInvoked -= 1
console.log(lambdasInvoked)
return { statusCode };
}
try {
var temp = rand(10)
var command = new SendMessageCommand({
QueueUrl: process.env.QUEUE_URL,
MessageBody: temp,
MessageGroupId: temp,
MessageDeduplicationId: temp,
MessageAttributes: {
AttributeName: {
StringValue: "Attribute Value",
DataType: "String",
},
},
});
await client.send(command);
message = "Message accepted!";
console.log(lambdasInvoked)
if (event.body && maxLambdas > lambdasInvoked) {
command = new InvokeCommand({
FunctionName: 'sqs-service-dev-consumer',
InvocationType: 'Event',
Payload: JSON.stringify({
body: "10",
}),
});
await lambdaClient.send(command);
lambdasInvoked += 1
}
} catch (error) {
console.log(error);
message = error;
statusCode = 500;
}
return {
statusCode,
body: JSON.stringify({
message,
}),
};
};
module.exports = {
main,
};