-
Notifications
You must be signed in to change notification settings - Fork 949
/
Copy pathlaunchpads.js
76 lines (69 loc) · 1.78 KB
/
launchpads.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
import got from 'got';
import { logger } from '../middleware/index.js';
const API = process.env.SPACEX_API;
const KEY = process.env.SPACEX_KEY;
const HEALTHCHECK = process.env.LAUNCHPADS_HEALTHCHECK;
/**
* Update launchpad attempts/successes
* @return {Promise<void>}
*/
export default async () => {
try {
const launchpads = await got.post(`${API}/launchpads/query`, {
json: {
options: {
pagination: false,
},
},
resolveBodyOnly: true,
responseType: 'json',
});
const updates = launchpads.docs.map(async (launchpad) => {
const [attempts, successes] = await Promise.all([
got.post(`${API}/launches/query`, {
json: {
query: {
launchpad: launchpad.id,
upcoming: false,
},
options: {
pagination: false,
},
},
resolveBodyOnly: true,
responseType: 'json',
}),
got.post(`${API}/launches/query`, {
json: {
query: {
launchpad: launchpad.id,
upcoming: false,
success: true,
},
options: {
pagination: false,
},
},
resolveBodyOnly: true,
responseType: 'json',
}),
]);
await got.patch(`${API}/launchpads/${launchpad.id}`, {
json: {
launch_attempts: attempts.totalDocs,
launch_successes: successes.totalDocs,
},
headers: {
'spacex-key': KEY,
},
});
});
await Promise.all(updates);
logger.info('Launchpads updated');
if (HEALTHCHECK) {
await got(HEALTHCHECK);
}
} catch (error) {
console.log(`Launchpads Error: ${error.message}`);
}
};