-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy pathindex.js
126 lines (119 loc) · 4.31 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/**
* Copyright 2022 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for t`he specific language governing permissions and
* limitations under the License.
*/
"use strict";
const path = require("path");
const fetch = require("node-fetch");
const functions = require("firebase-functions");
const {onTaskDispatched} = require("firebase-functions/v2/tasks");
const {onRequest} = require("firebase-functions/v2/https");
const {initializeApp} = require("firebase-admin/app");
const {getFunctions} = require("firebase-admin/functions");
const {getStorage} = require("firebase-admin/storage");
const logger = functions.logger;
const HttpsError = functions.https.HttpsError;
initializeApp();
const BACKUP_START_DATE = new Date("1995-06-17");
const BACKUP_COUNT = parseInt(process.env.BACKUP_COUNT) || 100;
const HOURLY_BATCH_SIZE = parseInt(process.env.HOURLY_BATCH_SIZE) || 500;
const BACKUP_BUCKET = process.env.BACKUP_BUCKET;
/**
* Grabs Astronomy Photo of the Day (APOD) using NASA's API.
*
*/
// [START v2TaskFunctionSetup]
exports.backupapod = onTaskDispatched(
{
retryConfig: {
maxAttempts: 5,
minBackoffSeconds: 60,
},
rateLimits: {
maxConcurrentDispatches: 6,
},
}, async (req) => {
// [END v2TaskFunctionSetup]
const date = req.data.date;
if (!date) {
logger.warn("Invalid payload. Must include date.");
throw new HttpsError(
"invalid-argument",
"Invalid payload. Must include date.",
);
}
logger.info(`Requesting data from apod api for date ${date}`);
let url = "https://api.nasa.gov/planetary/apod";
url += `?date=${date}`;
url += `&api_key=${process.env.NASA_API_KEY}`;
const apiResp = await fetch(url);
if (!apiResp.ok) {
logger.warn(
`request to NASA APOD API failed with reponse ${apiResp.status}`,
);
if (apiResp.status === 404) {
// APOD not published for the day. This is fine!
return;
}
if (apiResp.status >= 500) {
throw new HttpsError(
"unavailable",
"APOD API temporarily not available.",
);
}
throw new HttpsError("internal", "Uh-oh. Something broke.");
}
const apod = await apiResp.json();
const picUrl = apod.hdurl;
logger.info(`Fetched ${picUrl} from NASA API for date ${date}.`);
const picResp = await fetch(picUrl);
const dest = getStorage()
.bucket(BACKUP_BUCKET)
.file(`apod/${date}${path.extname(picUrl)}`);
try {
await new Promise((resolve, reject) => {
const stream = dest.createWriteStream();
picResp.body.pipe(stream);
picResp.body.on("end", resolve);
stream.on("error", reject);
});
} catch (err) {
logger.error(`Failed to upload ${picUrl} to ${dest.name}`, err);
throw new HttpsError("internal", "Uh-oh. Something broke.");
}
});
// [START v2EnqueueTasks]
exports.enqueuebackuptasks = onRequest(
async (_request, response) => {
const queue = getFunctions().taskQueue("backupapod");
const enqueues = [];
for (let i = 0; i <= BACKUP_COUNT; i += 1) {
const iteration = Math.floor(i / HOURLY_BATCH_SIZE);
// Delay each batch by N * hour
const scheduleDelaySeconds = iteration * (60 * 60);
const backupDate = new Date(BACKUP_START_DATE);
backupDate.setDate(BACKUP_START_DATE.getDate() + i);
// Extract just the date portion (YYYY-MM-DD) as string.
const date = backupDate.toISOString().substring(0, 10);
enqueues.push(
queue.enqueue({date}, {
scheduleDelaySeconds,
dispatchDeadlineSeconds: 60 * 5, // 5 minutes
}),
);
}
await Promise.all(enqueues);
response.sendStatus(200);
});
// [END v2EnqueueTasks]