-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
122 lines (114 loc) · 3.42 KB
/
index.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
const { getValidatorArray, getBalanceOfFaucet } = require("./validators");
const {
sendNoticeEmails,
sendErrorEmails,
sendNotFoundEmails,
sendFaucetBalanceEmail,
} = require("./email");
const { getContactDetails } = require("./contacts");
const schedule = require("node-schedule");
const logger = require("./logger");
const cronstrue = require("cronstrue");
// const cronScheduleFor3D = '0 9 6,15,28 * *'
const cronScheduleFor3D = "0 9 * * 1";
const cronScheduleForFaucetBalance = "5 8 * * MON";
// Set the schedule to run in cron format see helper https://crontab.guru/
// Format:
// minute hour day month day-of-week
// * 13 * * 1
//
// 0 13 * * 1 ==> every Monday at 13:00
// 0 13 */5 * * ==> every fifth day of the month at 13:00 5th, 10th, 15th...
//
if (process.env.NODE_ENV === "development") {
// logger.log("The script is running in dev");
// logger.log(
// "The cronScheduleForQa3D is scheduled to run " +
// cronstrue.toString(cronScheduleFor3D, {
// use24HourTimeFormat: true,
// verbose: true,
// })
// );
// schedule.scheduleJob(
// cronScheduleForFaucetBalance,
// checkValidatorsForMetadataUpdate
// );
} else {
logger.log("The script is running in production");
logger.log(
"The cron is scheduled to run " +
cronstrue.toString(cronScheduleFor3D, {
use24HourTimeFormat: true,
verbose: true,
})
);
logger.log(
"The cron is scheduled to run for Faucet Balance " +
cronstrue.toString(cronScheduleForFaucetBalance, {
use24HourTimeFormat: true,
verbose: true,
})
);
schedule.scheduleJob(
cronScheduleForFaucetBalance,
checkValidatorsForFaucetBalance
);
schedule.scheduleJob(cronScheduleFor3D, runCronOn2ndAnd4thMonday);
}
function checkValidatorsAndSendEmails() {
getValidatorArray()
.then((validatorsArray) => {
const result = validatorsArray.filter(
(validator) =>
!validator.isUp3d &&
validator.contactEmail.length !== 0 &&
validator.lastOnline
);
return result;
})
.then((offlineValidatorsArray) => {
logger.log(
"Got the offline status for validators for 3D- ",
offlineValidatorsArray
);
return sendNoticeEmails(offlineValidatorsArray, CC_CONTACTS);
})
.then((promises) => {
logger.log("✅ ✅ ✅ Run was successful ✅ ✅ ✅");
})
.catch((err) => {
logger.error("SOMETHING WENT WRONG");
logger.error(err.response);
});
}
function checkValidatorsForFaucetBalance() {
getBalanceOfFaucet()
.then((value) => {
if (value < 20) {
console.log("Faucet Balance:", value);
return sendFaucetBalanceEmail(
contactEmailForFaucet,
ccContactsForFaucet,
value
);
}
})
.then((promises) => {
logger.log("✅ ✅ ✅ Run was successful ✅ ✅ ✅");
})
.catch((err) => {
logger.error("SOMETHING WENT WRONG");
logger.error(err.stack);
});
}
function runCronOn2ndAnd4thMonday() {
let date = new Date();
let weekNumber = Math.floor(date.getDate() / 7);
if (weekNumber === 1 || weekNumber === 3) {
checkValidatorsAndSendEmails();
}
}