-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.js
156 lines (146 loc) · 4.06 KB
/
db.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
const mongoose = require("mongoose");
require('dotenv').config();
const passportLocalMongoose = require("passport-local-mongoose");
/*connects to database schedulerDB */
module.exports.connection = function () {
mongoose.connect(process.env.DB_CONNECTION_URL, {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true,
useFindAndModify: false,
});
mongoose.connection
.once("open", function () {
console.log("Database connection has been made");
})
.on("error", function (error) {
console.log("error is" + error);
});
};
/*
creates a collection in schedulerDB with fields
{lambdaURL, timeDelayInMs, taskState}
*/
module.exports.createSchedulerCollection = function () {
const schedulerCollection = mongoose.Schema({
username: String,
taskName:String,
lambdaURL: String,
scheduledTime:String,
retriesCount: Number,
retriesLeft:Number,
timeDelayBetweenRetries: Number,
parameters:String,
taskState: String,
serverResponse:String
});
return mongoose.model("task", schedulerCollection);
};
module.exports.createUsersCollection = function () {
const userAuthCollection = mongoose.Schema({
username: String,
password: String
});
//enable passportLocalMongoose for auth collection
userAuthCollection.plugin(passportLocalMongoose);
return mongoose.model("user", userAuthCollection);
};
module.exports.createOrchestratorCollection = function(){
const orchestratorCollection = mongoose.Schema({
username:String,
taskName:String,
scheduledTime:String,
tasksURL: [String],
conditionCheckURL:String,
fallbackTaskURL:String,
taskState:String,
taskStateDetailed: String,
conditionCheckRetries:Number,
initialDelay:Number,
timeDelayBetweenRetries:Number,
timeDelayForConditionCheck:Number,
serverResponse:String
});
return mongoose.model("orchestratorTask", orchestratorCollection);
};
module.exports.updateTaskState = function (TaskModel, id, taskState) {
TaskModel.findByIdAndUpdate(
id,
{ taskState: taskState },
function (err, result) {
if (err) {
console.log(
"could not update to taskState " + taskState + " of taskId " + id
);
} else {
console.log(
"Updated taskstate to " + taskState + " of taskId " + id
);
}
}
);
};
module.exports.updateTaskStateDetailed = function (TaskModel, id, taskStateDetailed){
TaskModel.findByIdAndUpdate(
id,
{ taskStateDetailed: taskStateDetailed },
function (err, result) {
if (err) {
console.log(
"could not update to taskStateDetailed " + taskStateDetailed + " of taskId " + id
);
} else {
console.log(
"Updated taskStateDetailed to " + taskStateDetailed + " of taskId " + id
);
}
}
);
}
module.exports.updateRetriesLeft = function (TaskModel, id, retriesLeft) {
TaskModel.findByIdAndUpdate(
id,
{ retriesLeft:retriesLeft},
function (err, result) {
if (err) {
console.log(
"could not update to retries left of taskId " + id
);
} else {
console.log('updated retriesLeft to '+retriesLeft);
}
}
);
};
module.exports.updateServerResponse = function (TaskModel, id, msg) {
TaskModel.findByIdAndUpdate(
id,
{ serverResponse:msg},
function (err, result) {
if (err) {
console.log(
"could not update server response of task with " + id
);
} else {
console.log('Updated server response for task with id '+id+' to '+msg);
}
}
);
};
module.exports.modifyTaskScheduledTime = function (TaskModel, id, scheduledTime) {
TaskModel.findByIdAndUpdate(
id,
{ scheduledTime: scheduledTime },
function (err, result) {
if (err) {
console.log(
"could not update to scheduledTime " + scheduledTime + " of taskId " + id
);
} else {
console.log(
"successfully updated scheuldedTime to " + scheduledTime + " of taskId " + id
);
}
}
);
};