-
Notifications
You must be signed in to change notification settings - Fork 0
/
whitelistcron.js
117 lines (103 loc) · 3.26 KB
/
whitelistcron.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
import { google } from 'googleapis';
import * as cron from 'node-cron';
import * as fs from 'fs';
// MAIN --------------------------------------------------------------------------------------------
// Run the getwhitelist function on startup
getWhiteList();
// Run the getwhitelist function every minute
cron.schedule('* * * * *', async () => {
try {
console.log('Update whitelist cron job started');
await getWhiteList();
} catch (error) {
writeToErrorLog(error);
}
}, {
name: 'pheno-plus-wl-update',
});
// Run the clearErrorLog function every week
cron.schedule('0 0 * * 0', () => {
try {
console.log('Clear error log cron job started');
clearErrorLog();
} catch (error) {
writeToErrorLog(error);
}
}, {
name: 'pheno-plus-error-log-clear',
});
//HELPER FUNCTIONS --------------------------------------------------------------------------------------------
async function getWhiteList() {
/**
* Gets the white list from the google sheet
* Writes the white list to a JSON file in the ./secrets folder
*/
const auth = new google.auth.GoogleAuth({
// Secrets is kept out of version control and will need to be added to the application folder manually
keyFile: './secrets/pheno-plus-cred.json',
scopes: ['https://www.googleapis.com/auth/spreadsheets.readonly'],
});
const sheets = google.sheets({
version: 'v4',
auth
});
try {
const fileId = '1SBFAE9DbE5eBjKeehx5ArbaLm4arsi6S2j5sJ7pnCZ0';
const range = 'Sheet1!A:B';
const response = await sheets.spreadsheets.values.get({
spreadsheetId: fileId,
range: range,
});
writeToJsonFile(response.data.values);
return response.data.values;
} catch (error) {
console.error('The API returned an error: ' + error);
writeToErrorLog(error);
throw error;
}
}
function writeToJsonFile(data) {
// remove the first row as it contains the header
data.shift();
// Make the data into an object rather than an array
const obj = {};
data.forEach((row) => {
obj[row[0]] = row[1];
});
// Write the object to a JSON file: STAGE
fs.writeFile('/ssd/emerson/pheno-plus-stage/dist/whiteList.json', JSON.stringify(obj), (err) => {
if (err) {
console.error(err);
writeToErrorLog(err);
return;
}
console.log('stage whitelist file has been updated');
});
// Write the object to a JSON file: PROD
fs.writeFile('/ssd/emerson/pheno-plus-prod/dist/whiteList.json', JSON.stringify(obj), (err) => {
if (err) {
console.error(err);
writeToErrorLog(err);
return;
}
console.log('prod whitelist file has been updated');
});
}
function writeToErrorLog(error) {
fs.appendFile('./secrets/error.log', error, (err) => {
if (err) {
console.error(err);
return;
}
console.log('error has been logged');
});
}
function clearErrorLog() {
fs.writeFile('./secrets/error.log', '', (err) => {
if (err) {
console.error(err);
return;
}
console.log('error log has been cleared');
});
}