-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
64 lines (53 loc) · 1.63 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
require('dotenv').config()
const request = require('superagent')
const { GOOGLE_CLOUD_PROJECT, LOG, REQUEST_RESULTS_URL, WAIT } = process.env
const { rebootModem } = require('./lib/modem')
const { pickAndRequest } = require('./lib/pickEpisodes')
const {
sleepFromOhConfig,
waitUntilConnectionIsDown,
waitUntilConnectionIsUp
} = require('./lib/wait')
const insertInBQ = GOOGLE_CLOUD_PROJECT ? require('./lib/insertInBQ') : null
const log = (...args) => {
if (LOG) console.log(new Date(), ...args)
}
const unitProcess = async () => {
try {
console.time('Unit loop')
log('Pick and request...')
const downloads = await pickAndRequest()
log(
`Downloads process done. Requested ${downloads.length} elements. Store them if needed...`
)
try {
if (insertInBQ && downloads.length) await insertInBQ(downloads)
if (REQUEST_RESULTS_URL) {
await request.get(REQUEST_RESULTS_URL).query({ downloads })
}
} catch (e) {
console.error('Error storing data', e)
}
log('Reboot modem... and wait until connection is down...')
await rebootModem()
await waitUntilConnectionIsDown()
log('Connection down. Wait until connection is up again...')
await waitUntilConnectionIsUp()
log(
'Connection is up again ! Loop after waiting as expected in the config.'
)
if (LOG) {
console.timeEnd('Unit loop')
console.log()
}
await sleepFromOhConfig(WAIT)
} catch (e) {
console.error('Error in the unit process', e)
}
}
const main = async () => {
while (true) {
await unitProcess()
}
}
main().catch((e) => console.error('GLOBAL ERROR', e))