Skip to content

Commit ce12b8c

Browse files
authored
changes for deployment to work fine (#187)
1 parent 08e7d99 commit ce12b8c

File tree

4 files changed

+22
-3
lines changed

4 files changed

+22
-3
lines changed

.env.example

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@ JWT_SECRET = somesupersecretstring
33
REDIS_AUTH = Redis password you get while provisioning a Redis DB
44
REDIS_URL = Redis DB connection string
55
REDIS_PORT = Any port you want.
6+
REDIS_USERNAME = Username of the user that has access to the Redis DB
7+
INSTANCE = ec2 instance id

pubsub.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const options = {
55
host: process.env.REDIS_URL,
66
port: process.env.REDIS_PORT,
77
password: process.env.REDIS_AUTH,
8+
username: process.env.REDIS_USERNAME,
89
retryStrategy: times => {
910
// reconnect after
1011
return Math.min(times * 50, 2000);

serverless.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ useDotenv: true
55
provider:
66
name: aws
77
runtime: nodejs14.x
8+
#allows lambda function to access ec2
9+
iamRoleStatements:
10+
- Effect: "Allow"
11+
Action: "*"
12+
Resource: "*"
813
functions:
914
graphql:
1015
# this is formatted as <FILENAME>.<HANDLER>

serverless/functions/manager.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,18 @@ const options = {
1818

1919
const manageInstance = async () => {
2020
const status = await ec2Client.send(new DescribeInstancesCommand(params));
21-
const state = status["Reservations"][0]["Instances"]["State"]["Status"];
21+
const instances = status["Reservations"][0]["Instances"];
22+
let state;
23+
for (let i = 0; i < instances.length; i++) {
24+
if (instances[i]["InstanceId"] === process.env.INSTANCE) {
25+
state = instances[i]["State"]["Name"];
26+
break;
27+
}
28+
}
29+
if (state == undefined) {
30+
console.log("Error", "Wrong instance ID");
31+
return;
32+
}
2233
const currentTime = new Date();
2334
const beacons = await Beacon.find({
2435
$or: [{ startsAt: { $gte: currentTime } }, { expiresAt: { $gte: currentTime } }],
@@ -35,7 +46,7 @@ const manageInstance = async () => {
3546
return o.startsAt;
3647
})
3748
);
38-
const earliestEnd = Math.min.apply(
49+
const latestEnd = Math.max.apply(
3950
Math,
4051
beacons.map(function (o) {
4152
return o.expiresAt;
@@ -50,7 +61,7 @@ const manageInstance = async () => {
5061
return data;
5162
}
5263
}
53-
if (earliestEnd < currentTime) {
64+
if (latestEnd < currentTime) {
5465
const data = await ec2Client.send(new StopInstancesCommand(params));
5566
console.log("ended instance", data.StoppingInstances);
5667
return data;

0 commit comments

Comments
 (0)