-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcronjob.js
113 lines (107 loc) · 3.35 KB
/
cronjob.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
import fetch from "node-fetch";
import schedule from "node-schedule";
import createVNuban from "./controller/createVNubanController.js";
const postSlackMessage = async (message) => {
try {
const response = await fetch(process.env.SLACK_WEBHOOK_URL, {
method: 'post',
body: JSON.stringify({
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "Auto generated contract creation status details:\n"
}
},
{
"type": "section",
"fields": [
{
"type": "mrkdwn",
"text": `*Status:*\n${message.status}`
},
{
"type": "mrkdwn",
"text": `*Environment:*\n${message.env}`
},
{
"type": "mrkdwn",
"text": `*Runtime:*\n${message.runTime}`
},
{
"type": "mrkdwn",
"text": `*Total record processed:*\n${message.totalNoOfRecordProcessed}`
},
{
"type": "mrkdwn",
"text": `*Time taken to process(second):*\n${message.totalTimeTaken}`
},
]
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*Result:*\n\n ```" + message.result + "``` "
}
},
]
})
});
const resText = await response.text();
if (resText !== "ok") {
throw new Error(resText);
} else {
console.log(resText);
console.log("Successfully posted message to slack channel")
}
} catch(e) {
console.log(e.message);
console.log("Failed to post message to slack channel")
}
};
const initCronJob = () => {
// nodejs schedule
const rule = new schedule.RecurrenceRule();
rule.hour = 23; // hour of the day at which it will trigger
rule.tz = 'Africa/Lagos'; // UTC+1 === WAT timezone (West Africa Time)
const job = schedule.scheduleJob("*/10 * * * * *", async () => {
const startTime = (new Date()).getTime();
const message = {
status: "",
env: (process?.env?.DB_DATABASE?.indexOf("v2staging") > -1) ? "Staging" : "Production",
runTime: new Date(),
totalTimeTaken: 0,
totalNoOfRecordProcessed: 0,
result:""
};
try {
const result = await fetch(`http://localhost:4020/create-contracts?days=30&noOfRecords=10`);
const responseJSON = await result.json();
message.status="Success";
message.totalNoOfRecordProcessed = responseJSON?.length || 0;
message.result = JSON.stringify(responseJSON);
} catch (e) {
message.status="Failure";
message.result=e.message;
} finally {
const endTime = (new Date()).getTime();
message.totalTimeTaken = (endTime - startTime)/1000;
postSlackMessage(message);
}
});
};
const initVNubanCronJob = () => {
//run between 9 AM WAT to 9 PM WAT except Sundays at interval of 2 hours
// (minute, hour, day(month), month, day(week))
const jobInterval = '0 9-21/2 * * 1-6'
const job1 = schedule.scheduleJob(jobInterval, () => {
try {
createVNuban()
} catch (error) {
console.log("Error scheduling cron job for vNuban ", error);
}
})
};
export {initCronJob, initVNubanCronJob};