-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathindex.js
111 lines (92 loc) · 2.41 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
require('dotenv').config();
const CronJob = require('cron').CronJob
const fs = require('fs')
const slack = require('./lib/slack');
const sheets = require('./lib/sheets');
const mail = require('./lib/mail');
const verifyDomain = require('./lib/verify');
const hbase = require('./lib/hbase');
const handleDomain = async (d, recheck) => {
const check = () => {
try {
console.log('verifying', d.cell, d.data[0], d.data[1])
verifyDomain(...d.data);
return 'valid';
} catch (err) {
return err.toString();
}
}
const result = check();
if (recheck && result === 'valid') {
return Promise.resolve();
}
if (recheck) {
await hbase.deleteDomain(d.data[1], d.data[0]);
}
else if (result === 'valid') {
await hbase.saveDomain(d.data[1], d.data[0]);
}
return sheets.writeCell(d.cell, result)
.then(() => {
if (d.data[1]) {
return Promise.all([
slack.send(d.data[1], d.data[0], result, recheck),
mail.notify(d.data, result)
]);
}
})
.catch(console.log)
};
const handleDomains = (domains, recheck = false) => {
return new Promise((resolve, reject) => {
const total = domains.length;
const next = () => {
const d = domains.shift();
if (d) {
handleDomain(d, recheck)
.then(() => {
setImmediate(next);
});
} else {
console.log('done');
resolve();
}
}
next();
});
};
const verifyNewDomains = () => {
console.log('starting new domain verification');
sheets.authorize()
.then(async () => {
const data = await sheets.getRows();
console.log(`${data.length} new rows`);
return handleDomains(data);
})
.catch(console.log);
}
const checkVerifiedDomains = () => {
console.log('checking verified domains');
sheets.authorize()
.then(async () => {
const data = await sheets.getRows(true);
const verified = data.filter(d => d.data[5] === 'valid' && d.data[6] === undefined);
console.log(`${verified.length} verified domains`);
return handleDomains(verified, true);
})
.catch(console.log);
}
const verifyCron = new CronJob({
cronTime: '00 01 * * * *',
onTick: verifyNewDomains,
start: true,
timeZone: 'America/Los_Angeles'
});
const checkCron = new CronJob({
cronTime: '00 01 01 * * *',
onTick: checkVerifiedDomains,
start: true,
timeZone: 'America/Los_Angeles'
});
verifyNewDomains();
//checkVerifiedDomains();